// 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