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