← Back to Official Package Registry
Tier 6: Local-First, IoT, Web & Media • Cron & Persistent Job Queuev1.0.0 • 27 KB • Ed25519 Verified

jobs_cron@1.0.0

Standard 5-field cron parser & next-tick calculator paired with a crash-safe Zero-FFI SQLite job queue (leases, retries, and dead-letter).

use [jobs_cron/cron]use [jobs_cron]
veko pkg add jobs_cron@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

jobs_cron

POSIX 5-field Cron scheduler and transactional persistent SQLite background job queue for Veko, built on Zero-FFI db/sqlite and datetime.

Installation

powershell
veko pkg add jobs_cron@1.0.0

Import in any Veko module:

rebol
use [jobs = jobs_cron, cron = jobs_cron/cron]

Architecture Notes

  • 64-Bit Bitmask Cron Compiler (jobs_cron/cron): Compiles POSIX 5-field schedules (minute hour dom month dow), wildcards (*), step intervals (*/15, 9-17/2), lists (1,15,30), 3-letter month/weekday names (jan..dec, sun..sat), and shorthand macros (@hourly, @daily, @weekly, @monthly, @yearly) into compact i64 bitmasks.
  • Calendar-Jumping Next-Run Search: cron/next-run-unix advances directly across non-matching months, days, and hours via lib/datetime rather than scanning minute-by-minute, resolving multi-year schedules (including leap-year Feb 29) in microseconds.
  • Transactional Zero-FFI SQLite Job Queue (jobs_cron): Stores jobs in standard .sqlite B-tree files (veko_jobs) with priority ordering, delayed run_at scheduling, BEGIN/COMMIT atomic worker claiming, exponential retry backoff (base * 2^(attempts - 1)), and 'dead' dead-letter inspection/replay.

API Reference

jobs_cron/cron

  • CronExpr — Compiled schedule (source, min_mask, hour_mask, dom_mask, month_mask, dow_mask).
  • cron/parse expr_str -> CronExpr
  • cron/matches-datetime? expr dt -> bool
  • cron/matches-unix? expr unix_sec -> bool
  • cron/next-run-unix expr after_unix_sec -> i64

jobs_cron

  • Job — { id, queue, kind, payload, data, status, priority, run_at, attempts, max_attempts, last_error, worker_id, created_at, updated_at }
  • QueueStats — { pending, running, completed, dead, total }
  • JobQueue — Open queue handle wrapping db/sqlite.
  • jobs/open-queue db_path -> JobQueue
  • jobs/close-queue mut q -> enum
  • jobs/enqueue mut q kind payload [/queue name] [/priority n] [/run-at unix_sec] [/max-attempts n] [/now unix_sec] -> i64
  • jobs/get-job q job_id -> Job
  • jobs/claim-next mut q worker_id now_unix [/queue name] -> Job | .none
  • jobs/complete-job mut q job_id now_unix -> enum
  • jobs/fail-job mut q job_id err_msg now_unix [/base-backoff sec] -> map
  • jobs/retry-dead-jobs mut q now_unix [/queue name] -> i64
  • jobs/queue-stats q [/queue name] -> QueueStats

Idiomatic Veko Example

rebol
use [jobs = jobs_cron, cron = jobs_cron/cron]

next_ts = cron/next-run-unix '*/15 9-17 * * 1-5' 1700000000

mut q = jobs/open-queue ':memory:'
jid = jobs/enqueue/priority/run-at mut q 'generate_report' { team = 'core' } 10 next_ts

claimed = jobs/claim-next mut q 'worker-a' next_ts
if claimed != .none [
    _ = jobs/complete-job mut q claimed.id (next_ts + 2)
]
_ = jobs/close-queue mut q
packages/jobs_cron/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/jobs_cron/examples/demo.vk
// Verification suite for `jobs_cron` and `jobs_cron/cron`:
// 5-field POSIX Cron expressions (`*/15 9-17 * * 1-5`, `@daily`, `@monthly`),
// `matches-unix?`, `next-run-unix`, and persistent SQLite job queue lifecycle
// (priority/scheduled claiming, exponential retry backoff, dead-lettering, recovery).

use [jobs = jobs_cron, cron = jobs_cron/cron, dt = datetime, fs]

assert-eq = fn [label str, got any, want any] [
	if got != want [
		println `[FAIL] {label}: got {mold got}, expected {mold want}`
		return (err `assertion failed: {label}`)
	]
	println `[PASS] {label}`
	.ok
]

assert-true = fn [label str, cond bool] [
	if not cond [
		println `[FAIL] {label}`
		return (err `assertion failed: {label}`)
	]
	println `[PASS] {label}`
	.ok
]

main = fn [] any [
	// 1. Verify POSIX 5-field Cron parser & `next-run-unix`
	work_hours = cron/parse/or '*/15 9-17 * * 1-5' [e -> return e]

	// Friday 2026-12-04 17:45:00 UTC -> weekday = 5 (Friday), matches `*/15 9-17 * * 1-5`
	fri_1745 = dt/to-unix (dt/DateTime { year = 2026, month = 12, day = 4, hour = 17, minute = 45, second = 0 })
	_ = assert-eq/or 'Friday 17:45 matches */15 9-17 * * 1-5' (cron/matches-unix? work_hours fri_1745) true [e -> return e]

	// Next run strictly after Friday 17:45 UTC must skip Saturday (Dec 5) & Sunday (Dec 6) -> Monday 2026-12-07 09:00:00 UTC!
	mon_0900 = dt/to-unix (dt/DateTime { year = 2026, month = 12, day = 7, hour = 9, minute = 0, second = 0 })
	next_work = cron/next-run-unix/or work_hours fri_1745 [e -> return e]
	_ = assert-eq/or 'next-run-unix skips weekend to Monday 09:00 UTC' next_work mon_0900 [e -> return e]

	// Verify `@daily` macro (`0 0 * * *`)
	next_daily = cron/next-run-unix/or '@daily' fri_1745 [e -> return e]
	sat_0000 = dt/to-unix (dt/DateTime { year = 2026, month = 12, day = 5, hour = 0, minute = 0, second = 0 })
	_ = assert-eq/or '@daily next-run-unix -> 2026-12-05T00:00:00Z' next_daily sat_0000 [e -> return e]

	// Verify list + step + month name (`0,30 */6 1 jan,dec *`)
	jan1_0630 = dt/to-unix (dt/DateTime { year = 2027, month = 1, day = 1, hour = 6, minute = 0, second = 0 })
	next_jan = cron/next-run-unix/or '0,30 */6 1 jan,dec *' jan1_0630 [e -> return e]
	jan1_0630_want = dt/to-unix (dt/DateTime { year = 2027, month = 1, day = 1, hour = 6, minute = 30, second = 0 })
	_ = assert-eq/or 'comma + step + month-name cron next-run' next_jan jan1_0630_want [e -> return e]

	// 2. Verify persistent file-backed SQLite job queue (with automatic temp file cleanup)
	tmp_db = str/join [(os/temp-dir) '/veko_jobs_cron_demo_' (mold (time/unix)) '.sqlite'] ''
	jnl_db = str/append tmp_db '-journal'
	defer [
		_ = os/remove/or tmp_db [ .ok ]
		_ = os/remove/or jnl_db [ .ok ]
	]

	mut q = jobs/open-queue/or tmp_db [e -> return e]
	_ = assert-true/or 'SQLite queue file created on disk' (fs/file? tmp_db) [e -> return e]

	t0 = 1700000000

	// Enqueue: low priority job, high priority job, and future-scheduled job
	id_low    = jobs/enqueue/priority/now/or mut q 'send_email' { to = 'user@example.com' } 1 t0 [e -> return e]
	id_high   = jobs/enqueue/priority/max-attempts/now/or mut q 'sync_ledger' { acct = 'A-9' } 10 2 t0 [e -> return e]
	id_future = jobs/enqueue/priority/run-at/now/or mut q 'nightly_digest' { batch = 7 } 50 (t0 + 300) t0 [e -> return e]

	// Claim at `t0`: `id_future` is not yet due (`t0 + 300 > t0`), so `id_high` (priority 10) must win over `id_low` (priority 1)!
	c1 = jobs/claim-next/or mut q 'worker-1' t0 [e -> return e]
	_ = assert-eq/or 'claim-next picks highest-priority due job (id_high)' c1.id id_high [e -> return e]
	_ = assert-eq/or 'claimed job status is running' c1.status 'running' [e -> return e]
	_ = assert-eq/or 'claimed job attempts == 1' c1.attempts 1 [e -> return e]
	_ = assert-eq/or 'claimed job decoded payload' (map/get c1.data 'acct') 'A-9' [e -> return e]

	// Fail `id_high` once (`max_attempts == 2`): should reschedule as `'pending'` with exponential backoff `t0 + 10` (`base=10 * 2^0`)
	f1 = jobs/fail-job/base-backoff/or mut q id_high 'upstream 503' t0 10 [e -> return e]
	_ = assert-eq/or 'first failure reschedules as pending' (map/get f1 'status') 'pending' [e -> return e]
	_ = assert-eq/or 'first failure backoff run_at == t0 + 10' (map/get f1 'run_at') (t0 + 10) [e -> return e]

	// Claim again at `t0`: `id_high` is backed off until `t0 + 10`, so `id_low` is claimed now!
	c2 = jobs/claim-next/or mut q 'worker-1' t0 [e -> return e]
	_ = assert-eq/or 'claim-next at t0 picks id_low while id_high backs off' c2.id id_low [e -> return e]
	_ = jobs/complete-job/or mut q id_low (t0 + 1) [e -> return e]

	// Reopen queue from disk to verify persistence across restarts!
	_ = jobs/close-queue mut q
	mut q2 = jobs/open-queue/or tmp_db [e -> return e]

	// Advance clock to `t0 + 10`: `id_high` becomes due again (attempt 2 of 2)
	c3 = jobs/claim-next/or mut q2 'worker-2' (t0 + 10) [e -> return e]
	_ = assert-eq/or 'reopened SQLite queue claims backed-off id_high at t0+10' c3.id id_high [e -> return e]
	_ = assert-eq/or 'id_high attempts == 2' c3.attempts 2 [e -> return e]

	// Fail `id_high` a 2nd time (`attempts == max_attempts == 2`) -> moves to `'dead'` dead-letter queue!
	f2 = jobs/fail-job/base-backoff/or mut q2 id_high 'permanent upstream 503' (t0 + 12) 10 [e -> return e]
	_ = assert-eq/or 'exhausting max_attempts transitions job to dead' (map/get f2 'status') 'dead' [e -> return e]

	st1 = jobs/queue-stats/or q2 [e -> return e]
	_ = assert-eq/or 'stats completed == 1' st1.completed 1 [e -> return e]
	_ = assert-eq/or 'stats dead == 1' st1.dead 1 [e -> return e]
	_ = assert-eq/or 'stats pending == 1 (future job)' st1.pending 1 [e -> return e]

	// Retry dead-letter jobs & claim both remaining jobs at `t0 + 300`
	retried = jobs/retry-dead-jobs/or mut q2 (t0 + 300) [e -> return e]
	_ = assert-eq/or 'retry-dead-jobs resets 1 dead job' retried 1 [e -> return e]

	c4 = jobs/claim-next/or mut q2 'worker-2' (t0 + 300) [e -> return e]
	_ = assert-eq/or 'at t0+300 priority-50 id_future claims first' c4.id id_future [e -> return e]
	_ = jobs/complete-job/or mut q2 id_future (t0 + 301) [e -> return e]

	c5 = jobs/claim-next/or mut q2 'worker-2' (t0 + 301) [e -> return e]
	_ = assert-eq/or 'retried dead job claims cleanly with reset attempts=1' c5.id id_high [e -> return e]
	_ = assert-eq/or 'retried job attempts == 1' c5.attempts 1 [e -> return e]
	_ = jobs/complete-job/or mut q2 id_high (t0 + 302) [e -> return e]

	c_idle = jobs/claim-next/or mut q2 'worker-2' (t0 + 302) [e -> return e]
	_ = assert-eq/or 'empty queue returns .none' c_idle .none [e -> return e]

	_ = jobs/close-queue mut q2

	println 'jobs_cron demo: all checks passed.'
	0
]

main
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/jobs_cron/1.0.0.vkp
Archive SHA-256: aa6d41c650767b1f0beb74cad777ade4a757be17fd6ff30aa58481786a0a5c96
Merkle Root: 4bdb70da0d55695aab6a3b74ee03399a9b7d4991fe1551e271ddbacf89036b4c
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"jobs_cron","version":"1.0.0","tier":6.0,"tier_label":"Tier 6: Local-First, IoT, Web & Media","category":"Cron & Persistent Job Queue","description":"Standard 5-field cron parser & next-tick calculator paired with a crash-safe Zero-FFI SQLite job queue (leases, retries, and dead-letter).","modules":["jobs_cron/cron","jobs_cron"],"homepage":"https://veko-lang.org/packages/jobs_cron"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md2 KB3eb89bd8c7b147a4f100b22bb0b1c288581686a45c8debc45843815405c47c76
cron.vk7 KBad724ae4d981842d49d6ab2449ffdc3217a3b1e97da08f666c8a9010eb67bafb
examples/demo.vk6 KBf2a218d90a3cbf0860ce0e0899d27a5361120b2d47f8afec359bd120297c2ba0
mod.vk9 KB629e9e39125c682ae95d25f674c4edf25dd65dc19b61904176d1653d56f16fc0