Skip to main content

agent_tui_usecases/usecases/
shutdown.rs

1//! Shutdown use case.
2
3use std::sync::Arc;
4use std::sync::atomic::AtomicBool;
5use std::sync::atomic::Ordering;
6
7use crate::domain::ShutdownInput;
8use crate::domain::ShutdownOutput;
9use crate::usecases::ports::ShutdownNotifier;
10
11pub trait ShutdownUseCase: Send + Sync {
12    fn execute(&self, input: ShutdownInput) -> ShutdownOutput;
13}
14
15pub struct ShutdownUseCaseImpl {
16    shutdown_flag: Arc<AtomicBool>,
17    notifier: Arc<dyn ShutdownNotifier>,
18}
19
20impl ShutdownUseCaseImpl {
21    pub fn new(shutdown_flag: Arc<AtomicBool>, notifier: Arc<dyn ShutdownNotifier>) -> Self {
22        Self {
23            shutdown_flag,
24            notifier,
25        }
26    }
27}
28
29impl ShutdownUseCase for ShutdownUseCaseImpl {
30    fn execute(&self, _input: ShutdownInput) -> ShutdownOutput {
31        self.shutdown_flag.store(true, Ordering::SeqCst);
32        let acknowledged = self.notifier.notify().is_ok();
33
34        ShutdownOutput { acknowledged }
35    }
36}
37
38#[cfg(test)]
39#[path = "shutdown_tests.rs"]
40mod tests;