Scheduled jobs and crons
Use the scheduler when work should run later, after a successful write, or on a recurring schedule. Jobs are persisted in the database and can be inspected in the dashboard.
Queries cannot be scheduled. Mutations and actions can schedule mutations or actions, including internal functions.
Schedule work after a write
In a module containing the add mutation from Functions and data:
use rxferrix::prelude::*;
use rxferrix::scheduler::ScheduledJob;
#[mutation]
pub async fn add_later(
ctx: &MutationCtx,
#[validate(trim, non_empty, code = "empty_text", message = "A task needs some text")] text: String,
delay_seconds: u32,
) -> Result<Id<ScheduledJob>> {
ctx.scheduler.run_after::<add>(delay_seconds.seconds(), (text,)).await
}The #[validate] here is on add_later, so its caller hears about empty text. Rules on the scheduled function (add) are only checked when the job runs, and a bad value then fails the job. Scheduling inside a mutation commits with that mutation: a rollback creates no job. Scheduling inside an action takes effect immediately in a separate transaction.
Lengths of time read as units: 5.minutes(), 1.days(), 1.5.hours() (TimeUnits, in the prelude). They go wherever an app passes rxferrix a length of time (run_after, every, session_ttl, get_signed_url, ...; an Option<Duration> or a struct field takes 5.minutes().into()), and move a timestamp, which stays milliseconds since the epoch: ctx.now() - 1.days() is a day ago. For a constant, const TTL: TimeSpan = TimeSpan::seconds(4);.
Use run_at::<f>(at_ms, args) for an absolute time in milliseconds since the Unix epoch. A time in the past means as soon as the scheduler can run it. Save the returned job ID if you need to cancel it.
Pass the data the job needs
Scheduled functions run with no user identity, even when a signed-in user scheduled them. An auth check such as require_user() or a SignedIn parameter therefore fails inside the job.
Authorize the request in the public function, then pass the user ID and other needed data to an internal job function. Decide whether delayed work should recheck current permissions when it runs. Do not expose a public function that lets callers impersonate a user by supplying an arbitrary ID.
Add recurring work
Register recurring jobs in the RxFerrix builder, in the app's configure function (src/app.rs), so tests run them too. For an internal maintenance::purge mutation:
RxFerrix::new().schedule(|s| {
s.mutation::<maintenance::purge>().named("purge").hourly();
s.action::<reports::digest>().named("digest").timezone("Europe/Bucharest").weekdays_at("09:00");
s.mutation_with::<feeds::sync>("eu".to_string()).named("sync-eu").every(5.minutes());
})Each line is a cron: hourly() is the cron expression 0 0 * * * *, evaluated in UTC unless a timezone is given. The frequencies are every_minute(), every_minutes(n) (n divides the hour: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20 or 30), hourly(), hourly_at(minute), daily(), daily_at("HH:MM"), weekdays_at("HH:MM"), weekly_on("Mon", "HH:MM"), monthly_on(day, "HH:MM"), cron("<expression>"), and every(duration), a fixed interval: the first run comes duration after the cron is declared (or changed), and the schedule carries on across restarts. Use mutation / action for a function without arguments and mutation_with / action_with to pass them. named(..) sets the cron's name, which _crons and the dashboard show and which keeps its state across restarts; without it, the name is the function's path (a function whose path starts with _, reserved for rxferrix's own crons, needs a name), so renaming the function starts the cron over. A bad value, such as daily_at("25:00"), stops the app from starting.
Cron expressions have six fields: second, minute, hour, day, month, weekday. The builder also takes them directly: .cron::<f>(name, "0 0 * * * *", args), .cron_tz::<f>(name, expression, "Europe/Bucharest", args) for local time, and .interval::<f>(name, duration, args). A spring-forward time that does not exist runs later by the gap; a repeated fall-back time runs once.
Missed runs are coalesced into one run on startup. A cron skips a run if its previous job is still running. Administrators can use Scheduler → Run now.
Understand execution and retries
| Job | Behavior |
|---|---|
| Scheduled mutation | Its writes and completion record commit together; a restart cannot commit the writes without recording completion. |
| Scheduled action, no retries | One attempt; an interruption may have external side effects and is marked failed on recovery. |
| Scheduled action with retries | Failures and restart interruptions can retry while attempts remain. External side effects may repeat. |
Opt into action retries with ctx.scheduler.with_retries(3).run_after::<f>(delay, args). Retries use exponential backoff. Add .jitter(0.5) to randomize each delay by up to half, so actions that fail together don't retry at the same moment. Retry options do not make failed mutations retry.
Give retryable external calls an idempotency key or another duplicate-handling strategy. A failed or canceled action cannot undo mutations it already committed.
Limit concurrency with a workpool
Register a pool in the app's configure function:
use rxferrix::scheduler::Workpool;
rxferrix::RxFerrix::new()
.workpool(Workpool::new("emails").max_parallelism(5).retries(3).jitter(0.5))
.run()Enqueue an action with ctx.scheduler.pool("emails").enqueue::<send_email>((to,)).await?. Each pool has independent slots and retry defaults (retries, backoff and jitter). on_complete::<callback>(context) schedules a mutation after success, final failure, or cancellation; the callback takes the work ID, supplied context, and WorkResult<Output>.
Cancel and inspect jobs
ctx.scheduler.cancel(id).await? cancels a pending job and signals a running action. Actions can check ctx.is_canceled() or await ctx.canceled(). After the default five-second grace period, a still-running action is aborted at an await point. cancel_pool(name) cancels a pool's queued and running work.
The dashboard shows pending and active jobs in _scheduled_jobs. Finished jobs move to history in aux.db after one hour by default and are retained for seven days. Tune this through SchedulerOptions.
In tests, use TestApp::run_due_jobs() and advance_time(...) instead of sleeping. For durable sequences with sleeps and events, see Workflows. Full signatures are in the scheduler reference.