Skip to content

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)"
  • Program wraps exactly one Media file 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.ChannelID is a plain integer. The domain never imports the playback models' association; channel membership of the workspace is checked with raw SQL on channels (scope.go). The EPG handlers do load playback.Channel to get name and SID.
  • organization_id and workspace_id are set from the authorized workspace, never from the body, and every query filters by workspace_id.
  • EndTime is 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 Type drives priority when slots overlap at sync time (emergency > event_window > blackout ≈ promo) in the original design. SyncChannelSchedule doesn't implement that resolution today; it sends every approved slot.

Checks on create

  1. Channel, program and media must belong to the workspace (400 otherwise).
  2. Media Duration must be known (400 while processing).
  3. Overlap: channel_id = ? AND status != 'completed' AND start_time < newEnd AND end_time > newStart → 409 naming the conflicting slots. Done at creation, not deferred to sync.
  4. 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, else 400. 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):

  1. Load the workspace's channel with Origins.Streamer. Not found → ErrChannelNotInWorkspace (404). No origins → error (502).
  2. Load approved slots, ordered by start, with program and media. Skip rows whose media is in another workspace.
  3. Build [{name: program.title, scheduled: "2006-01-02T15:04:05", items: [{filePath: media.StorageKey}]}]. scheduled has no zone offset and is formatted from the stored time as is.
  4. PatchStreamPlaylist to each origin's streamer for the channel's ScheduledPlaylistKey (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.go ApplySlotFilters filters on genre and title, columns schedule_slots no longer has. The genre and search query parameters would fail with a database error (500), so the public docs omit them.
  • The EPG handlers' 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.activated event on NATS when a slot starts (SCHEDULING_NATS_ENABLED existed 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 + ScheduleSlot with type and status; sync.go added; blackout and recurrence stubs removed; EPG route parameter renamed from :channelId to :id.

2026-07-01

  • Initial implementation: slot CRUD, EPG queries, overlap validation. The scheduling domain does not import the playback models; channelRow used explicit gorm:"column:…" tags so SID did not map to s_id. Fixed a duplicated time-parsing block in UpdateSlot.