Skip to main content

APScheduler Register for Django

Project description

Django APScheduler Register

PyPI PyPI - Python Version PyPI - Django Version GitHub Workflow Status Codecov Code style:black

APScheduler register for Django.

Installation

pip install django-aps-register

Quick start

  • Add django_apscheduler to your INSTALLED_APPS setting like this:
INSTALLED_APPS = (
    # ...
    "django_apscheduler",
)
  • django-apscheduler comes with sensible configuration defaults out of the box. The defaults can be overridden by adding the following settings to your Django settings.py file:
# Format string for displaying run time timestamps in the Django admin site. The default
# just adds seconds to the standard Django format, which is useful for displaying the timestamps
# for jobs that are scheduled to run on intervals of less than one minute.
# 
# See https://docs.djangoproject.com/en/dev/ref/settings/#datetime-format for format string
# syntax details.
APSCHEDULER_DATETIME_FORMAT = "N j, Y, f:s a"

# Maximum run time allowed for jobs that are triggered manually via the Django admin site, which
# prevents admin site HTTP requests from timing out.
# 
# Longer running jobs should probably be handed over to a background task processing library
# that supports multiple background worker processes instead (e.g. Dramatiq, Celery, Django-RQ,
# etc. See: https://djangopackages.org/grids/g/workers-queues-tasks/ for popular options).
APSCHEDULER_RUN_NOW_TIMEOUT = 25  # Seconds
  • Run python manage.py migrate to create the django_apscheduler models.

  • Add a custom Django management command to your project that schedules the APScheduler jobs and starts the scheduler:

# runapscheduler.py
import logging

from django.conf import settings

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from django.core.management.base import BaseCommand
from django_apscheduler.jobstores import DjangoJobStore
from django_apscheduler.models import DjangoJobExecution
from django_apscheduler import util

logger = logging.getLogger(__name__)


def my_job():
  # Your job processing logic here...
  pass


# The `close_old_connections` decorator ensures that database connections, that have become
# unusable or are obsolete, are closed before and after your job has run. You should use it
# to wrap any jobs that you schedule that access the Django database in any way. 
@util.close_old_connections
def delete_old_job_executions(max_age=604_800):
  """
  This job deletes APScheduler job execution entries older than `max_age` from the database.
  It helps to prevent the database from filling up with old historical records that are no
  longer useful.
  
  :param max_age: The maximum length of time to retain historical job execution records.
                  Defaults to 7 days.
  """
  DjangoJobExecution.objects.delete_old_job_executions(max_age)


class Command(BaseCommand):
  help = "Runs APScheduler."

  def handle(self, *args, **options):
    scheduler = BlockingScheduler(timezone=settings.TIME_ZONE)
    scheduler.add_jobstore(DjangoJobStore(), "default")

    scheduler.add_job(
      my_job,
      trigger=CronTrigger(second="*/10"),  # Every 10 seconds
      id="my_job",  # The `id` assigned to each job MUST be unique
      max_instances=1,
      replace_existing=True,
    )
    logger.info("Added job 'my_job'.")

    scheduler.add_job(
      delete_old_job_executions,
      trigger=CronTrigger(
        day_of_week="mon", hour="00", minute="00"
      ),  # Midnight on Monday, before start of the next work week.
      id="delete_old_job_executions",
      max_instances=1,
      replace_existing=True,
    )
    logger.info(
      "Added weekly job: 'delete_old_job_executions'."
    )

    try:
      logger.info("Starting scheduler...")
      scheduler.start()
    except KeyboardInterrupt:
      logger.info("Stopping scheduler...")
      scheduler.shutdown()
      logger.info("Scheduler shut down successfully!")
  • The management command defined above should be invoked via ./manage.py runapscheduler whenever the webserver serving your Django application is started. The details of how and where this should be done is implementation specific, and depends on which webserver you are using and how you are deploying your application to production. For most people this should involve configuring a supervisor process of sorts.

  • Register any APScheduler jobs as you would normally. Note that if you haven't set DjangoJobStore as the 'default' job store, then you will need to include jobstore='djangojobstore' in your scheduler.add_job() calls.

Advanced usage

django-apscheduler assumes that you are already familiar with APScheduler and its proper use. If not, then please head over to the project page and have a look through the APScheduler documentation.

It is possible to make use of different types of schedulers depending on your environment and use case. If you would prefer running a BackgroundScheduler instead of using a BlockingScheduler, then you should be aware that using APScheduler with uWSGI requires some additional configuration steps in order to re-enable threading support.

Supported databases

Please take note of the list of databases that are officially supported by Django. django-apscheduler probably won't work with unsupported databases like Microsoft SQL Server, MongoDB, and the like.

Database connections and timeouts

django-apscheduler is dependent on the standard Django database configuration settings. These settings, in combination with how your database server has been configured, determine how connection management will be performed for your specific deployment.

The close_old_connections decorator should be applied to APScheduler jobs that require database access. Doing so ensures that Django's CONN_MAX_AGE configuration setting is enforced before and after your job is run. This mirrors the standard Django functionality of doing the same before and after handling each HTTP request.

If you still encounter any kind of 'lost database connection' errors then it probably means that:

  • Your database connections timed out in the middle of executing a job. You should probably consider incorporating a connection pooler as part of your deployment for more robust database connection management (e.g. pgbouncer for PostgreSQL, or the equivalent for other DB platforms).
  • Your database server has crashed / been restarted. Django will not reconnect automatically and you need to re-start django-apscheduler as well.

Common footguns

Unless you have a very specific set of requirements, and have intimate knowledge of the inner workings of APScheduler, you really shouldn't be using BackgroundScheduler. Doing so can lead to all sorts of temptations like:

  • Firing up a scheduler inside of a Django view: this will most likely cause more than one scheduler to run concurrently and lead to jobs running multiple times (see the above introduction to this README for a more thorough treatment of the subject).
  • Bootstrapping a scheduler somewhere else inside your Django application: it feels like this should solve the problem mentioned above and guarantee that only one scheduler is running. The downside is that you have just delegated the management of all of your background task processing threads to whatever webserver you are using (Gunicorn, uWSGI, etc.). The webserver will probably kill any long-running threads (your jobs) with extreme prejudice (thinking that they are caused by misbehaving HTTP requests).

Relying on BlockingScheduler forces you to run APScheduler in its own dedicated process that is not handled or monitored by the webserver. The example code provided in runapscheduler.py above is a good starting point.

Project resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-aps-register-0.1.0.tar.gz (466.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_aps_register-0.1.0-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file django-aps-register-0.1.0.tar.gz.

File metadata

  • Download URL: django-aps-register-0.1.0.tar.gz
  • Upload date:
  • Size: 466.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.7

File hashes

Hashes for django-aps-register-0.1.0.tar.gz
Algorithm Hash digest
SHA256 30c4ce38d6dedb8d2a4002e1609fd11c8725bc249870b376b3faab16631e221c
MD5 5dff7c4e8855d778f62360c343d27159
BLAKE2b-256 d970844e3ac807b3a172b432068c7e2be4245ce6567a85c2e7fb76f1a7ae3d45

See more details on using hashes here.

File details

Details for the file django_aps_register-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_aps_register-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c32f582f8a5b6df65e7331f8edf31aa3db4ff577059384476d11d414de60add3
MD5 d89c74e1144bd8a1dd381182a5c595d8
BLAKE2b-256 0f159f161266d9bef2556c1d833afe036a8b55706446741f7fbf38e6076ab9a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page