Skip to content
Tejas Jadhav edited this page May 7, 2017 · 3 revisions

Schedule a task

TaskScheduler.schedule(func, description, args=None, kwargs=None, rrule_string=None, trigger_at=None, until=None)

Parameter Type Description
func function Required. Task function.
description str Required. Description of the task. This may get deprecated in future.
args list or tuple Optional. Default: None. Arguments to be passed to func. All values should be JSON serializable.
kwargs dict Optional. Default: None. Keyword arguments to be passed to func. All values should be JSON serializable.
rrule_string str Optional. Default: None. Recurrence rule. func will be executed as per the pattern defined by this rule. If not specified, func will be executed only once.
trigger_at datetime with tzinfo Optional. Default: None. Sets the datetime for the first run of the task. If not specified, it defaults to datetime.now().
until datetime with tzinfo Optional. Default: None. Sets the end time for the task. The task will never execute after this datetime. If not specified, task will run as long as it is defined in the recurrence rule.

NOTE: It is important that all the datetime instances passed should have timezone information, i. e., they should be timezone aware. Use Django's timezone.now() utility to get a timezone aware instance with USE_TZ=True in settings.

Returns

Type Description
UUID Scheduled Task ID.

Example

from datetime import timedelta
from django.utils import timezone
from scheduler.scheduler import TaskScheduler

def say_hello(*args, **kwargs):
    print(f'[{timezone.now().isoformat()}] Just saying hello', args, kwargs)

TaskScheduler.schedule(
    func=say_hello,
    description='Prints hello with timestamp and arguments', 
    args=(1, 2, 3),
    kwargs={'a': 'Apple', 'b': 'Banana'}, 
    rrule_string='RRULE:FREQ=HOURLY;INTERVAL=1',
    trigger_at=now() + timedelta(minutes=10),
    until=now() + timedelta(hours=10))

Output

UUID('4bc150f4-1427-4aee-8c13-fbce13d4e796')

Celery Output

[2017-05-07T20:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-07T21:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-07T22:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-07T23:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-08T00:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-08T00:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-08T01:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-08T02:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-08T03:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}
[2017-05-08T04:30:00] Just saying hello (1, 2, 3) {'a': 'Apple', 'b': 'Banana'}

Cancel scheduled task

TaskSchedule.cancel(task_id)

Parameter Type Description
scheduled_task_id UUID Required. Scheduled Task ID.

Returns

Type Description
ScheduledTask Scheduled task model instance. This may get deprecated in future.
Clone this wiki locally