Scheduling
Internals of the Scheduling domain. For the public API, see Scheduling API.
The domain lives in CoreAPI under domain/scheduling/. It talks to PostgreSQL and, for sync, to the Castis Streamer
API. Routes are /api/v1/scheduling/:workspaceId/… behind ProtectedRS256 + RequireWorkspacePermission
(workspace:schedules:view for reads, workspace:schedules:manage for writes). Background:
Playout workspace auth.
Code layout¶
domain/scheduling/
├── config.go empty Config + ConfigFromEnv() (placeholder; no env vars)
├── models.go Program, ScheduleSlot (+ type/status constants)
├── helper.go ApplySlotFilters, DayWindow, ParseChannelIDs
├── validation.go CreateSlotRequest, UpdateSlotRequest validators, ParseID
├── sync.go SyncChannelSchedule: approved slots -> streamer PATCH
├── handlers/
│ ├── scope.go workspaceID/org from locals, channelInWorkspace (raw SQL), findMedia
│ ├── handler_schedule.go ListSlots, GetSlotByID, CreateSlot, UpdateSlot, DeleteSlot, SyncSchedule
│ └── handler_epg.go GetEPGGrid, GetChannelEPG, GetNowNext
└── routes/ wire.go (Setup, the only entry main calls) + routes.go
Wired in routes_platform.go: schedulingroutes.Setup(v1, db.GetDB(), rs256Validator). There are no environment
variables. Models are registered for AutoMigrate (Program, ScheduleSlot).
Data model¶
erDiagram
Program ||--o{ ScheduleSlot : "airs as"
Media ||--o{ Program : "wraps one"
Channel ||--o{ ScheduleSlot : "has (loose FK)"
Programwraps exactly oneMediafile and carries the EPG-facing title and description, so a file can be re-aired under different framing and history reads as "Program X aired" rather than a file name. Programs have no route of their own: they are created inline with a slot.ScheduleSlot.ChannelIDis a plain integer. The domain never imports the playback models' association; channel membership of the workspace is checked with raw SQL onchannels(scope.go). The EPG handlers do loadplayback.Channelto get name and SID.organization_idandworkspace_idare set from the authorized workspace, never from the body, and every query filters byworkspace_id.EndTimeis always computed:StartTime + Program.Media.Duration. The streamer can't enforce an arbitrary end, it plays a file to completion, so accepting an end time would be dishonest.- Slot
Typedrives priority when slots overlap at sync time (emergency > event_window > blackout ≈ promo) in the original design.SyncChannelScheduledoesn't implement that resolution today; it sends every approved slot.
Checks on create¶
- Channel, program and media must belong to the workspace (
400otherwise). - Media
Durationmust be known (400while processing). - Overlap:
channel_id = ? AND status != 'completed' AND start_time < newEnd AND end_time > newStart→409naming the conflicting slots. Done at creation, not deferred to sync. - Codec consistency (soft): if the new media has a
VideoCodec, every other non-completed slot's media with a codec must match on codec, profile, pixel format and audio codec, else400. Slots whose media has no codec data are skipped, so the check starts enforcing automatically once the normalization pipeline fills those fields.
UpdateSlot recomputes end_time when startTime changes but runs neither the overlap nor the codec check.
Status changes are not validated against a state machine.
Sync¶
POST /sync/:id calls scheduling.SyncChannelSchedule(db, workspaceID, channelID):
- Load the workspace's channel with
Origins.Streamer. Not found →ErrChannelNotInWorkspace(404). No origins → error (502). - Load
approvedslots, ordered by start, with program and media. Skip rows whose media is in another workspace. - Build
[{name: program.title, scheduled: "2006-01-02T15:04:05", items: [{filePath: media.StorageKey}]}].scheduledhas no zone offset and is formatted from the stored time as is. PatchStreamPlaylistto each origin's streamer for the channel'sScheduledPlaylistKey(scheduled, reserved at channel creation, see Playback internals). Full replace, so it is idempotent and self-healing. Any origin failure aggregates into one error (502).
Whether scheduled is interpreted by the streamer as UTC or local time is not verified here; the code sends the
UTC slot time without an offset.
Known issues and gaps¶
helper.goApplySlotFiltersfilters ongenreandtitle, columnsschedule_slotsno longer has. Thegenreandsearchquery parameters would fail with a database error (500), so the public docs omit them.- The
EPGhandlers' comment says the route param is:channelId; it is:id. - The grid and now/next endpoints return slots of every status, including
draft. - Planned, no code: blackout rules (region or subscriber tier), recurrence rules, XMLTV import, and publishing a
slot.activatedevent on NATS when a slot starts (SCHEDULING_NATS_ENABLEDexisted as a placeholder and was removed with the 2026-07-17 rewrite).
Connection to the Playback domain¶
Scheduling knows channels by ID and reads id, sid, name and workspace_id from channels. A channel plays
whether or not it has slots; the OTT client calls both domains independently: Playback for what to play, Scheduling
for what to show in the guide. A scheduled channel's reserved timedAlternativeUrl slot is what sync fills.
Change history¶
2026-07-17¶
- Domain rebuilt around
Program+ScheduleSlotwithtypeandstatus;sync.goadded; blackout and recurrence stubs removed; EPG route parameter renamed from:channelIdto:id.
2026-07-01¶
- Initial implementation: slot CRUD, EPG queries, overlap validation. The scheduling domain does not import the
playback models;
channelRowused explicitgorm:"column:…"tags soSIDdid not map tos_id. Fixed a duplicated time-parsing block inUpdateSlot.