|
| 1 | +# scheduler |
| 2 | + |
| 3 | +Job scheduler with pluggable storage backends. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Pluggable storage**: Bring your own `JobStore` implementation (Postgres included) |
| 8 | +- **Concurrent workers**: Configurable worker pool for parallel job execution |
| 9 | +- **Dynamic configuration**: Override settings at runtime via `SettingsProvider` |
| 10 | +- **Testable**: Fake clock, store, and logger for deterministic tests |
| 11 | +- **Schedule types**: Daily, Weekly, and Interval schedules with timezone support |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +```go |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "log" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/hatmaxkit/hatmax/scheduler" |
| 24 | + "github.com/hatmaxkit/hatmax/scheduler/postgres" |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + db := connectDB() |
| 29 | + store := postgres.NewStore(db) |
| 30 | + |
| 31 | + cfg := scheduler.Config{ |
| 32 | + Enabled: true, |
| 33 | + Interval: 30 * time.Second, |
| 34 | + BatchSize: 20, |
| 35 | + Workers: 4, |
| 36 | + } |
| 37 | + |
| 38 | + sched := scheduler.New(store, cfg, logger) |
| 39 | + |
| 40 | + sched.Register("send-email", func(ctx context.Context, job scheduler.Job) scheduler.Result { |
| 41 | + // Process job |
| 42 | + return scheduler.Result{Output: map[string]any{"sent": true}} |
| 43 | + }) |
| 44 | + |
| 45 | + sched.Start(ctx) |
| 46 | + defer sched.Stop(ctx) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Configuration |
| 51 | + |
| 52 | +### Static (Config struct) |
| 53 | + |
| 54 | +| Field | Default | Description | |
| 55 | +|-------|---------|-------------| |
| 56 | +| Enabled | false | Enable/disable scheduler | |
| 57 | +| Interval | 1m | Polling interval | |
| 58 | +| BatchSize | 20 | Max jobs per tick | |
| 59 | +| Workers | 1 | Concurrent workers | |
| 60 | +| RetryAttempts | 3 | Max retry attempts | |
| 61 | +| RetryBackoff | 1m | Base backoff duration | |
| 62 | + |
| 63 | +### Dynamic (SettingsProvider) |
| 64 | + |
| 65 | +Implement `SettingsProvider` to override at runtime: |
| 66 | + |
| 67 | +```go |
| 68 | +sched.SetSettings(settingsService) |
| 69 | +``` |
| 70 | + |
| 71 | +Settings keys: |
| 72 | +- `scheduler.enabled` - Override Enabled |
| 73 | +- `scheduler.interval_seconds` - Override Interval |
| 74 | +- `scheduler.paused` - Pause without stopping |
| 75 | + |
| 76 | +## Schedule Types |
| 77 | + |
| 78 | +```go |
| 79 | +// Daily at 9:00 AM UTC |
| 80 | +daily := scheduler.Daily{Hour: 9, Minute: 0} |
| 81 | + |
| 82 | +// Weekly on Friday at 5:00 PM in New York |
| 83 | +loc, _ := time.LoadLocation("America/New_York") |
| 84 | +weekly := scheduler.Weekly{Day: time.Friday, Hour: 17, Minute: 0, TZ: loc} |
| 85 | + |
| 86 | +// Every 30 minutes |
| 87 | +interval := scheduler.Interval{Every: 30 * time.Minute} |
| 88 | + |
| 89 | +// Get next run time |
| 90 | +next := daily.Next(time.Now()) |
| 91 | +``` |
| 92 | + |
| 93 | +## Testing |
| 94 | + |
| 95 | +Use fakes for deterministic tests: |
| 96 | + |
| 97 | +```go |
| 98 | +store := scheduler.NewFakeStore() |
| 99 | +clock := scheduler.NewFakeClock(baseTime) |
| 100 | +log := &scheduler.FakeLogger{} |
| 101 | + |
| 102 | +sched := scheduler.New(store, cfg, log) |
| 103 | +sched.SetClock(clock) |
| 104 | + |
| 105 | +store.AddJob(scheduler.Job{ |
| 106 | + ID: "test-job", |
| 107 | + TaskType: "email", |
| 108 | + ScheduledFor: clock.Now(), |
| 109 | +}) |
| 110 | + |
| 111 | +sched.Tick(ctx) |
| 112 | +clock.Advance(time.Hour) |
| 113 | +``` |
| 114 | + |
| 115 | +## Postgres Backend |
| 116 | + |
| 117 | +```go |
| 118 | +import "github.com/hatmaxkit/hatmax/scheduler/postgres" |
| 119 | + |
| 120 | +store := postgres.NewStore(db) |
| 121 | + |
| 122 | +// Apply schema (or use migrations) |
| 123 | +db.Exec(postgres.Schema) |
| 124 | +``` |
| 125 | + |
| 126 | +Tables: |
| 127 | +- `scheduled_jobs` - Job definitions |
| 128 | +- `job_runs` - Execution history |
0 commit comments