Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Bypass de default models used by django_celery_beat.scheduler (#516)
when
`CELERY_BEAT_(?:PERIODICTASKS?|(?:CRONTAB|INTERVAL|SOLAR|CLOCKED)SCHEDULE)_MODEL`
constants are defined in django `settings`.

Providing the `app_label.model_name` of your own models as value for the
constants
`CELERY_BEAT_(?:PERIODICTASKS?|(?:CRONTAB|INTERVAL|SOLAR|CLOCKED)SCHEDULE)_MODEL`
will let `django_celery_beat.scheduler` use the custom models instead of
the default ones (aka generic models, in this context/pull-request):

```python
CELERY_BEAT_PERIODICTASK_MODEL = "app_label.model_name"
CELERY_BEAT_PERIODICTASKS_MODEL = "app_label.model_name"
CELERY_BEAT_CRONTABSCHEDULE_MODEL = "app_label.model_name"
CELERY_BEAT_INTERVALSCHEDULE_MODEL = "app_label.model_name"
CELERY_BEAT_SOLARSCHEDULE_MODEL = "app_label.model_name"
CELERY_BEAT_CLOCKEDSCHEDULE_MODEL = "app_label.model_name"
```

Doing this we add support to automatically bypass the default
`django_celery_beat` models without forcing developers to overwrite the
whole `django_celery_beat.scheduler` in projects where the default
models doesn't fit the requirements

I updated the `README.rst` with a small explanation about how this work

Additonal information:
* related issue: #516
* pull-request: #534
  • Loading branch information
diegocastrum committed Jun 3, 2022
commit 742ee5472e5b8020662a71664f43e78524d88192
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"
)
20 changes: 15 additions & 5 deletions django_celery_beat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
from django.db.utils import DatabaseError, InterfaceError
from django.core.exceptions import ObjectDoesNotExist

from .models import (
PeriodicTask, PeriodicTasks,
CrontabSchedule, IntervalSchedule,
SolarSchedule, ClockedSchedule
)
from .clockedschedule import clocked
from .helpers import (
clockedschedule_model,
crontabschedule_model,
intervalschedule_model,
periodictask_model,
periodictasks_model,
solarschedule_model,
)
from .utils import NEVER_CHECK_TIMEOUT

# This scheduler must wake up more frequently than the
Expand All @@ -39,6 +42,13 @@
logger = get_logger(__name__)
debug, info, warning = logger.debug, logger.info, logger.warning

ClockedSchedule = clockedschedule_model()
CrontabSchedule = crontabschedule_model()
IntervalSchedule = intervalschedule_model()
PeriodicTask = periodictask_model()
PeriodicTasks = periodictasks_model()
SolarSchedule = solarschedule_model()


class ModelEntry(ScheduleEntry):
"""Scheduler entry taken from database row."""
Expand Down