Documentation
¶
Overview ¶
Package service hosts the schedule-generation workflow shared by the CLI (cmd/generate.go) and the HTTP API (internal/api/schedule.go): loading the active scheduling blocks, fetching available Tunarr content, running the scheduling engine over a time window, and -- when asked -- pushing the result to Tunarr and committing engine state.
This package is a straight extraction of what cmd/generate.go used to do inline (see git history for ProcessSchedule and its helpers): the scheduling logic itself (block loading, content fetching, engine invocation, apply) is unchanged. What did change, because this code now also runs inside a long-lived API server rather than a one-shot CLI process, is presentation and lifecycle: colored fmt.Printf progress output became slog calls, context.Background() became the caller's ctx, and the CLI's per-invocation (and therefore always-empty) content cache became a Runner-scoped one that actually gets reused across calls.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ActiveBlocks ¶
ActiveBlocks returns the Spec of every enabled block in the store. scheduler.yaml is import-only (see blockio.Bootstrap): the store is the engine's live source of scheduling truth, and disabled blocks stay defined but out of schedule generation. Moved here from cmd/generate.go's loadActiveBlocks so both the CLI and the API server share one implementation.
Types ¶
type Options ¶
Options configures a single Run. Days is the number of 24h days ahead of now to generate a schedule for; callers (the API's handlers, cmd/generate.go) are responsible for validating it (the API restricts it to 1..30) -- Run does not re-validate it. ChannelID, when non-empty, restricts which blocks are planned at all -- see Run's doc comment for why that matters beyond just narrowing the returned/pushed result. Apply gates whether Run mutates anything: false is a pure dry-run (no UpdateSchedule calls, no Engine.Commit()).
type Result ¶
type Result struct {
Applied bool
Channels map[string][]scheduler.ScheduledSlot
}
Result is what Run produces: the generated (and, if Applied, pushed) schedule, keyed by Tunarr channel ID.
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner executes the schedule-generation workflow against a store and a Tunarr client held for its lifetime.
func NewRunner ¶
func NewRunner(st *store.Store, tc *tunarr.Client, l *slog.Logger, loc *time.Location, historyWindow time.Duration) *Runner
NewRunner builds a Runner backed by st and tc. l and loc default to slog.Default() and time.Local respectively when nil, matching scheduler.NewEngine's own nil-handling. historyWindow is forwarded unchanged to every scheduler.Engine Run builds (scheduler.EngineOptions.HistoryWindow) -- callers pass config.MaintenanceHistoryRetention(cfg) here (see cmd/generate.go, cmd/serve.go) so the engine's in-memory dedup window and its Commit-time schedule_history pruning both honor the configured maintenance.history_retention value instead of a hardcoded 7 days. Zero falls back to scheduler's own default (7 days) -- see scheduler.NewEngineWithOptions.
func (*Runner) Run ¶
Run executes one generate-(and-maybe-apply) cycle: load the active blocks (restricted to o.ChannelID's blocks when set -- see below), fetch available Tunarr content, run the scheduling engine over [now, now+o.Days days), and -- only when o.Apply is set -- push the result to Tunarr per channel and commit the engine's pending state. A dry run (o.Apply == false) never calls UpdateSchedule or Commit, so it cannot mutate the store or Tunarr.
o.ChannelID filtering happens to the *blocks*, before scheduler.Engine ever sees them -- not just to the result map after planning. This matters because Engine plans every block it's given: for each one it mutates in-memory pending state (series-cursor advances in pendingStates, "aired" rows in pendingHistory -- see planSeriesForConfig/recordHistory in engine.go) regardless of whether that block's channel ends up in the caller's requested scope. If filtering were applied only to Run's *return value* (as an earlier version of this function did), a channel-scoped Apply would still plan -- and Engine.Commit() would still persist -- every other channel's series cursors and history, even though nothing was pushed to Tunarr for them: series scheduling on untouched channels would silently skip episodes or wrongly dedup against history it never actually aired. Pre-filtering the blocks slice keeps planning itself scoped, so pendingStates/pendingHistory can only ever contain entries for blocks on the requested channel.
A ChannelID that matches no enabled block (typo, disabled, unknown channel) is not an error: blocks ends up empty, Engine plans nothing, and Run returns Result{Applied: o.Apply, Channels: <empty map>} with nothing planned or committed -- the same "well-formed request, nothing to schedule" treatment an empty result gets for any other reason. An error was considered (an unrecognized channel_id could reasonably be a caller mistake worth surfacing loudly), but Run has no channel registry to validate against here -- only Tunarr does, via GetChannels, which Run never calls -- so rejecting it would mean guessing at "known channels" from a source Run doesn't otherwise consult.