feat(channel): add auto-schedule feature to channels with background scheduler

This commit is contained in:
2026-03-13 02:27:27 +01:00
parent dfd8f52a53
commit 1fc473342d
14 changed files with 161 additions and 6 deletions

View File

@@ -69,6 +69,7 @@ pub struct UpdateChannelRequest {
/// Replace the entire schedule config (template import/edit)
pub schedule_config: Option<domain::ScheduleConfig>,
pub recycle_policy: Option<domain::RecyclePolicy>,
pub auto_schedule: Option<bool>,
}
#[derive(Debug, Serialize)]
@@ -80,6 +81,7 @@ pub struct ChannelResponse {
pub timezone: String,
pub schedule_config: domain::ScheduleConfig,
pub recycle_policy: domain::RecyclePolicy,
pub auto_schedule: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
@@ -94,6 +96,7 @@ impl From<domain::Channel> for ChannelResponse {
timezone: c.timezone,
schedule_config: c.schedule_config,
recycle_policy: c.recycle_policy,
auto_schedule: c.auto_schedule,
created_at: c.created_at,
updated_at: c.updated_at,
}

View File

@@ -21,6 +21,7 @@ mod dto;
mod error;
mod extractors;
mod routes;
mod scheduler;
mod state;
use crate::config::Config;
@@ -72,6 +73,7 @@ async fn main() -> anyhow::Result<()> {
// Build media provider — Jellyfin if configured, no-op fallback otherwise.
let media_provider: Arc<dyn IMediaProvider> = build_media_provider(&config);
let bg_channel_repo = channel_repo.clone();
let schedule_engine = ScheduleEngineService::new(
Arc::clone(&media_provider),
channel_repo,
@@ -91,6 +93,9 @@ async fn main() -> anyhow::Result<()> {
cors_origins: config.cors_allowed_origins.clone(),
};
let bg_schedule_engine = Arc::clone(&state.schedule_engine);
tokio::spawn(scheduler::run_auto_scheduler(bg_schedule_engine, bg_channel_repo));
let app = Router::new()
.nest("/api/v1", routes::api_v1_router())
.with_state(state);

View File

@@ -76,6 +76,9 @@ pub(super) async fn update_channel(
if let Some(rp) = payload.recycle_policy {
channel.recycle_policy = rp;
}
if let Some(auto) = payload.auto_schedule {
channel.auto_schedule = auto;
}
channel.updated_at = Utc::now();
let channel = state.channel_service.update(channel).await?;

View File

@@ -0,0 +1,76 @@
//! Background auto-scheduler task.
//!
//! Runs every hour, finds channels with `auto_schedule = true`, and regenerates
//! their schedule if it is within 24 hours of expiry (or already expired).
use std::sync::Arc;
use std::time::Duration;
use chrono::Utc;
use domain::{ChannelRepository, ScheduleEngineService};
pub async fn run_auto_scheduler(
schedule_engine: Arc<ScheduleEngineService>,
channel_repo: Arc<dyn ChannelRepository>,
) {
loop {
tokio::time::sleep(Duration::from_secs(3600)).await;
tick(&schedule_engine, &channel_repo).await;
}
}
async fn tick(
schedule_engine: &Arc<ScheduleEngineService>,
channel_repo: &Arc<dyn ChannelRepository>,
) {
let channels = match channel_repo.find_auto_schedule_enabled().await {
Ok(c) => c,
Err(e) => {
tracing::warn!("auto-scheduler: failed to fetch channels: {}", e);
return;
}
};
let now = Utc::now();
for channel in channels {
let from = match schedule_engine.get_latest_schedule(channel.id).await {
Ok(Some(s)) => {
let remaining = s.valid_until - now;
if remaining > chrono::Duration::hours(24) {
// Still fresh — skip until it gets close to expiry
continue;
} else if s.valid_until > now {
// Seamless handoff: new schedule starts where the old one ends
s.valid_until
} else {
// Expired: start from now to avoid scheduling in the past
now
}
}
Ok(None) => now,
Err(e) => {
tracing::warn!(
"auto-scheduler: failed to fetch latest schedule for channel {}: {}",
channel.id,
e
);
continue;
}
};
if let Err(e) = schedule_engine.generate_schedule(channel.id, from).await {
tracing::warn!(
"auto-scheduler: failed to generate schedule for channel {}: {}",
channel.id,
e
);
} else {
tracing::info!(
"auto-scheduler: generated schedule for channel {} starting at {}",
channel.id,
from
);
}
}
}