ctb_workspace_ipc/services/process/
channel.rs1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use tokio::sync::{mpsc, oneshot};
30
31use crate::error::Error;
32use crate::services::process::api::{
33 ProcessService, ShutdownTreeRequest, ShutdownTreeResponse,
34};
35
36#[derive(Debug)]
38pub struct ShutdownChannelProcessService {
39 shutdown_tx: mpsc::Sender<Option<String>>,
40}
41
42impl ShutdownChannelProcessService {
43 pub fn new(shutdown_tx: mpsc::Sender<Option<String>>) -> Self {
45 Self { shutdown_tx }
46 }
47}
48
49#[async_trait::async_trait]
50impl ProcessService for ShutdownChannelProcessService {
51 async fn shutdown_tree(
52 &self,
53 request: ShutdownTreeRequest,
54 ) -> Result<ShutdownTreeResponse, Error> {
55 let _ = self.shutdown_tx.send(request.reason).await;
56 Ok(ShutdownTreeResponse { acknowledged: true })
57 }
58
59 async fn shutdown_own_tree(
60 &self,
61 request: ShutdownTreeRequest,
62 ) -> Result<ShutdownTreeResponse, Error> {
63 let _ = self.shutdown_tx.send(request.reason).await;
64 Ok(ShutdownTreeResponse { acknowledged: true })
65 }
66}
67
68#[derive(Debug)]
75pub struct OneshotShutdownProcessService {
76 shutdown_tx: tokio::sync::Mutex<Option<oneshot::Sender<Option<String>>>>,
77}
78
79impl OneshotShutdownProcessService {
80 pub fn new(shutdown_tx: oneshot::Sender<Option<String>>) -> Self {
82 Self {
83 shutdown_tx: tokio::sync::Mutex::new(Some(shutdown_tx)),
84 }
85 }
86}
87
88#[async_trait::async_trait]
89impl ProcessService for OneshotShutdownProcessService {
90 async fn shutdown_tree(
91 &self,
92 request: ShutdownTreeRequest,
93 ) -> Result<ShutdownTreeResponse, Error> {
94 let mut guard = self.shutdown_tx.lock().await;
95 if let Some(tx) = guard.take() {
96 let _ = tx.send(request.reason);
97 }
98 Ok(ShutdownTreeResponse { acknowledged: true })
99 }
100
101 async fn shutdown_own_tree(
102 &self,
103 request: ShutdownTreeRequest,
104 ) -> Result<ShutdownTreeResponse, Error> {
105 let mut guard = self.shutdown_tx.lock().await;
106 if let Some(tx) = guard.take() {
107 let _ = tx.send(request.reason);
108 }
109 Ok(ShutdownTreeResponse { acknowledged: true })
110 }
111}