#![allow(
clippy::wildcard_imports,
clippy::too_many_arguments,
clippy::needless_pass_by_value,
dead_code,
reason = "split-out module preserves existing code while keeping files under the linecheck limit"
)]
use super::*;
pub(crate) const ROUTINE_LINE_MARKER: &str = "# moadim-routine:";
pub fn sync_routines_to_crontab(store: &RoutineStore) -> Result<(), SyncError> {
let on_multi_thread_runtime = tokio::runtime::Handle::try_current()
.is_ok_and(|handle| handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::MultiThread);
let result = if on_multi_thread_runtime {
tokio::task::block_in_place(|| sync_routines_to_crontab_blocking(store))
} else {
sync_routines_to_crontab_blocking(store)
};
match &result {
Ok(()) => crate::sync::record_crontab_sync_success(),
Err(err) => crate::sync::record_crontab_sync_failure(err),
}
result
}
pub(crate) fn sync_routines_to_crontab_blocking(store: &RoutineStore) -> Result<(), SyncError> {
let _crontab_guard = crontab_sync_lock().lock_recover();
let current = read_crontab()?;
if store.lock_recover().is_empty() && current.contains(ROUTINE_LINE_MARKER) {
log::warn!(
"routine sync: store is empty but the crontab still has routine lines; refusing to \
wipe the routines block (suspected load failure or a concurrent daemon)"
);
return Ok(());
}
let block = build_block(store);
let new_crontab = replace_block_with(¤t, &block, BLOCK_BEGIN, BLOCK_END);
if new_crontab == current {
return Ok(());
}
write_crontab(&new_crontab)
}