feat(server): fix the sync scheduler for sources

This commit is contained in:
perf3ct 2025-06-15 18:05:56 +00:00
parent cebae12363
commit a39fc807fa
3 changed files with 43 additions and 12 deletions

View File

@ -36,6 +36,7 @@ pub struct AppState {
pub db: Database, pub db: Database,
pub config: Config, pub config: Config,
pub webdav_scheduler: Option<std::sync::Arc<webdav_scheduler::WebDAVScheduler>>, pub webdav_scheduler: Option<std::sync::Arc<webdav_scheduler::WebDAVScheduler>>,
pub source_scheduler: Option<std::sync::Arc<source_scheduler::SourceScheduler>>,
} }
/// Health check endpoint for monitoring /// Health check endpoint for monitoring

View File

@ -135,6 +135,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
db: web_db, db: web_db,
config: config.clone(), config: config.clone(),
webdav_scheduler: None, // Will be set after creating scheduler webdav_scheduler: None, // Will be set after creating scheduler
source_scheduler: None, // Will be set after creating scheduler
}; };
let web_state = Arc::new(web_state); let web_state = Arc::new(web_state);
@ -143,6 +144,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
db: background_db, db: background_db,
config: config.clone(), config: config.clone(),
webdav_scheduler: None, webdav_scheduler: None,
source_scheduler: None,
}; };
let background_state = Arc::new(background_state); let background_state = Arc::new(background_state);
@ -220,6 +222,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
db: web_state.db.clone(), db: web_state.db.clone(),
config: web_state.config.clone(), config: web_state.config.clone(),
webdav_scheduler: Some(webdav_scheduler.clone()), webdav_scheduler: Some(webdav_scheduler.clone()),
source_scheduler: Some(source_scheduler.clone()),
}; };
let web_state = Arc::new(updated_web_state); let web_state = Arc::new(updated_web_state);

View File

@ -7,6 +7,7 @@ use axum::{
}; };
use std::sync::Arc; use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
use tracing::error;
use crate::{ use crate::{
auth::AuthUser, auth::AuthUser,
@ -262,26 +263,52 @@ async fn trigger_sync(
.await .await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
// Trigger the appropriate sync based on source type // Trigger sync using the universal source scheduler
match source.source_type { if let Some(scheduler) = &state.source_scheduler {
crate::models::SourceType::WebDAV => { if let Err(e) = scheduler.trigger_sync(source_id).await {
// Send a message to trigger WebDAV sync error!("Failed to trigger sync for source {}: {}", source_id, e);
if let Some(scheduler) = &state.webdav_scheduler {
scheduler.trigger_sync(source_id).await;
}
}
_ => {
// Other source types not implemented yet
state state
.db .db
.update_source_status( .update_source_status(
source_id, source_id,
crate::models::SourceStatus::Error, crate::models::SourceStatus::Error,
Some("Source type not implemented".to_string()), Some(format!("Failed to trigger sync: {}", e)),
) )
.await .await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
return Err(StatusCode::NOT_IMPLEMENTED); return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
} else {
// Fallback to WebDAV scheduler for backward compatibility
match source.source_type {
crate::models::SourceType::WebDAV => {
if let Some(webdav_scheduler) = &state.webdav_scheduler {
webdav_scheduler.trigger_sync(source_id).await;
} else {
state
.db
.update_source_status(
source_id,
crate::models::SourceStatus::Error,
Some("No scheduler available".to_string()),
)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
return Err(StatusCode::INTERNAL_SERVER_ERROR);
}
}
_ => {
state
.db
.update_source_status(
source_id,
crate::models::SourceStatus::Error,
Some("Source type not supported".to_string()),
)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
return Err(StatusCode::NOT_IMPLEMENTED);
}
} }
} }