Added the watch and scheduler systems

This commit is contained in:
Connor Johnstone
2026-03-20 16:28:15 -04:00
parent eaaff5f98f
commit 9d6c0e31c1
16 changed files with 948 additions and 164 deletions
+40
View File
@@ -33,6 +33,7 @@ struct ArtistListItem {
id: i32,
name: String,
musicbrainz_id: Option<String>,
monitored: bool,
total_watched: usize,
total_owned: usize,
total_items: usize,
@@ -72,6 +73,11 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
.route(web::post().to(add_artist)),
)
.service(web::resource("/artists/{id}/full").route(web::get().to(get_artist_full)))
.service(
web::resource("/artists/{id}/monitor")
.route(web::post().to(set_monitored))
.route(web::delete().to(unset_monitored)),
)
.service(
web::resource("/artists/{id}")
.route(web::get().to(get_artist))
@@ -121,6 +127,7 @@ async fn list_artists(
id: a.id,
name: a.name.clone(),
musicbrainz_id: a.musicbrainz_id.clone(),
monitored: a.monitored,
total_watched,
total_owned,
total_items,
@@ -324,6 +331,8 @@ pub async fn enrich_artist(
added_at: chrono::Utc::now().naive_utc(),
top_songs: "[]".to_string(),
similar_artists: "[]".to_string(),
monitored: false,
last_checked_at: None,
};
(synthetic, None, mbid)
}
@@ -603,6 +612,7 @@ pub async fn enrich_artist(
"total_watched_tracks": total_artist_watched,
"total_owned_tracks": total_artist_owned,
"enriched": !skip_track_fetch,
"monitored": artist.monitored,
"artist_info": artist_info,
"artist_photo": artist_photo,
"artist_bio": artist_bio,
@@ -800,3 +810,33 @@ async fn delete_artist(
queries::artists::delete(state.db.conn(), id).await?;
Ok(HttpResponse::NoContent().finish())
}
async fn set_monitored(
state: web::Data<AppState>,
session: Session,
path: web::Path<i32>,
) -> Result<HttpResponse, ApiError> {
auth::require_auth(&session)?;
let id = path.into_inner();
let artist = queries::artists::set_monitored(state.db.conn(), id, true).await?;
Ok(HttpResponse::Ok().json(serde_json::json!({
"id": artist.id,
"name": artist.name,
"monitored": artist.monitored,
})))
}
async fn unset_monitored(
state: web::Data<AppState>,
session: Session,
path: web::Path<i32>,
) -> Result<HttpResponse, ApiError> {
auth::require_auth(&session)?;
let id = path.into_inner();
let artist = queries::artists::set_monitored(state.db.conn(), id, false).await?;
Ok(HttpResponse::Ok().json(serde_json::json!({
"id": artist.id,
"name": artist.name,
"monitored": artist.monitored,
})))
}
+59 -152
View File
@@ -20,6 +20,8 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
.service(web::resource("/tasks/{id}").route(web::get().to(get_task)))
.service(web::resource("/watchlist").route(web::get().to(list_watchlist)))
.service(web::resource("/watchlist/{id}").route(web::delete().to(remove_watchlist)))
.service(web::resource("/monitor/check").route(web::post().to(trigger_monitor_check)))
.service(web::resource("/monitor/status").route(web::get().to(get_monitor_status)))
.service(
web::resource("/config")
.route(web::get().to(get_config))
@@ -48,6 +50,14 @@ async fn get_status(
let needs_tagging = queries::tracks::get_needing_metadata(state.db.conn()).await?;
// Scheduled task info
let sched = state.scheduler.lock().await;
let scheduled_tasks = serde_json::json!({
"next_pipeline": sched.next_pipeline,
"next_monitor": sched.next_monitor,
});
drop(sched);
Ok(HttpResponse::Ok().json(serde_json::json!({
"library": summary,
"queue": {
@@ -61,6 +71,7 @@ async fn get_status(
"items": needs_tagging.iter().take(20).collect::<Vec<_>>(),
},
"tasks": tasks,
"scheduled": scheduled_tasks,
})))
}
@@ -173,158 +184,7 @@ async fn trigger_pipeline(
session: Session,
) -> Result<HttpResponse, ApiError> {
auth::require_auth(&session)?;
let sync_id = state.tasks.register_pending("sync");
let download_id = state.tasks.register_pending("download");
let index_id = state.tasks.register_pending("index");
let tag_id = state.tasks.register_pending("tag");
let organize_id = state.tasks.register_pending("organize");
let enrich_id = state.tasks.register_pending("enrich");
let task_ids = vec![
sync_id.clone(),
download_id.clone(),
index_id.clone(),
tag_id.clone(),
organize_id.clone(),
enrich_id.clone(),
];
let state = state.clone();
tokio::spawn(async move {
let cfg = state.config.read().await.clone();
// Step 1: Sync
state.tasks.start(&sync_id);
state
.tasks
.update_progress(&sync_id, 0, 0, "Syncing watchlist to download queue...");
match shanty_dl::sync_wanted_to_queue(state.db.conn(), false).await {
Ok(stats) => state.tasks.complete(&sync_id, format!("{stats}")),
Err(e) => state.tasks.fail(&sync_id, e.to_string()),
}
// Step 2: Download
state.tasks.start(&download_id);
let cookies = cfg.download.cookies_path.clone();
let format: shanty_dl::AudioFormat = cfg
.download
.format
.parse()
.unwrap_or(shanty_dl::AudioFormat::Opus);
let source: shanty_dl::SearchSource = cfg
.download
.search_source
.parse()
.unwrap_or(shanty_dl::SearchSource::YouTubeMusic);
let rate = if cookies.is_some() {
cfg.download.rate_limit_auth
} else {
cfg.download.rate_limit
};
let backend = shanty_dl::YtDlpBackend::new(rate, source, cookies.clone());
let backend_config = shanty_dl::BackendConfig {
output_dir: cfg.download_path.clone(),
format,
cookies_path: cookies,
};
let task_state = state.clone();
let progress_tid = download_id.clone();
let on_progress: shanty_dl::ProgressFn = Box::new(move |current, total, msg| {
task_state
.tasks
.update_progress(&progress_tid, current, total, msg);
});
match shanty_dl::run_queue_with_progress(
state.db.conn(),
&backend,
&backend_config,
false,
Some(on_progress),
)
.await
{
Ok(stats) => {
let _ = queries::cache::purge_prefix(state.db.conn(), "artist_totals:").await;
state.tasks.complete(&download_id, format!("{stats}"));
}
Err(e) => state.tasks.fail(&download_id, e.to_string()),
}
// Step 3: Index
state.tasks.start(&index_id);
state
.tasks
.update_progress(&index_id, 0, 0, "Scanning library...");
let scan_config = shanty_index::ScanConfig {
root: cfg.library_path.clone(),
dry_run: false,
concurrency: cfg.indexing.concurrency,
};
match shanty_index::run_scan(state.db.conn(), &scan_config).await {
Ok(stats) => state.tasks.complete(&index_id, format!("{stats}")),
Err(e) => state.tasks.fail(&index_id, e.to_string()),
}
// Step 4: Tag
state.tasks.start(&tag_id);
state
.tasks
.update_progress(&tag_id, 0, 0, "Tagging tracks...");
match shanty_tag::MusicBrainzClient::new() {
Ok(mb) => {
let tag_config = shanty_tag::TagConfig {
dry_run: false,
write_tags: cfg.tagging.write_tags,
confidence: cfg.tagging.confidence,
};
match shanty_tag::run_tagging(state.db.conn(), &mb, &tag_config, None).await {
Ok(stats) => state.tasks.complete(&tag_id, format!("{stats}")),
Err(e) => state.tasks.fail(&tag_id, e.to_string()),
}
}
Err(e) => state.tasks.fail(&tag_id, e.to_string()),
}
// Step 5: Organize
state.tasks.start(&organize_id);
state
.tasks
.update_progress(&organize_id, 0, 0, "Organizing files...");
let org_config = shanty_org::OrgConfig {
target_dir: cfg.library_path.clone(),
format: cfg.organization_format.clone(),
dry_run: false,
copy: false,
};
match shanty_org::organize_from_db(state.db.conn(), &org_config).await {
Ok(stats) => {
let promoted = queries::wanted::promote_downloaded_to_owned(state.db.conn())
.await
.unwrap_or(0);
let msg = if promoted > 0 {
format!("{stats}{promoted} items marked as owned")
} else {
format!("{stats}")
};
state.tasks.complete(&organize_id, msg);
}
Err(e) => state.tasks.fail(&organize_id, e.to_string()),
}
// Step 6: Enrich
state.tasks.start(&enrich_id);
state
.tasks
.update_progress(&enrich_id, 0, 0, "Refreshing artist data...");
match enrich_all_watched_artists(&state).await {
Ok(count) => state
.tasks
.complete(&enrich_id, format!("{count} artists refreshed")),
Err(e) => state.tasks.fail(&enrich_id, e.to_string()),
}
});
let task_ids = crate::pipeline::spawn_pipeline(&state);
Ok(HttpResponse::Accepted().json(serde_json::json!({ "task_ids": task_ids })))
}
@@ -361,6 +221,53 @@ async fn remove_watchlist(
Ok(HttpResponse::NoContent().finish())
}
async fn trigger_monitor_check(
state: web::Data<AppState>,
session: Session,
) -> Result<HttpResponse, ApiError> {
auth::require_admin(&session)?;
let state = state.clone();
let task_id = state.tasks.register("monitor_check");
let tid = task_id.clone();
tokio::spawn(async move {
state
.tasks
.update_progress(&tid, 0, 0, "Checking monitored artists...");
match crate::monitor::check_monitored_artists(&state).await {
Ok(stats) => state.tasks.complete(
&tid,
format!(
"{} artists checked, {} new releases, {} tracks added",
stats.artists_checked, stats.new_releases_found, stats.tracks_added
),
),
Err(e) => state.tasks.fail(&tid, e.to_string()),
}
});
Ok(HttpResponse::Accepted().json(serde_json::json!({ "task_id": task_id })))
}
async fn get_monitor_status(
state: web::Data<AppState>,
session: Session,
) -> Result<HttpResponse, ApiError> {
auth::require_auth(&session)?;
let monitored = queries::artists::list_monitored(state.db.conn()).await?;
let items: Vec<serde_json::Value> = monitored
.iter()
.map(|a| {
serde_json::json!({
"id": a.id,
"name": a.name,
"musicbrainz_id": a.musicbrainz_id,
"monitored": a.monitored,
"last_checked_at": a.last_checked_at,
})
})
.collect();
Ok(HttpResponse::Ok().json(items))
}
async fn get_config(
state: web::Data<AppState>,
session: Session,