ctb_workspace_ipc/services/process/
api.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//! Process service API definitions.
21//!
22//! This service provides operations that any process can handle, such as
23//! graceful shutdown of its process tree.
24
25#[allow(
26    unused_imports,
27    clippy::wildcard_imports,
28    reason = "Standard workspace module prelude"
29)]
30use crate::utilities::*;
31
32use anyhow::Result;
33use async_trait::async_trait;
34use serde::{Deserialize, Serialize};
35
36/// Method name for `shutdown_tree` - shuts down the entire application.
37pub const METHOD_SHUTDOWN_TREE: &str = "shutdown_tree";
38
39/// Method name for `shutdown_own_tree` - shuts down only the calling process
40/// and its descendants.
41pub const METHOD_SHUTDOWN_OWN_TREE: &str = "shutdown_own_tree";
42
43/// Request to shutdown a process and its children.
44#[derive(Debug, Clone, Default, Serialize, Deserialize)]
45pub struct ShutdownTreeRequest {
46    /// Optional reason for shutdown (for logging/audit).
47    pub reason: Option<String>,
48}
49
50/// Response from `shutdown_tree`.
51#[derive(Debug, Clone, Default, Serialize, Deserialize)]
52pub struct ShutdownTreeResponse {
53    /// Whether the shutdown was acknowledged.
54    pub acknowledged: bool,
55}
56
57/// Trait for process-level service operations.
58///
59/// Any process that participates in the IPC system should implement this
60/// trait to handle graceful shutdown requests.
61///
62/// Two distinct shutdown operations are provided:
63/// - `shutdown_tree`: Shuts down the entire application/workspace. This is a
64///   privileged operation typically reserved for workspace-level processes.
65/// - `shutdown_own_tree`: Shuts down only the calling process and its owned
66///   descendants. This allows processes to terminate themselves and their
67///   children without affecting the entire application.
68#[async_trait]
69pub trait ProcessService: Send + Sync + std::fmt::Debug {
70    /// Request graceful shutdown of the entire application/workspace.
71    ///
72    /// This is a privileged operation that triggers a full application
73    /// shutdown. Only processes with appropriate capabilities should be
74    /// allowed to invoke this method.
75    async fn shutdown_tree(
76        &self,
77        request: ShutdownTreeRequest,
78    ) -> Result<ShutdownTreeResponse, crate::error::Error>;
79
80    /// Request graceful shutdown of the calling process and its descendants.
81    ///
82    /// This allows a process to shut down itself and any child processes it
83    /// owns, without affecting other parts of the application. This is the
84    /// standard shutdown operation for most processes.
85    async fn shutdown_own_tree(
86        &self,
87        request: ShutdownTreeRequest,
88    ) -> Result<ShutdownTreeResponse, crate::error::Error>;
89}
90
91#[derive(Debug)]
92pub struct MockProcessService;
93
94#[async_trait]
95impl ProcessService for MockProcessService {
96    async fn shutdown_tree(
97        &self,
98        _request: ShutdownTreeRequest,
99    ) -> Result<ShutdownTreeResponse, crate::error::Error> {
100        Ok(ShutdownTreeResponse { acknowledged: true })
101    }
102
103    async fn shutdown_own_tree(
104        &self,
105        _request: ShutdownTreeRequest,
106    ) -> Result<ShutdownTreeResponse, crate::error::Error> {
107        Ok(ShutdownTreeResponse { acknowledged: true })
108    }
109}