wiki:DeveloperGuidelines/Scheduler

Version 4 (modified by James O'Neill, 12 years ago) ( diff )

Fix kwarg name

Schduler

The scheduler is used for asynchronous tasks (not from a user request to the web server). It is used for daily instance maintenance and for processing and sending messages.

The Eden scheduler is a wrapper to the web2py scheduler.

New Task

To create a new function in /models/tasks.py for your new task, and point to it by setting a value in the tasks dictionary, e.g.,

def my_task(*args, **kwargs):
        return result

tasks["my_task"] = my_task

Recurring Tasks

Adding a schedule for a recurring task can be done by using s3task.schedule_task() in /models/zzz_1st_run.py.

s3task.schedule_task(
        "my_task",
        args=[
                "foo",
        ],
        vars={
                "bar": "cha",
        },
        period=300, # every 5 minutes
        repeats=0 # repeat forever
)

Note that recurring tasks don't normally use credentials.

Asynchronous

The scheduler can be used to 'push work async' so that the browser page returns to the user, whilst the task then runs in the background.

Tasks run in this way normally run as the user who initiated the task.

e.g.:

def stats_data_onapprove(row):
    """
       When a stats_data record is approved then the related stats_aggregate
       fields need to be updated so that the results are kept up to date.

       This is done async as this can take some time
    """
    current.s3task.async("stats_update_time_aggregate",
                         args = [row.data_id],
                         )
Note: See TracWiki for help on using the wiki.