title: scheduler section: scheduler
Lets a webapp register, check, and remove background scheduled tasks on behalf of the signed-in user. Tasks call a script that lives inside the webapp's own folder — not in user virtual storage.
Prerequisite — the user must have granted cron-job permission to this app first. The recommended flow is:
- Check
scheduler.hasPermission()in a backend.agiscript.- If false, return a signal to the frontend so it can call
ao_module_requestSchedulerPermission()to show the permission dialog.- After permission is granted, register the task from the backend.
scheduler.hasPermission() → boolReturns true when the current user is allowed to create scheduled tasks.
requirelib("scheduler");
if (!scheduler.hasPermission()) {
sendResp("no_permission");
}
scheduler.registered(taskName, appName) → boolReturns true when a task with the given name is already registered for this user+app combination.
requirelib("scheduler");
if (scheduler.registered("MyApp_DailySync", "MyApp")) {
sendResp("already_registered");
}
scheduler.register(taskName, appName, intervalSecs [, description [, scriptName]]) → boolRegisters a new background task. Returns true on success.
| Parameter | Type | Description |
|---|---|---|
taskName |
string | Unique task identifier (max 32 chars) |
appName |
string | Module folder name (must match ./web/<appName>/) |
intervalSecs |
number | Execution interval in seconds |
description |
string | Optional human-readable description |
scriptName |
string | Script filename inside the app folder (default: "cron.agi") |
requirelib("scheduler");
var ok = scheduler.register("MyApp_DailySync", "MyApp", 86400, "Daily maintenance", "cron.agi");
if (!ok) {
sendResp("register_failed");
}
scheduler.unregister(taskName) → boolRemoves a previously registered task. Returns true on success.
requirelib("scheduler");
scheduler.unregister("MyApp_DailySync");
The cron script must reside inside the webapp's own web folder, not in user virtual storage:
./web/MyApp/
init.agi ← module registration
index.html
backend.agi ← called by the frontend to register/query scheduler
cron.agi ← executed by the scheduler at each interval
The scheduler calls cron.agi with the permissions of the user who approved it, so all file-system and database operations are scoped to that user.