Schedules¶
Schedule¶
Schedules are regular Django models.
You can manage them through the Admin pages or directly from your code with the schedule()
function or the Schedule
model:
from django_q import Schedule, schedule
# Use the schedule wrapper
schedule('math.copysign',
2, -2,
hook='hooks.print_result',
schedule_type=Schedule.DAILY)
# Or create the object directly
Schedule.objects.create(func='math.copysign',
hook='hooks.print_result',
args='2,-2',
schedule_type=Schedule.DAILY
)
# In case you want to use async options
schedule('math.sqrt',
9,
hook='hooks.print_result',
q_options={'timeout': 30},
schedule_type=Schedule.HOURLY)
Management Commands¶
If you want to schedule regular Django management commands, you can use the django.core.management
module to make a wrapper function which you can schedule in Django Q:
# tasks.py
from django.core import management
# wrapping `manage.py clearsessions`
def clear_sessions_command():
return management.call_command('clearsessions')
# now you can schedule it to run every hour
from django_q import schedule
schedule('tasks.clear_sessions_command', schedule_type='H')
Reference¶
-
schedule
(func, *args, name=None, hook=None, schedule_type='O', repeats=-1, next_run=now(), q_options=None, **kwargs)¶ Creates a schedule
Parameters: - func (str) – the function to schedule. Dotted strings only.
- args – arguments for the scheduled function.
- name (str) – An optional name for your schedule.
- hook (str) – optional result hook function. Dotted strings only.
- schedule_type (str) – (O)nce, (H)ourly, (D)aily, (W)eekly, (M)onthly, (Q)uarterly, (Y)early or
Schedule.TYPE
- repeats (int) – Number of times to repeat schedule. -1=Always, 0=Never, n =n.
- next_run (datetime) – Next or first scheduled execution datetime.
- q_options (dict) – async options to use for this schedule
- kwargs – optional keyword arguments for the scheduled function.
-
class
Schedule
¶ A database model for task schedules.
-
id
¶
Primary key
-
name
¶
A name for your schedule. Tasks created by this schedule will assume this or the primary key as their group id.
-
func
¶
The function to be scheduled
-
hook
¶
Optional hook function to be called after execution.
-
args
¶
Positional arguments for the function.
-
kwargs
¶
Keyword arguments for the function
-
schedule_type
¶
The type of schedule. Follows
Schedule.TYPE
-
TYPE
¶
ONCE
,HOURLY
,DAILY
,WEEKLY
,MONTHLY
,QUARTERLY
,YEARLY
-
repeats
¶
Number of times to repeat the schedule. -1=Always, 0=Never, n =n. When set to -1, this will keep counting down.
-
next_run
¶
Datetime of the next scheduled execution.
-
task
¶
Id of the last task generated by this schedule.
-
last_run
()¶
Admin link to the last executed task.
-
success
()¶
Returns the success status of the last executed task.
-
ONCE
¶
‘O’ the schedule will only run once. If it has a negative
repeats
it will be deleted after it has run. If you want to keep the result, setrepeats
to a positive number.-
HOURLY
¶
‘H’ the scheduled task will run every hour after its first run.
-
DAILY
¶
‘D’ the scheduled task will run every day at the time of its first run.
-
WEEKLY
¶
‘W’ the task will run every week on they day and time of the first run.
-
MONTHLY
¶
‘M’ the tasks runs every month on they day and time of the last run.
Note
Months are tricky. If you schedule something on the 31st of the month and the next month has only 30 days or less, the task will run on the last day of the next month. It will however continue to run on that day, e.g. the 28th, in subsequent months.
-
QUARTERLY
¶
‘Q’ this task runs once every 3 months on the day and time of the last run.
-
YEARLY
¶
‘Y’ only runs once a year. The same caution as with months apply; If you set this to february 29th, it will run on february 28th in the following years.
-