Skip to main content

Overview

TrailBase includes a built-in job scheduler that executes background tasks on a cron-like schedule. Jobs can be defined in your configuration or dynamically registered via WASM components.

System Jobs

TrailBase includes several system jobs that run automatically:

Backup

Daily database backups (disabled by default)

Heartbeat

Health check every minute (for monitoring)

Log Cleaner

Hourly cleanup of old log entries

Auth Cleaner

Hourly removal of expired sessions

Query Optimizer

Daily SQLite query optimizer

File Deletions

Hourly cleanup of deleted files

System Job Configuration

Configure system jobs in your config.textproto:

Cron Syntax

Jobs use cron syntax with optional 7 fields:

Field Values

Named Schedules

Examples

Interval-based schedules like “every 100 seconds” are not supported. Cron is time-based, not interval-based. Use the smallest supported interval (seconds) and implement your own timing logic if needed.

Creating Jobs in WASM Components

Job Lifecycle

From crates/core/src/scheduler.rs:

Error Handling

Jobs should handle errors gracefully:

Long-Running Jobs

Jobs should complete quickly. For long-running tasks, create work items and process them incrementally:

Monitoring Jobs

Admin API

List and monitor jobs via the admin API:

Job Status

Best Practices

1

Idempotent jobs

Design jobs to be safely re-run. Check for existing work before processing:
2

Transaction safety

Use transactions for multi-step operations:
3

Resource cleanup

Always clean up resources, even on failure:
4

Logging

Log job execution for debugging:
5

Rate limiting

Avoid overwhelming external APIs:

Example: Email Digest Job

Next Steps

Email

Send emails from jobs

Custom Endpoints

Trigger jobs via API

Vector Search

Batch embedding generation

WASM Components

Create job handlers