ctb_workspace_ipc/services/process/
channel.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Channel-based implementations for the process service.
21
22#[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/// Process service implementation that forwards shutdown requests to a channel.
37#[derive(Debug)]
38pub struct ShutdownChannelProcessService {
39    shutdown_tx: mpsc::Sender<Option<String>>,
40}
41
42impl ShutdownChannelProcessService {
43    /// Create a new process service with the given shutdown channel.
44    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/// Process service implementation that forwards shutdown requests to a oneshot
69/// channel.
70///
71/// This is useful for subprocess implementations that only expect a single
72/// shutdown signal. Both `shutdown_tree` and `shutdown_own_tree` send to the
73/// same oneshot channel, which can only be used once.
74#[derive(Debug)]
75pub struct OneshotShutdownProcessService {
76    shutdown_tx: tokio::sync::Mutex<Option<oneshot::Sender<Option<String>>>>,
77}
78
79impl OneshotShutdownProcessService {
80    /// Create a new process service with the given oneshot shutdown channel.
81    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}