Welcome to Django Serverless Cronās documentation!¶
Contents:
django-serverless-cron 𦔶
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:
https://cloud.google.com/scheduler -> Great feature set, easy to use, reasonable free tier & very cheap.
https://azure.microsoft.com/en-gb/services/logic-apps formerly https://docs.microsoft.com/en-us/azure/scheduler/scheduler-intro
https://cron-job.org/en/ -> Absolutely free and open-source: https://github.com/pschlan/cron-job.org
https://cronless.com -> Has 30 Second Cron Jobs
https://github.com/features/actions; https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onschedule -> eg making a HTTP request using curl in a step
Credits¶
Tools used in rendering this package:
Contributing¶
Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.
You can contribute in many ways:
Types of Contributions¶
Report Bugs¶
Report bugs at https://github.com/paulonteri/django-serverless-cron/issues.
If you are reporting a bug, please include:
Your operating system name and version.
Any details about your local setup that might be helpful in troubleshooting.
Detailed steps to reproduce the bug.
Fix Bugs¶
Look through the GitHub issues for bugs. Anything tagged with ābugā is open to whoever wants to implement it.
Implement Features¶
Look through the GitHub issues for features. Anything tagged with āfeatureā is open to whoever wants to implement it.
Write Documentation¶
Django Serverless Cron could always use more documentation, whether as part of the official Django Serverless Cron docs, in docstrings, or even on the web in blog posts, articles, and such.
Submit Feedback¶
The best way to send feedback is to file an issue at https://github.com/paulonteri/django-serverless-cron/issues.
If you are proposing a feature:
Explain in detail how it would work.
Keep the scope as narrow as possible, to make it easier to implement.
Remember that this is a volunteer-driven project, and that contributions are welcome :)
Get Started!¶
Ready to contribute? Hereās how to set up django-serverless-cron for local development.
Fork the django-serverless-cron repo on GitHub.
Clone your fork locally:
$ git clone git@github.com:your_name_here/django-serverless-cron.git
Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:
$ mkvirtualenv django-serverless-cron $ cd django-serverless-cron/ $ python setup.py develop
Create a branch for local development:
$ git checkout -b name-of-your-bugfix-or-feature
Now you can make your changes locally.
When youāre done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:
$ flake8 django_serverless_cron tests $ python setup.py test $ tox
To get flake8 and tox, just pip install them into your virtualenv.
Commit your changes and push your branch to GitHub:
$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature
Submit a pull request through the GitHub website.
Pull Request Guidelines¶
Before you submit a pull request, check that it meets these guidelines:
The pull request should include tests.
If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
The pull request should work for Python 2.6, 2.7, and 3.3, and for PyPy. Check https://travis-ci.org/paulonteri/django-serverless-cron/pull_requests and make sure that the tests pass for all supported Python versions.
Tips¶
To run a subset of tests:
$ python -m unittest tests.test_django_serverless_cron
Tests¶
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install tox
(myenv) $ tox
Development commands¶
pip install -r requirements_dev.txt
invoke -l
Credits¶
Development Lead¶
Paul Onteri <https://paulonteri.com>
Contributors¶
None yet. Why not be the first?
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