ctb_workspace/
environment.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//! Environment capture IPC endpoints hosted by the workspace supervisor.
21//!
22//! Subprocesses query these methods via IPC so that only the main workspace
23//! process performs external network discovery and maintains the cache.
24
25#[allow(
26    unused_imports,
27    clippy::wildcard_imports,
28    reason = "Standard workspace module prelude"
29)]
30use crate::utilities::*;
31
32pub use ctb_io_environment::EnvDescription;
33
34/// Capture a full snapshot of the execution environment on the workspace supervisor.
35///
36/// # Errors
37/// Returns an error if environment capture fails.
38#[ipc_method]
39pub async fn capture() -> Result<EnvDescription> {
40    if let Some(cached) = ctb_io_environment::get_boot_cached_env() {
41        return Ok((*cached).clone());
42    }
43    tokio::task::spawn_blocking(|| (*ctb_io_environment::init_boot_env()).clone())
44        .await
45        .context("Environment capture worker panicked")
46}
47
48/// Capture a quick snapshot of the execution environment on the workspace supervisor,
49/// returning cached IP/time information if populated at boot.
50///
51/// # Errors
52/// Returns an error if environment capture fails.
53#[ipc_method]
54pub async fn capture_quick() -> Result<EnvDescription> {
55    Ok(ctb_io_environment::capture_quick())
56}
57
58/// Clear cached IP and timestamp offset values on the workspace supervisor and
59/// immediately refill them.
60///
61/// # Errors
62/// Returns an error if cache refresh or background task fails.
63#[ipc_method]
64pub async fn env_cache_reset() -> Result<()> {
65    tokio::task::spawn_blocking(ctb_io_environment::env_cache_reset)
66        .await
67        .context("env_cache_reset worker thread panicked")?;
68    Ok(())
69}