Skip to content

Latest commit

Β 

History

History
102 lines (78 loc) Β· 4.57 KB

CLAUDE.md

File metadata and controls

102 lines (78 loc) Β· 4.57 KB

datasette-cron

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.

Architecture

  • 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)

Commands

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

Project Structure

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

Core Concepts

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.

Routes

Pages:

  • GET /-/cron β†’ Task list
  • GET /-/cron/<name> β†’ Task detail + run history

API:

  • GET /-/api/cron/tasks β†’ JSON task list
  • GET /-/api/cron/tasks/<name>/runs β†’ Run history
  • POST /-/api/cron/tasks/<name>/trigger β†’ Manual trigger
  • POST /-/api/cron/tasks/<name>/enable β†’ Toggle enable/disable

Database

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.

Hooks Used

  • register_routes() β€” registers all routes from shared router
  • extra_template_vars() β€” provides datasette_cron_vite_entry() for templates
  • register_actions() β€” defines datasette-cron-access permission
  • menu_links() β€” adds "Cron Tasks" to nav menu
  • startup() β€” applies migrations, builds handler registry, registers the scheduler loop via datasette.add_background_task(scheduler.run, ...)
  • shutdown() β€” cancels in-flight executions before core cancels the loop task

Hooks Defined

  • cron_register_handlers(datasette) β€” plugins implement this to register handler functions

Key Conventions

  • Svelte 5 runes: Use $state(), $derived(), $effect(), $props()
  • Permissions: All routes use @check_permission() decorator
  • Template: One template for all pages β€” different entrypoint and page_data per route
  • Internal DB reads: Use execute_write_fn() even for reads
  • add_task is idempotent: Safe to call on every startup (upsert)