crontab
A Crontab can be used as the run_every value of a periodic task entry to add :manpage:crontab(5)-like scheduling.
Attributes
| Attribute | Type | Description |
|---|---|---|
| minute | set = * | A (list of) integers from 0-59 or a string representing a Crontab pattern that represent the minutes of an hour of when execution should occur. |
| hour | set = * | A (list of) integers from 0-23 or a string representing a Crontab pattern that represent the hours of a day of when execution should occur. |
| day_of_week | set = * | A (list of) integers from 0-6 (Sunday=0) or a string representing a Crontab pattern that represent the days of a week that execution should occur. |
| day_of_month | set = * | A (list of) integers from 1-31 or a string representing a Crontab pattern that represents the days of the month that execution should occur. |
| month_of_year | set = * | A (list of) integers from 1-12 or a string representing a Crontab pattern that represents the months of the year during which execution can occur. |
| nowfun | callable | Function returning the current date and time (datetime.datetime). |
| app | [Celery](../app/base/celery.md?sid=celery_app_base_celery) | The Celery app instance. |
Constructor
Signature
def crontab(
minute: Cronspec = '*',
hour: Cronspec = '*',
day_of_week: Cronspec = '*',
day_of_month: Cronspec = '*',
month_of_year: Cronspec = '*',
**kwargs: Any
) - > None
Parameters
| Name | Type | Description |
|---|---|---|
| minute | Cronspec = '*' | Minute(s) of an hour when the task should run (0-59). |
| hour | Cronspec = '*' | Hour(s) of a day when the task should run (0-23). |
| day_of_week | Cronspec = '*' | Day(s) of the week when the task should run (0-6, Sunday=0). |
| day_of_month | Cronspec = '*' | Day(s) of the month when the task should run (1-31). |
| month_of_year | Cronspec = '*' | Month(s) of the year when the task should run (1-12). |
| **kwargs | Any | Additional keyword arguments passed to the BaseSchedule parent class. |
Methods
from_string()
@classmethod
def from_string(
crontab: str
) - > [crontab](crontab.md?sid=celery_schedules_crontab)
Create a Crontab from a cron expression string. For example crontab.from_string('* * * * *').
Parameters
| Name | Type | Description |
|---|---|---|
| crontab | str | A space-separated string containing five cron fields: minute, hour, day of month, month, and day of week |
Returns
| Type | Description |
|---|---|
[crontab](crontab.md?sid=celery_schedules_crontab) | A new crontab instance initialized with the parsed schedule components |
remaining_delta()
@classmethod
def remaining_delta(
last_run_at: datetime,
tz: tzinfo | None,
ffwd: type
) - > tuple[datetime, Any, datetime]
Calculates the time difference between the last run and the next scheduled execution based on the crontab rules.
Parameters
| Name | Type | Description |
|---|---|---|
| last_run_at | datetime | The timestamp when the task was last executed |
| tz | `tzinfo | None` |
| ffwd | type | The fast-forward utility class used to calculate time jumps |
Returns
| Type | Description |
|---|---|
tuple[datetime, Any, datetime] | A tuple containing the localized last run time, the fast-forward delta to the next run, and the localized current time |
remaining_estimate()
@classmethod
def remaining_estimate(
last_run_at: datetime,
ffwd: type
) - > timedelta
Estimate of next run time. Returns when the periodic task should run next as a :class:~datetime.timedelta.
Parameters
| Name | Type | Description |
|---|---|---|
| last_run_at | datetime | The timestamp of the last execution to calculate the next interval from |
| ffwd | type | The fast-forward utility class used for time calculations |
Returns
| Type | Description |
|---|---|
timedelta | The duration of time remaining until the next scheduled execution |
is_due()
@classmethod
def is_due(
last_run_at: datetime
) - > tuple[bool, datetime]
Return tuple of (is_due, next_time_to_run). If :setting:beat_cron_starting_deadline has been specified, the scheduler will make sure that the last_run_at time is within the deadline.
Parameters
| Name | Type | Description |
|---|---|---|
| last_run_at | datetime | The timestamp of the last successful execution to check against the schedule and deadline |
Returns
| Type | Description |
|---|---|
tuple[bool, datetime] | A tuple where the first element indicates if the task is ready to run, and the second is the time remaining until the next run |