use serde::Deserialize;
use crate::error::AppError;
use crate::global_lock::LockScope;
pub use crate::global_lock::LockStatus;
pub use crate::routines::RoutineStore;
#[derive(Deserialize, utoipa::ToSchema)]
pub struct LockRequest {
pub scope: String,
}
fn parse_scope(scope: &str) -> Result<LockScope, AppError> {
match scope {
"shared" => Ok(LockScope::Shared),
"local" => Ok(LockScope::Local),
other => Err(AppError::BadRequest(format!(
"unknown scope {other:?}; use \"shared\" or \"local\""
))),
}
}
pub fn build(store: &RoutineStore, scope: &str) -> Result<LockStatus, AppError> {
let lock_scope = parse_scope(scope)?;
crate::global_lock::set_lock(lock_scope, true).map_err(|_| AppError::Internal)?;
if let Err(sync_err) = crate::sync::routines::sync_routines_to_crontab(store) {
log::warn!("crontab sync after lock failed: {sync_err}");
}
Ok(crate::global_lock::lock_status())
}
#[cfg(test)]
#[path = "logic_tests.rs"]
mod logic_tests;