Scheduled tasks and background jobs for Datasette. Provides a central scheduler where plugins register handler functions via a hook, and tasks (DB rows) reference those handlers with config + schedule.
- Backend: Python, Datasette >=1.0a23, datasette-plugin-router, Pydantic
- Frontend: Svelte 5 (runes), TypeScript, Vite, openapi-fetch
- Database: sqlite-migrate for internal.db schema management
- Build: Just (Justfile), uv (Python), npm (frontend)
| Command | What it does |
|---|---|
just dev |
Run Datasette dev server on port 8010 |
just dev-with-hmr |
Datasette + Vite HMR (restarts on .py/.html changes) |
just frontend-dev |
Start Vite dev server on port 5180 |
just frontend |
Build frontend for production |
just types |
Regenerate all TypeScript types from Python |
just types-watch |
Watch .py files, auto-regenerate types |
just format |
Format backend (ruff) + frontend (prettier) |
just check |
Type-check backend (ty) + frontend (svelte-check) |
just test |
Run Python tests |
just shots [names...] |
Regenerate committed doc screenshots in docs/screenshots/ (throwaway datasette on :8492; one-time setup npx playwright install chromium in frontend/) |
datasette_cron/
βββ __init__.py # Plugin hooks (startup, shutdown, routes, menu_links)
βββ hookspecs.py # cron_register_handlers hookspec
βββ router.py # Shared Router + permission decorator
βββ page_data.py # Pydantic models (page data + API contracts)
βββ internal_db.py # Database operations wrapper
βββ internal_migrations.py # sqlite-migrate schema
βββ schedules.py # CronSchedule, IntervalSchedule, RRuleSchedule
βββ scheduler.py # Scheduler class: loop, execution, retry, CRUD API
βββ routes/
β βββ pages.py # Page routes (render HTML)
β βββ api.py # API routes (return JSON)
βββ templates/
βββ cron_base.html # Single base template
frontend/src/
βββ pages/index/ # Task list page
βββ pages/detail/ # Task detail + run history page
βββ page_data/load.ts # loadPageData<T>() helper
βββ page_data/*.types.ts # Generated TypeScript types
βββ components/ # Shared components
βββ store.svelte.ts # Global state
Handlers: Python functions registered via cron_register_handlers(datasette) hook. Return {"name": callable}. Signature: async fn(datasette, config: dict).
Tasks: DB rows in datasette_cron_tasks. Reference a handler by name + JSON config + schedule. Created via datasette._cron_scheduler.add_task(...).
Schedules: Three types β cron string ("0 8 * * *"), interval ({"interval": 60}), rrule ({"rrule": "FREQ=WEEKLY;BYDAY=MO"}). All support timezone.
Pages:
GET /-/cronβ Task listGET /-/cron/<name>β Task detail + run history
API:
GET /-/api/cron/tasksβ JSON task listGET /-/api/cron/tasks/<name>/runsβ Run historyPOST /-/api/cron/tasks/<name>/triggerβ Manual triggerPOST /-/api/cron/tasks/<name>/enableβ Toggle enable/disable
Tables (in internal.db):
datasette_cron_tasksβ Task definitions (handler, config, schedule, status)datasette_cron_runsβ Execution history (status, duration, errors)
Migrations: internal_migrations.py β applied on startup.
register_routes()β registers all routes from shared routerextra_template_vars()β providesdatasette_cron_vite_entry()for templatesregister_actions()β definesdatasette-cron-accesspermissionmenu_links()β adds "Cron Tasks" to nav menustartup()β applies migrations, builds handler registry, registers the scheduler loop viadatasette.add_background_task(scheduler.run, ...)shutdown()β cancels in-flight executions before core cancels the loop task
cron_register_handlers(datasette)β plugins implement this to register handler functions
- Svelte 5 runes: Use
$state(),$derived(),$effect(),$props() - Permissions: All routes use
@check_permission()decorator - Template: One template for all pages β different
entrypointandpage_dataper route - Internal DB reads: Use
execute_write_fn()even for reads - add_task is idempotent: Safe to call on every startup (upsert)