| 1 | package chronos |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "log/slog" |
| 6 | ) |
| 7 | |
| 8 | // supervisor is the unexported seam a [ChronosContext] delegates to. It |
| 9 | // is implemented by the package's supervisor in a later phase; keeping |
| 10 | // it unexported lets [ChronosContext] reach back into the running |
| 11 | // supervisor without exposing the dependency. |
| 12 | type supervisor interface { |
| 13 | add(j *Job) error |
| 14 | deactivate(name string) bool |
| 15 | logger() *slog.Logger |
| 16 | } |
| 17 | |
| 18 | // ChronosContext is the handle a [JobFunc] receives on each run. It |
| 19 | // embeds the per-run [context.Context] so Deadline/Done/Err/Value |
| 20 | // reflect the run's cancellation (IV11), and offers methods to log, |
| 21 | // register sibling jobs, and deactivate jobs at runtime. |
| 22 | type ChronosContext struct { |
| 23 | context.Context // the per-run context; promotes Deadline/Done/Err/Value (IV11) |
| 24 | |
| 25 | sup supervisor |
| 26 | name string // the running job's name |
| 27 | } |
| 28 | |
| 29 | // Name returns the running job's name. |
| 30 | func (c *ChronosContext) Name() string { return c.name } |
| 31 | |
| 32 | // Logger returns the supervisor's logger. |
| 33 | func (c *ChronosContext) Logger() *slog.Logger { return c.sup.logger() } |
| 34 | |
| 35 | // Add registers a new [Job] with the running supervisor, surfacing any |
| 36 | // stashed build error (IV6). |
| 37 | func (c *ChronosContext) Add(j *Job) error { return c.sup.add(j) } |
| 38 | |
| 39 | // Deactivate retires the running job (by name), stopping its further |
| 40 | // fires without cancelling this run (IV13). |
| 41 | func (c *ChronosContext) Deactivate() { c.sup.deactivate(c.name) } |
| 42 | |
| 43 | // DeactivateJob retires the named job, reporting whether it existed. |
| 44 | func (c *ChronosContext) DeactivateJob(n string) bool { return c.sup.deactivate(n) } |
| 45 | |