Added the watch and scheduler systems
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user