Mountain/ApplicationState/Internal/Recovery/RecoverState/
SafeStateOperationWithTimeout.rs1#![allow(non_snake_case)]
2
3use CommonLibrary::Error::CommonError::CommonError;
9
10use crate::dev_log;
11
12pub fn Fn<T, F>(Operation:F, TimeoutMs:u64, OperationName:&str) -> Result<T, CommonError>
13where
14 F: FnOnce() -> Result<T, CommonError> + Send + 'static,
15 T: Send + 'static, {
16 let (Sender, Receiver) = std::sync::mpsc::channel();
17
18 std::thread::spawn(move || {
19 let _ = Sender.send(Operation());
20 });
21
22 match Receiver.recv_timeout(std::time::Duration::from_millis(TimeoutMs)) {
23 Ok(Result) => Result,
24 Err(_) => {
25 dev_log!(
26 "lifecycle",
27 "error: [RecoverState] Operation '{}' timed out after {}ms",
28 OperationName,
29 TimeoutMs
30 );
31 Err(CommonError::Unknown { Description:format!("Operation '{}' timed out", OperationName) })
32 },
33 }
34}