django-serverless-cron 🦡 A Django library with a simpler approach running cron jobs in a serverless environment through HTTP requests. This allows you to run any task without having to manage always-on infrastructure. https://pypi.org/project/django-serverless-cron
MIT License
.. image:: https://badge.fury.io/py/django-serverless-cron.svg :target: https://badge.fury.io/py/django-serverless-cron
.. image:: https://github.com/paulonteri/django-serverless-cron/actions/workflows/tests.yml/badge.svg :target: https://github.com/paulonteri/django-serverless-cron/actions/workflows/tests.yml
.. image:: https://codecov.io/gh/paulonteri/django-serverless-cron/branch/master/graph/badge.svg :target: https://codecov.io/gh/paulonteri/django-serverless-cron
.. image:: https://readthedocs.org/projects/django-serverless-cron/badge/?version=latest :target: http://django-serverless-cron.readthedocs.io/?badge=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.
This is essentially a replacement/supplement for a traditional OS 'cron' or 'job scheduler' system:
Google Cloud Scheduler
and Amazon EventBridge
.Documentation is graciously hosted at https://django-serverless-cron.readthedocs.io.
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. 🙂
Installation ^^^^^^^^^^^^
Install Django Serverless Cron::
pip install django-serverless-cron
Settings ^^^^^^^^
Add it to your INSTALLED_APPS
:
.. code-block:: python
INSTALLED_APPS = (
# ...
'django_serverless_cron'
# ...
)
Add jobs to your settings file:
.. code-block:: python
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', "is_bulk": True}
),
(
'1_hours',
'mail.jobs.send_mail_function',
{} # job without kwargs
),
]
URL patterns ^^^^^^^^^^^^ Add the jobs to your URL patterns:
.. code-block:: python
from django.urls import path, re_path, include #add re_path and include
from django_serverless_cron import urls as django_serverless_cron_urls
urlpatterns = [
# ...
re_path(r'^', include(django_serverless_cron_urls)),
#...
]
Migrate ^^^^^^^
.. code-block:: bash
python manage.py migrate
In Development ^^^^^^^^^^^^^^
Running Jobs through HTTP requests """"""""""""""""""""""""""""""""""
Call the /run
path to run all jobs:
Example:
.. code-block:: bash
curl http://localhost:8000/run
or
.. code-block:: python
import requests
x = requests.get('http://localhost:8000/run')
Running Jobs through the management command """""""""""""""""""""""""""""""""""""""""""
This will run the jobs every 30 seconds:
.. code-block:: bash
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:
curl
in a stepFor more learning check out:
Tools used in rendering this package:
cookiecutter-djangopackage
_.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _cookiecutter-djangopackage
: https://github.com/pydanny/cookiecutter-djangopackage