Add an HTTP API server that runs alongside the scheduler, enabling external CLI/tooling to trigger jobs and query status.
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/jobs/{job_id}/trigger |
Trigger a manual job run, returns RunId |
GET |
/api/jobs |
List all registered jobs |
GET |
/api/jobs/{job_id} |
Get job details |
GET |
/api/jobs/{job_id}/runs |
List recent runs for a job (with ?limit=N) |
GET |
/api/runs/{run_id} |
Get run status and details |
GET |
/api/runs/{run_id}/tasks |
Get task states for a run |
GET |
/api/health |
Health check endpoint |
GET |
/api/scheduler/state |
Get scheduler state (running/paused) |
POST |
/api/scheduler/pause |
Pause scheduled triggers |
POST |
/api/scheduler/resume |
Resume scheduled triggers |
axum- HTTP framework (integrates well with existing tokio runtime)tower-http- Middleware (CORS, tracing)
src/api/
├── mod.rs # Module exports, router construction
├── handlers.rs # Request handlers
├── responses.rs # JSON response types
└── errors.rs # Error type -> HTTP status mapping
Currently Scheduler::new() takes ownership of storage. Need to:
- Change to
Arc<S>whereS: Storage - Pass same
Arc<S>to both scheduler and API layer
Files to modify:
src/scheduler/engine.rs- Changestorage: Stostorage: Arc<S>src/main.rs- Wrap storage in Arc before passing
pub struct ApiState<S: Storage> {
pub handle: SchedulerHandle,
pub storage: Arc<S>,
pub jobs: Arc<HashMap<JobId, Job>>, // For job metadata lookup
}Map existing storage/handle methods to HTTP endpoints with proper error handling.
- API server enabled by default
--no-apiflag to disable HTTP server--api-portto configure port (default: 8565)--api-hostto configure bind address (default: 127.0.0.1)
Run HTTP server as separate tokio task alongside scheduler.
| Error | HTTP Status |
|---|---|
JobNotFound |
404 |
StorageError::NotFound |
404 |
DependencyNotSatisfied |
409 Conflict |
MaxConcurrentRunsExceeded |
429 Too Many Requests |
NotRunning |
503 Service Unavailable |
| Other | 500 Internal Server Error |
| File | Action |
|---|---|
src/api/mod.rs |
Create |
src/api/handlers.rs |
Create |
src/api/responses.rs |
Create |
src/api/errors.rs |
Create |
src/lib.rs |
Add pub mod api; |
src/main.rs |
Add CLI flags, spawn API server |
src/scheduler/engine.rs |
Refactor to use Arc<S> |
Cargo.toml |
Add axum, tower-http |
# Start scheduler (API enabled by default on port 8565)
petit --db petit.db run examples/jobs
# Start without API
petit --db petit.db run examples/jobs --no-api
# Custom port
petit --db petit.db run examples/jobs --api-port 9000
# Trigger job from another terminal
curl -X POST http://localhost:8565/api/jobs/hello_world/trigger
# Check run status
curl http://localhost:8565/api/runs/<run-id>
# List jobs
curl http://localhost:8565/api/jobs