### Install from source using pip Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/installation.txt Steps to download, build, and install django-celery-beat from a source tarball. Ensure you have build tools installed. ```bash python3 -m venv .venv source .venv/bin/activate pip install --upgrade build pip tar xvfz django-celery-beat-0.0.0.tar.gz cd django-celery-beat-0.0.0 python -m build pip install . ``` -------------------------------- ### Install development version using pip Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/installation.txt Install the latest snapshot from the master branch on GitHub. This provides the most recent code but may be less stable. ```bash pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat ``` -------------------------------- ### Start Django Celery Beat Scheduler (DatabaseScheduler) Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Starts the Celery beat service using the DatabaseScheduler. Replace '[project-name]' with your Django project name. ```bash celery -A [project-name] beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler ``` -------------------------------- ### Install django-celery-beat using pip Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/installation.txt Use this command to install the latest stable version of django-celery-beat from PyPI. ```bash pip install --upgrade django-celery-beat ``` -------------------------------- ### Install Latest Development Version Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Installs the latest development version of django-celery-beat directly from its GitHub repository using pip. ```bash pip install git+https://github.com/celery/django-celery-beat#egg=django-celery-beat ``` -------------------------------- ### Start Celery Worker and Beat Together (Development) Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Combines starting Celery worker and beat services into a single command, recommended for development environments only. Replace '[project-name]' with your Django project name. ```bash celery -A [project-name] worker --beat --scheduler django --loglevel=info ``` -------------------------------- ### Start Celery Worker Service Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt This command starts a Celery worker. Replace '[project-name]' with your actual Django project name. Ensure the default Celery package is installed. ```sh $ celery -A [project-name] worker --loglevel=info ``` -------------------------------- ### Install from default branch using pip Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/installation.txt Install the current default branch of django-celery-beat directly from GitHub. This is useful for testing the latest features. ```bash python3 -m venv .venv source .venv/bin/activate pip install --upgrade pip pip install git+https://github.com/celery/django-celery-beat.git ``` -------------------------------- ### Run Local Development Environment Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Starts a local development environment for django-celery-beat using Docker Compose. Access the Django admin at http://127.0.0.1:58000/admin/. ```bash docker-compose up --build ``` -------------------------------- ### Start Celery Beat Service with Scheduler Flag Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt An alternative way to start the Celery beat service using the '-S' flag to specify the Django scheduler. For more options, refer to 'celery beat --help'. ```sh $ celery -A [project-name] beat -l info -S django ``` -------------------------------- ### Create a Basic Periodic Task with Interval Schedule Source: https://github.com/celery/django-celery-beat/blob/main/README.rst This example shows how to create a PeriodicTask that is associated with a previously defined IntervalSchedule. It includes the task name and the name of the Celery task to be executed. ```Python >>> PeriodicTask.objects.create( ... interval=schedule, # we created this above. ... name='Importing contacts', # simply describes this periodic task. ... task='proj.tasks.import_contacts', # name of task. ... ) ``` -------------------------------- ### Start Celery Beat Service with Django Scheduler Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt This command starts the Celery beat service, which is responsible for triggering periodic tasks. It uses the Django scheduler. Ensure both worker and beat services run concurrently. ```sh $ celery -A [project-name] beat -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler ``` -------------------------------- ### Create Interval Schedule Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt Create an IntervalSchedule object to define how often a periodic task should run. This example sets a task to run every 10 seconds. ```python from django_celery_beat.models import PeriodicTask, IntervalSchedule # executes every 10 seconds. schedule, created = IntervalSchedule.objects.get_or_create( every=10, period=IntervalSchedule.SECONDS, ) ``` -------------------------------- ### Run django_celery_beat Migrations Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Applies database migrations for django_celery_beat using Django's manage.py command. ```bash python manage.py migrate django_celery_beat ``` -------------------------------- ### Create a Periodic Task with Arguments, Keyword Arguments, and Expiry Source: https://github.com/celery/django-celery-beat/blob/main/README.rst This snippet demonstrates creating a PeriodicTask with additional parameters, including arguments (args), keyword arguments (kwargs), and an expiry time. Arguments and keyword arguments must be JSON serialized. ```Python >>> import json >>> from datetime import datetime, timedelta >>> PeriodicTask.objects.create( ... interval=schedule, # we created this above. ... name='Importing contacts', # simply describes this periodic task. ... task='proj.tasks.import_contacts', # name of task. ... args=json.dumps(['arg1', 'arg2']), ... kwargs=json.dumps({ ... 'be_careful': True, ... }), ... expires=datetime.utcnow() + timedelta(seconds=30) ... ) ``` -------------------------------- ### Create Periodic Task with Arguments and Expiry Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt Create a PeriodicTask with specific arguments, keyword arguments (JSON serialized), and an expiry time. The arguments and keyword arguments must be JSON encoded. ```python import json from datetime import datetime, timedelta PeriodicTask.objects.create( interval=schedule, # we created this above. name='Importing contacts', # simply describes this periodic task. task='proj.tasks.import_contacts', # name of task. args=json.dumps(['arg1', 'arg2']), kwargs=json.dumps({ 'be_careful': True, }), expires=datetime.utcnow() + timedelta(seconds=30) ) ``` -------------------------------- ### Create Periodic Task with Crontab Schedule Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt This code demonstrates how to create a periodic task and associate it with a previously defined crontab schedule. Ensure the schedule object and task details are correctly specified. ```python PeriodicTask.objects.create( crontab=schedule, name='Importing contacts', task='proj.tasks.import_contacts', ) ``` -------------------------------- ### Run Celery Worker and Beat Together (Development) Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt This command is a convenient way to run both the Celery worker and beat services simultaneously. It is recommended for development environments only. ```sh $ celery -A [project-name] worker --beat --scheduler django --loglevel=info ``` -------------------------------- ### Create Periodic Task with Interval Schedule Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt After creating an IntervalSchedule, create a PeriodicTask object, associating it with the schedule and providing task details. ```python PeriodicTask.objects.create( interval=schedule, # we created this above. name='Importing contacts', # simply describes this periodic task. task='proj.tasks.import_contacts', # name of task. ) ``` -------------------------------- ### Add django_celery_beat to Django Settings Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Configures Django settings to include 'django_celery_beat' in INSTALLED_APPS. ```Python INSTALLED_APPS = [ ..., 'django_celery_beat', ] ``` -------------------------------- ### Configure Timezone Awareness Source: https://github.com/celery/django-celery-beat/blob/main/README.rst Sets DJANGO_CELERY_BEAT_TZ_AWARE to False in Django settings to make the project timezone naive. ```Python DJANGO_CELERY_BEAT_TZ_AWARE=False ``` -------------------------------- ### Create Crontab Schedule Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt Use this snippet to create a crontab schedule object in Django. This is useful for defining recurring tasks based on minute, hour, day of week, day of month, and month of year. ```python from django_celery_beat.models import CrontabSchedule, PeriodicTask schedule, _ = CrontabSchedule.objects.get_or_create( minute='30', hour='*', day_of_week='*', day_of_month='*', month_of_year='*', ) ``` -------------------------------- ### Configure Celery Beat Scheduler in Django Settings Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt You can configure the Celery beat scheduler directly in your Django settings file by setting the 'CELERY_BEAT_SCHEDULER' variable. ```python CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler' ``` -------------------------------- ### JavaScript for Crontab Translation Source: https://github.com/celery/django-celery-beat/blob/main/django_celery_beat/templates/admin/djcelery/change_periodictask_form.html Enhances the crontab field in the Django admin form by displaying human-readable translations of crontab schedules. It parses JSON data embedded in the template and updates the help text dynamically. ```html {% extends 'admin/change_form.html' %} {% block admin_change_form_document_ready %} {{ block.super }} {{ readable_crontabs|json_script:"readable-crontabs" }} const readableCrontabs = JSON.parse(django.jQuery("#readable-crontabs").text()); var originalCrontabHelp = "{{ adminform.form.fields.crontab.help_text }}"; var updateCrontabHelp = function(additional) { django.jQuery(".field-crontab .help").html( `${originalCrontabHelp}
Translation: ${additional} `); }; django.jQuery(".field-crontab_translation").hide() django.jQuery(".field-crontab").change(function() { updateCrontabHelp(readableCrontabs[django.jQuery("#id_crontab").val()]); }); {% endblock %} ``` -------------------------------- ### Manually Update Changed Counter Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt When updating periodic tasks in bulk, the counter that signals schedule changes to the celery beat service needs to be updated manually. ```python from django_celery_beat.models import PeriodicTasks PeriodicTasks.changed() ``` -------------------------------- ### Resetting Periodic Task Last Run Time Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt If the Django TIME_ZONE setting is changed, periodic task schedules may become misaligned. Reset the 'last run time' for all periodic tasks to fix this. ```python from django_celery_beat.models import PeriodicTask, PeriodicTasks PeriodicTask.objects.update(last_run_at=None) PeriodicTasks.update_changed() ``` -------------------------------- ### Temporarily Disable a Periodic Task Source: https://github.com/celery/django-celery-beat/blob/main/docs/includes/introduction.txt Use the 'enabled' flag to temporarily disable a periodic task. Remember to save the changes after modifying the flag. ```python periodic_task.enabled = False periodic_task.save() ``` -------------------------------- ### Update Changed Counter for Periodic Tasks Source: https://github.com/celery/django-celery-beat/blob/main/README.rst When updating periodic tasks in bulk, the change counter in the PeriodicTasks model needs to be manually incremented to signal the celery beat service to reload the schedule. ```Python >>> from django_celery_beat.models import PeriodicTasks >>> PeriodicTasks.update_changed() ``` -------------------------------- ### Resetting Last Run Time for Periodic Tasks Source: https://github.com/celery/django-celery-beat/blob/main/README.rst If the Django TIME_ZONE setting is changed, periodic task schedules may become desynchronized. This snippet resets the 'last run time' for all periodic tasks and updates the change counter to ensure the beat service reloads the schedule. ```Python >>> from django_celery_beat.models import PeriodicTask, PeriodicTasks >>> PeriodicTask.objects.all().update(last_run_at=None) >>> PeriodicTasks.update_changed() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.