Skip to main content

Running python crons in a Django 5 project

Project description

django-cron

PyPI version Python Versions Django Versions

A Django app for running scheduled tasks (cron jobs) within your Django project. This is a fork of Tivix/django-cron with full Django 5 compatibility. Used by Ticketping in production.

Features

  • Simple API: Define cron jobs with a clean, Pythonic interface
  • Django 5 Compatible: Fully tested and working with Django 5.x
  • Flexible Scheduling: Support for various schedule types (daily, hourly, custom intervals)
  • Multiple Locking Backends: File, cache, and database-based locking to prevent concurrent runs
  • Job Logging: Track job execution history and debug issues easily
  • No System Cron Required: Jobs run within your Django process

Installation

Install from PyPI:

pip install django-cron-django5

Add django_cron to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    'django_cron',
]

Run migrations:

python manage.py migrate django_cron

Quick Start

Create a cron job by subclassing CronJobBase:

from django_cron import CronJobBase, Schedule

class MyCronJob(CronJobBase):
    RUN_EVERY_MINS = 60  # Run every hour

    schedule = Schedule(run_every_mins=RUN_EVERY_MINS)
    code = 'myapp.my_cron_job'  # Unique code

    def do(self):
        # Your code here
        pass

Register your cron job in settings:

CRON_CLASSES = [
    'myapp.cron.MyCronJob',
]

Run cron jobs via crontab or similar. This is the most simplest and efficient way.

python manage.py runcrons

If you'd prefer to run via systemd or similar, use cronloop.

python manage.py cronloop

Here's an example service file

[Unit]
Description=Django Cron Loop Service
After=network.target postgresql.service

[Service]
Type=simple
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/loc-backend
Environment="PATH=/home/ubuntu/.pyenv/versions/locenv/bin:/usr/local/bin:/usr/bin:/bin"
Environment="PYTHONUNBUFFERED=1"

# THE KEY CHANGES - Add these:

# Limit total iterations, then restart (prevents memory leaks)
# Runs ~50 times (50 * 2 min = 100 minutes) then restarts
ExecStart=/home/ubuntu/.pyenv/versions/locenv/bin/python manage.py cronloop --sleep 120

# Set memory limits (adjust based on your server)
MemoryMax=512M
MemoryHigh=400M

# Always restart when process exits
Restart=always
RestartSec=10

# Restart every 2 hours regardless (prevents long-term memory leaks)
RuntimeMaxSec=7200

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=django-cronloop

# Security
NoNewPrivileges=true
PrivateTmp=yes

[Install]
WantedBy=multi-user.target

Requirements

  • Python >= 3.9
  • Django >= 4.2, < 6.0

Documentation

For detailed documentation, please visit the GitHub repository.

Contributing

We welcome contributions! Please see our GitHub repository for more information.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Credits


Development Setup

Pre-requisites

  • Python >= 3.9

We suggest using pyenv to easily manage python versions.

Configure local development setup

  1. Install and activate python:
pyenv install 3.11.13
pyenv virtualenv 3.11.13 djcron
pyenv local djcron
  1. Install project requirements:
pip install -r requirements-dev.txt
  1. Install pre-commit hooks (if pre-commit is installed globally):
pre-commit install

Running Tests

python testmanage.py test django_cron

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_cron_django5-0.6.2.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

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

django_cron_django5-0.6.2-py2.py3-none-any.whl (67.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_cron_django5-0.6.2.tar.gz.

File metadata

  • Download URL: django_cron_django5-0.6.2.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for django_cron_django5-0.6.2.tar.gz
Algorithm Hash digest
SHA256 7eb5fe6fc8ef340900b4e669e5670ab7675b2d1f75ed16b87e78e35bbbcc9ccd
MD5 7675b3b5ca862b1e2d9db5da20b4976c
BLAKE2b-256 bebab776466303c641efca5102db70a52699f5ec3d0114d9db7e56c974a13bca

See more details on using hashes here.

File details

Details for the file django_cron_django5-0.6.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_cron_django5-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9e2d988ed257db1312ff0a99cbee3d9ccac4ba897a9b8f74f92f71da0e8214ae
MD5 47dad63881bf2e46edc1a14c78475541
BLAKE2b-256 5ff762b35afd398550df947434338dab441b09bad9730c27287578181b009c00

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