Jobs (@swelte/plus)

Scheduled @[cron] ticks — beyond-SvelteKit, optional package. Core stays parity-only (byte-identical output without plugins); plus owns attrs core never names.

install
npm install @swelte/plus
vite.config.ts
import { swelte } from '@swelte/ssr-qjs';
import { jobsPlugin } from '@swelte/plus/jobs'; // or '@swelte/plus'

export default defineConfig({
  plugins: [svelte(svelteConfig), swelte({ vBackendUrl, plugins: [jobsPlugin] })],
});

Without the plugin, @[cron] is an unknown-attr build error. With it, src/app.v may hold one-or-more tick fns; core ignores them everywhere else. No CLI, no SSH — ticks start at boot in the container.

@[cron] ticks

src/app.v
module app

@[cron('60s')]
pub fn tick() {
	println('every minute')
}

@[cron('5m,retry=3')]
pub fn sweep() !void {
	// fallible: may throw, retried 3x with 5s-linear backoff
}
RuleDetail
locationsrc/app.v only (route files → only allowed in src/app.v)
qualifier'<duration>[,retry=n]' — duration is digits + s|m|h, nonzero ('60s', '5m', '1h'); whitespace around the comma tolerated
retryretry=1..5, fallible (!void) fns only — void + retry is a file+line build error
shapeno params, void / !void only; bodies must be idempotent

Console log schema

All tick outcomes go to the console (stderr) — no log file by design (container-native: collected by Railway/docker logs, grep-able). First-try success stays silent.

stderr
[swelte:cron] tick=tick status=catchup lag=130s   # wake-up behind >= 2 intervals, fires once
[swelte:cron] tick=sweep status=retry attempt=0 reason=failed
[swelte:cron] tick=sweep status=retry attempt=1 reason=failed
[swelte:cron] tick=sweep status=ok attempt=2       # recovered
[swelte:cron] tick=sweep status=dead attempt=3 reason=failed  # exhausted
StatusWhen
(none)first-try success — silent, zero log spam
retryattempt failed, sleeping (attempt+1) * 5s then retrying
okrecovered on attempt=N (only logged when N > 0)
deadall retry=N attempts failed — dead-letter
errorfallible tick failed, no retry= configured
catchupscheduler woke behind >= 2 * every, fires once then resumes

Default process-local scheduling

  • loadAppHooks reads src/app.v, validates every @[cron] fn, and emits two fragments into the V harness. Jobs start after app startup succeeds and remain lifecycle-tracked.
  • main: top-level swelte_cron_N fns (user bodies verbatim, so fallible fns compile) + swelte_cron_run() — the ONE scheduler thread.
  • mainInner: // @[cron] header + spawn swelte_cron_run().
  • The scheduler holds a next[i] unix deadline per tick, sleeps to the nearest one (idle = one sleeping thread, zero wakeups/CPU), fires due ticks, and advances next[i] = now + every. Missed ticks coalesce, never a catch-up burst.
  • Retries run a blocking linear-backoff loop inside the tick's arm — the scheduler thread is preserved, but a retrying tick may briefly delay other ticks (cap 5 bounds this to ~75s worst case).
  • Zero ticks → empty fragments: harness byte-identical to parity output.

Durable schedules

vite.config.ts
import { swelte } from '@swelte/ssr-qjs'
import { createJobsPlugin } from '@swelte/plus'

const jobs = createJobsPlugin({ durable: true })
// Include this alongside your Svelte Vite plugin:
swelte({ plugins: [jobs] })
// SWELTE_JOBS_PATH=data/jobs.db (default)

The factory keeps the same @[cron] functions while persisting their schedules in SQLite. SWELTE_JOBS_PATH selects a dedicated file (default data/jobs.db), independent of dbPlugin and app data. SQLite development files are needed at build time.

A function name is its persistent identity. First registration schedules it one interval ahead; workers poll once a second. An exclusive SQLite transaction spans each due job and its retries, serializing cooperating processes. Completion commits next_run, last_run and last_status (ok or failed) to swelte_jobs. Overdue intervals coalesce into one run after restart.

Process death releases the lock and rolls back the deadline, making the run due again. External side effects can commit before a crash, so bodies must be idempotent. Execution is at least once; a crash restarts the retry policy. A long body or backoff holds the scheduler database writer lock and delays its other jobs. Jobs must not access that scheduler database themselves.

  • All cooperating processes must share the same SQLite file, job names and intervals on a local/shared-host filesystem. Independent files or network filesystems do not provide this coordination.
  • Renaming a function creates a new schedule. Interval changes apply after its next run. Removed functions are not executed, even if their rows remain.
  • Shutdown stops scheduling and releases the connection/transaction as tracked work exits.
  • Default jobsPlugin remains process-local best effort: two containers run two schedulers.
  • Duration qualifiers are supported; cron expressions are not parsed.