Skip to content

Latest commit

 

History

History
182 lines (112 loc) · 5.34 KB

schedules.rst

File metadata and controls

182 lines (112 loc) · 5.34 KB

Schedules

Schedule

Schedules are regular Django models. You can manage them through the admin_page 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)

# Run a schedule every 5 minutes, starting at 6.
import arrow

schedule('math.hypot',
        3, 4,
        schedule_type=Schedule.MINUTES,
        minutes = 5,
        next_run = arrow.utcnow().replace(hour=18, minute=0))

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

A database model for task schedules.

Primary key

A name for your schedule. Tasks created by this schedule will assume this or the primary key as their group id.

The function to be scheduled

Optional hook function to be called after execution.

Positional arguments for the function.

Keyword arguments for the function

The type of schedule. Follows Schedule.TYPE

ONCE, MINUTES, HOURLY, DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY

The number of minutes the MINUTES schedule should use. Is ignored for other schedule types.

Number of times to repeat the schedule. -1=Always, 0=Never, n =n. When set to -1, this will keep counting down.

Datetime of the next scheduled execution.

Id of the last task generated by this schedule.

Admin link to the last executed task.

Returns the success status of the last executed task.

'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, set repeats to a positive number.

'I' will run every minutes after its first run.

'H' the scheduled task will run every hour after its first run.

'D' the scheduled task will run every day at the time of its first run.

'W' the task will run every week on they day and time of the first run.

'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.

'Q' this task runs once every 3 months on the day and time of the last run.

'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.