wiki:DeveloperGuidelines/Scheduler

Version 12 (modified by Fran Boon, 11 years ago) ( diff )

--

Scheduler

The scheduler is used for asynchronous tasks (not from a user request to the web server).

Some example uses are for daily 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, **vars):
    return result

tasks["my_task"] = my_task

Notes:

  • args/vars need to be simple Text, Integer or JSON structures
  • the scope for a task varies depending on whether the scheduler is running (new request) or not (same request as pushes it async)
    • therefore assume the least scope (new request) & ensure that testing is done with a real scheduler running

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],
                         )

Launch Scheduler

cd web2py
python web2py.py -K eden

Note that this cannot be used with SQLite as it needs simultaneous access to the database.

See Also:

Debugging

A task can be run through the Eclipse debugger using this script:

Note: The Task must have an entry in the recurring tasks table: db.scheduler_task & this is where the args/vars are read from

Note: See TracWiki for help on using the wiki.