Skip to main content

Django Serverless Cron 🦡

Project description

https://badge.fury.io/py/django-serverless-cron.svg https://github.com/paulonteri/django-serverless-cron/actions/workflows/tests.yml/badge.svg https://codecov.io/gh/paulonteri/django-serverless-cron/branch/master/graph/badge.svg https://readthedocs.org/projects/django-serverless-cron/badge/?version=latest

django-serverless-cron is a Django app with a simpler approach running cron jobs. This is done through exposing a HTTP endpoint to invoke the jobs that allows you to run any task without having to manage always-on infrastructure.

There is also an option to run jobs via management commands and the Django admin.

Why?

This is essentially a replacement/supplement for a traditional OS ‘cron’ or ‘job scheduler’ system:

  • Serverless cron jobs no-longer a pain.

  • Schedule jobs to run at a frequency that is less than 1 min. (crontab is limited to 1 min)

  • The machine running crontab is no longer a single point of failure.

  • The problem with the above systems is that they are often configured at the operating system level, which means their configuration is probably not easily ‘portable’ and ‘debug-able’ (if you are developing on Windows, the scheduler works differently from Linux or Unix). Also can not easily be integrated into a development environment.

  • Manually triggered cron jobs. Eg: via the Django Admin.

  • Alternative to cron services that aren’t always available on free (and sometimes paid) web hosting services.

  • Easier access to cron job execution logs and monitoring execution failures.

  • No need to learn crontab. Think of it as a friendlier alternative to traditional cron jobs. Simple cron job creation. No need for cron syntax, no guessing on job frequency. Easy controls.

Documentation

Documentation is graciously hosted at https://django-serverless-cron.readthedocs.io.

Contributions

Feel free to make pull requests and submit issues/requests. Find more detailed instructions under the contributing section.

Alternatively, you can leave a star on the repo to show your support. 🙂

Quickstart

Installation

Install Django Serverless Cron:

pip install django-serverless-cron

Settings

Add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'django_serverless_cron'
    # ...
)

Add jobs to your settings file:

SERVERLESS_CRONJOBS = [
    # (
    #   '1_hours',                       # frequency (seconds, minutes, hours, days, weeks) -> in this case, every one hour
    #   'mail.jobs.send_mail_function',  # path to task/function functions -> in this case, send_mail_function()
    #   {'kwarg1': 'foo'}                # kwargs passed to the function
    # ),
    (
        '1_days',
        'your_app.services.your_job_function',
        {'kwarg1': 'foo', 'kwarg2': 'bar'}
    ),
    (
        '1_hours',
        'mail.jobs.send_mail_function',
        {"is_bulk": True}
    ),
]

URL patterns

Add the jobs to your URL patterns:

from django_serverless_cron import urls as django_serverless_cron_urls


urlpatterns = [
    # ...
    url(r'^', include(django_serverless_cron_urls))
    #...
]

Migrate

python manage.py migrate

Running Jobs

In Development

Running Jobs through HTTP requests

Call the /run path to run all jobs:

Example:

curl http://localhost:8000/run

or

import requests

x = requests.get('http://localhost:8000/run')

Running Jobs through the management command

This will run the jobs every 30 seconds:

python manage.py serverless_cron_run

You can alternatively add the –single_run=’True’ option to run the jobs just once.

In Production

Similar to in development, we can call the /run path via fully managed services which are usually ridiculously cheap. Examples:

Credits

Tools used in rendering this package:

History

0.1.3 (2022-01-02)

  • fix bug in running the management command

  • wrap run_all_jobs() in a class

0.1.2 (2022-01-01)

  • Fix typo in the docs

0.1.1 (2022-01-01)

Birth: First release on PyPI.

Has the ability to:

  • Run jobs via the /run path

  • Run jobs via the management command serverless_cron_run.py

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

django_serverless_cron-0.1.3-py2.py3-none-any.whl (12.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_serverless_cron-0.1.3-py2.py3-none-any.whl.

File metadata

  • Download URL: django_serverless_cron-0.1.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10

File hashes

Hashes for django_serverless_cron-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d874e85b4458ccc042b8c3be6700c723a58c2c5293d20b765f17fbc5ae8b52fb
MD5 241198928a9b902e889957907eb2cf9a
BLAKE2b-256 e5c35308d070ef3646b55fe98e856bae0144af2a3d1ffefdf760a33600382f99

See more details on using hashes here.

Supported by

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