Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for abstract base models improved. (#516) #534

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ coverage.xml
.python-version
venv
.env
.vscode/
48 changes: 48 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,54 @@ manually:
>>> from django_celery_beat.models import PeriodicTasks
>>> PeriodicTasks.update_changed()

Custom Models
=============

It's possible to use your own models instead of the default ones provided by ``django_celery_beat``, to do that just define your models inheriting from the right one from ``django_celery_beat.models.abstract``:

.. code-block:: Python

# custom_app.models.py
from django_celery_beat.models.abstract import (
AbstractClockedSchedule,
AbstractCrontabSchedule,
AbstractIntervalSchedule,
AbstractPeriodicTask,
AbstractPeriodicTasks,
AbstractSolarSchedule,
)

class CustomPeriodicTask(AbstractPeriodicTask):
...

class CustomPeriodicTasks(AbstractPeriodicTasks):
...

class CustomCrontabSchedule(AbstractCrontabSchedule):
...

class CustomIntervalSchedule(AbstractIntervalSchedule):
...

class CustomSolarSchedule(AbstractSolarSchedule):
...

class CustomClockedSchedule(AbstractClockedSchedule):
...

To let ``django_celery_beat.scheduler`` make use of your own modules, you must provide the ``app_name.model_name`` of your own custom models as values to the next constants in your settings:

.. code-block:: Python

# settings.py
# CELERY_BEAT_(?:PERIODICTASKS?|(?:CRONTAB|INTERVAL|SOLAR|CLOCKED)SCHEDULE)_MODEL = "app_label.model_name"
CELERY_BEAT_PERIODICTASK_MODEL = "custom_app.CustomPeriodicTask"
CELERY_BEAT_PERIODICTASKS_MODEL = "custom_app.CustomPeriodicTasks"
CELERY_BEAT_CRONTABSCHEDULE_MODEL = "custom_app.CustomCrontabSchedule"
CELERY_BEAT_INTERVALSCHEDULE_MODEL = "custom_app.CustomIntervalSchedule"
CELERY_BEAT_SOLARSCHEDULE_MODEL = "custom_app.CustomSolarSchedule"
CELERY_BEAT_CLOCKEDSCHEDULE_MODEL = "custom_app.CustomClockedSchedule"

Example creating interval-based periodic task
---------------------------------------------

Expand Down
133 changes: 133 additions & 0 deletions django_celery_beat/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

from .models import (
PeriodicTask, PeriodicTasks,
CrontabSchedule, IntervalSchedule,
SolarSchedule, ClockedSchedule
)

def crontabschedule_model():
"""Return the CrontabSchedule model that is active in this project."""
if not hasattr(settings, 'CELERY_BEAT_CRONTABSCHEDULE_MODEL'):
return CrontabSchedule

try:
return apps.get_model(
settings.CELERY_BEAT_CRONTABSCHEDULE_MODEL
)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_CRONTABSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_CRONTABSCHEDULE_MODEL}' that has not "
"been installed"
)

def intervalschedule_model():
"""Return the IntervalSchedule model that is active in this project."""
if not hasattr(settings, 'CELERY_BEAT_INTERVALSCHEDULE_MODEL'):
return IntervalSchedule

try:
return apps.get_model(
settings.CELERY_BEAT_INTERVALSCHEDULE_MODEL
)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_INTERVALSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_INTERVALSCHEDULE_MODEL}' that has not "
"been installed"
)

def periodictask_model():
"""Return the PeriodicTask model that is active in this project."""
if not hasattr(settings, 'CELERY_BEAT_PERIODICTASK_MODEL'):
return PeriodicTask

try:
return apps.get_model(settings.CELERY_BEAT_PERIODICTASK_MODEL)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASK_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASK_MODEL refers to model "
f"'{settings.CELERY_BEAT_PERIODICTASK_MODEL}' that has not been "
"installed"
)

def periodictasks_model():
"""Return the PeriodicTasks model that is active in this project."""
if not hasattr(settings, 'CELERY_BEAT_PERIODICTASKS_MODEL'):
return PeriodicTasks

try:
return apps.get_model(
settings.CELERY_BEAT_PERIODICTASKS_MODEL
)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASKS_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_PERIODICTASKS_MODEL refers to model "
f"'{settings.CELERY_BEAT_PERIODICTASKS_MODEL}' that has not been "
"installed"
)

def solarschedule_model():
"""Return the SolarSchedule model that is active in this project."""
if not hasattr(settings, 'CELERY_BEAT_SOLARSCHEDULE_MODEL'):
return SolarSchedule

try:
return apps.get_model(
settings.CELERY_BEAT_SOLARSCHEDULE_MODEL
)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_SOLARSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_SOLARSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_SOLARSCHEDULE_MODEL}' that has not been "
"installed"
)

def clockedschedule_model():
"""Return the ClockedSchedule model that is active in this project."""
if not hasattr(settings, 'CELERY_BEAT_CLOCKEDSCHEDULE_MODEL'):
return ClockedSchedule

try:
return apps.get_model(
settings.CELERY_BEAT_CLOCKEDSCHEDULE_MODEL
)
except ValueError:
raise ImproperlyConfigured(
"CELERY_BEAT_CLOCKEDSCHEDULE_MODEL must be of the form "
"'app_label.model_name'"
)
except LookupError:
raise ImproperlyConfigured(
"CELERY_BEAT_CLOCKEDSCHEDULE_MODEL refers to model "
f"'{settings.CELERY_BEAT_CLOCKEDSCHEDULE_MODEL}' that has not "
"been installed"
)
21 changes: 21 additions & 0 deletions django_celery_beat/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .abstract import crontab_schedule_celery_timezone
from .generic import (
ClockedSchedule,
ClockScheduler,
CrontabSchedule,
IntervalSchedule,
PeriodicTask,
PeriodicTasks,
SolarSchedule,
)

__ALL__ = [
"ClockedSchedule",
"ClockScheduler",
"CrontabSchedule",
"IntervalSchedule",
"PeriodicTask",
"PeriodicTasks",
"SolarSchedule",
"crontab_schedule_celery_timezone",
]
Loading