Skip to main content

Manage pgcron jobs from Django

Project description

django-pgcron

django-pgcron is a Django library that enables you to manage scheduled cron jobs through Django's interface. It integrates with pg_cron, a Postgres extension that lets you schedule and automate database queries to run directly within PostgreSQL on a specified schedule, eliminating the need for Celery or another application-level job queuing library for simple database tasks.

Quick Start

Install django-pgcron and add pgcron to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    "pgcron",
]

Jobs are intended to be scheduled in a jobs.py submodule of your app.

my_django_project/
│
├── my_app/
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── jobs.py <--- Add your jobs here
│   ├── migrations/
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── views.py
│   └── ...

Defining your jobs is as simple as decorating a function returning with @pgcron.job. Three types of jobs are supported: pgcron.Update, pgcron.Delete, and pgcron.SQLExpression.

Update

A pgcron.Update is an update statement on an ORM query.

import pgcron

@pgcron.job("0 0 1 1 *")
def my_job():
    return pgcron.Update(NameTestModel.objects.all().filter(name="test"), name="test2")

Delete

A pgcron.Delete is a delete statement on an ORM query.

import pgcron

@pgcron.job("0 0 1 1 *")
def my_job():
    return pgcron.Delete(NameTestModel.objects.all().filter(name="test"))

SQL Expressions

A pgcron.SQLExpression is a simple SQL expression to be executed by pgcron.

import pgcron

@pgcron.job("0 0 1 1 *")
def my_job():
    return pgcron.SQLExpression("INSERT INTO my_table (name) VALUES ('test');")

Syncing Jobs

Once you've defined your jobs, you can sync them to the database with the pgcron sync command.

python manage.py pgcron sync

This will register all of your current jobs, and drop any jobs that are no longer defined in your application. It's recommended to run this command as part of your application's deployment process alongside migrate.

Installation

Install django-pgcron with:

pip install django-pgcron

Installing pg_cron

In order to use django-pgcron, you must have pg_cron installed in your database.

For instructions on installing pg_cron, see the pg_cron documentation.

Compatibility

django-pgcron is compatible with Python 3.10 - 3.13, Django 5.0+, and Postgres 13 - 17.

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_pgcron-0.1.0.tar.gz (21.5 kB view details)

Uploaded Source

Built Distribution

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

django_pgcron-0.1.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file django_pgcron-0.1.0.tar.gz.

File metadata

  • Download URL: django_pgcron-0.1.0.tar.gz
  • Upload date:
  • Size: 21.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.12

File hashes

Hashes for django_pgcron-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e750e666ce713c9db389463be64574490839fe0d64b097a5a5e27c421835b833
MD5 74a4ef9ba0443d7f3af0c1ac76c25508
BLAKE2b-256 7e3b618f4a91a19ccfe85d22558d66a955b787943211435ad0aa47581391fa68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_pgcron-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccfb887713bdec27dd077e11dd00ff070fe29c653b26e0f6c2dc1b0e0d47836c
MD5 5f32c9576f8c93ad571cfbdfcc00a405
BLAKE2b-256 d39e4591285ddfd972c6fc5a4296eb629cf4eb314904918340d211aa0f4d79d1

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