Scheduled tasks

A cron-like scheduler. Each task is a record of the BSPK_TASK_MANAGER table; the manager, started by BSPK_STARTUP and run on the server, checks the tasks about once a second and launches those that are due. BSPK_QUIT stops it, along with the workers it launched.

A task

FieldRole
nameThe task name, which is also the name of the worker it runs in.
method_nameThe method to run (31 characters at most).
periodWhen, see below.
paramAn object, passed as the method's only parameter.
actifFalse: the task never runs.
executeOnStartupRun once at startup, then follow the period.
executeOnDevIn DEV mode, only tasks with this flag run.
forceRun at the next pass whatever the period (reset to false afterwards).
lastExecuteFilled in by the manager.

There is no screen and no configuration file: you add a task by creating the record, once, for instance in an update method or at startup, after checking it does not exist yet:

var $Task : cs.BSPK_TASK_MANAGEREntity
If (ds.BSPK_TASK_MANAGER.query("name = :1"; "NIGHTLY_EXPORT").length=0)
	$Task:=ds.BSPK_TASK_MANAGER.new()
	$Task.name:="NIGHTLY_EXPORT"
	$Task.method_name:="NIGHTLY_EXPORT"
	$Task.period:="0 0 2 * *"      // every day at 02:00:00
	$Task.param:={vl_Days: 7}
	$Task.actif:=True
	$Task.executeOnDev:=False
	$Task.executeOnStartup:=False
	$Task.save()
End if

The period

Up to five space-separated fields, starting with the seconds (this is not the Unix crontab order):

ss  mm  hh  jj  MMM
FieldValues
ss (seconds)0 to 59
mm (minutes)0 to 59
hh (hours)0 to 23
jj (day)1 to 31, or a weekday: Monday … Sunday
MMM (month)1 to 12

Each field is * (any value), */x (when the value is a multiple of x) or a number. A missing field is not checked: it matches any value.

PeriodRuns
0 */30 * * *every 30 minutes, at second 0
0 5 3 * *every day at 03:05:00
0 0 8 Mondayevery Monday at 08:00:00

How it runs

The manager goes through your project's BSPH_CALL_WORKER method, which does CALL WORKER(name; method_name; param). Consequences:

  • the method runs in your project's context, on the server: it must be one of your methods, or a shared component method;
  • one worker per task name: a run still in progress delays the next one (calls queue in the worker), two runs of the same task never run in parallel.

A task without a method_name is skipped, and an alert goes out once as an error mail.

The component's tasks

TaskPeriodRole
BSPK_DUMP_WEBevery 30 minutesrefreshes the site's exchange snapshot (BWEB/exchange/snapshot), on a development machine only (a DEV_PARAMETERS is present); see Content reconciliation
BSPK_DUMP_MAINevery minutethe quick pass of the date memory, on a development machine only
CRON_EXPORT_BSPK_HISTORYevery day at 03:05archiving of the change history

Good to know

  • An empty period runs the task at every pass, that is, about every second.
  • A * in the seconds runs the task at every pass of the matching minute (sixty times or so): fix the second (0 5 3 * *, not * 5 3 * *).
  • A deadline precise to the second can be missed: the check happens about every second, and a second is sometimes skipped. There is no catching up.
  • */x is a multiple of the value, not "every x units since the last run": */7 on minutes fires at 0, 7, …, 56, then again at 0, four minutes later.
  • In DEV mode, a task without executeOnDev never runs, with no message.

See also