PluginsBuild a plugin

Scheduled tasks

Run a Rustrak plugin's code on a schedule with #[task]: crontabs in UTC, schedules the admin sets, the slot each run is for, time limits, overlaps and failures.

A task is a function Rustrak runs on a crontab, with no request behind it. Roll up yesterday’s numbers, poll a status page, delete what a plugin no longer needs. Tasks go in src/server/tasks/, one attribute each, like routes:

// src/server/tasks/rollup.rs
use rustrak_plugin::prelude::*;

#[task("0 * * * *")]
fn rollup(ctx: &Ctx) -> Result<()> {
    let now = ctx.scheduled_at().unwrap_or_default();
    let today = now - now % 86_400;
    ctx.db().execute(
        "INSERT OR IGNORE INTO daily_visits (project_id, day, visits) \
         SELECT project_id, date(created_at, 'unixepoch'), count(*) FROM visits \
         WHERE created_at < ?1 GROUP BY 1, 2",
        params![today],
    )?;
    Ok(())
}
Rust

Put #[task] anywhere else and the build stops with a task goes in src/server/tasks/.

The crontab

#[task] takes a five-field crontab, read in UTC, or an alias such as @hourly or @daily. There is no seconds field, so once a minute is as often as a task runs.

CrontabRuns
* * * * *Every minute
*/15 * * * *Every 15 minutes
0 3 * * *Every day at 03:00 UTC
0 9 * * 1Every Monday at 09:00 UTC
@hourlyAt the start of every hour

Rustrak refuses to install a plugin whose crontab does not parse, or never matches a date (0 0 30 2 *, February 30th).

A task runs as long as the plugin is installed, whether or not it is turned on in any project.

A schedule the admin sets

Name a setting instead of a crontab, and the admin decides when the task runs, from the plugin’s settings form:

// rustrak.config.ts
settings: {
  rollupSchedule: { type: 'text', default: '0 * * * *' },
},

The setting must be a text setting with scope: 'instance', the default. Rustrak checks its default when the plugin is installed, and each value the admin saves. One that is not a crontab is refused on the field: rollupSchedule is not a crontab: ....

A saved value applies from the next minute. An empty value turns the task off, and so does a setting with no value and no default. #[task] takes a crontab or a setting, never both.

The context

A task takes &Ctx, or nothing, and returns Result<()>.

CtxIn a task
ctx.db()The plugin’s database, as in a route
ctx.scheduled_at()Some(seconds): the slot this run is for, in unix seconds. A run late in its minute still gets the minute’s start
Settings::get(ctx)The instance settings. A task runs in no project, so it cannot read a project’s values
ctx.project_id()None
ctx.fetch(...)A request to a host the plugin allows. See Outbound HTTP
ctx.enqueue(...)Queues a background job, which runs with no project either

In a route, ctx.scheduled_at() is None.

The rollup above uses the slot to find today’s midnight in UTC, and adds up only the days before it, once each. A day that is over gets no more visits, so its total stays right even after retention deletes the visits behind it.

Missed slots and overlaps

A slot that passes while Rustrak is stopped is not run later. A task that must catch up keeps the last slot it handled in its database and starts from there.

A task never overlaps itself. If its last run is still going when the next slot comes, Rustrak skips that slot.

Failures

A task that returns an Error, panics or runs out of time is written to Rustrak’s log, under the rustrak::plugin target, and runs again in its next slot. Nothing retries the slot that failed.

[my-plugin] task rollup failed: the plugin ran past its time budget

Limits

WhatLimit
Time per run, queries and requests included30 seconds
Memory32 MB
Tasks and jobs running at onceOne, across every plugin. The others wait their turn
Runs of one task at onceOne

Because tasks and jobs take turns, a task that runs for 30 seconds every minute delays every other plugin’s tasks and jobs. Keep a run short, or split the work into jobs.

Which Rustrak runs them

Tasks need Rustrak 0.37.0 or later. A plugin built with this version of the SDK declares the plugin world 0.1.1, which an older Rustrak refuses to install. Plugins built before keep working on 0.37.0 unchanged.

Esc

Type to search guides, configuration and the API.