hyprstream_service/service/manager/mod.rs
1//! Service lifecycle management
2//!
3//! Provides a unified trait for managing hyprstream services across different
4//! platforms and init systems.
5
6use anyhow::Result;
7use async_trait::async_trait;
8
9use crate::service::spawner::{Spawnable, SpawnedService};
10
11pub mod units;
12
13#[cfg(feature = "systemd")]
14pub mod systemd;
15
16pub mod standalone;
17
18/// Which D-Bus / systemd scope to target for service installation.
19#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
20pub enum ServiceTarget {
21 /// User daemon via the systemd private bus (session-independent, default).
22 ///
23 /// Connects to `$XDG_RUNTIME_DIR/systemd/private/bus`. Works on headless
24 /// machines and survives logout when user linger is enabled. Correct for
25 /// all long-lived public service daemons.
26 #[default]
27 User,
28
29 /// User daemon via the D-Bus session bus (`$DBUS_SESSION_BUS_ADDRESS`).
30 ///
31 /// Session-scoped: the unit is managed by the session's systemd user
32 /// instance and only while the session is active. Use only for
33 /// session-local services such as the TUI multiplexer.
34 UserSession,
35
36 /// System daemon in `/etc/systemd/system/` (requires root).
37 System,
38}
39
40/// Service lifecycle management trait
41///
42/// Abstracts over systemd (Linux) vs standalone (process spawn) for service management.
43#[async_trait]
44pub trait ServiceManager: Send + Sync {
45 /// Install unit files (idempotent)
46 ///
47 /// For systemd, this writes socket/service unit files to ~/.config/systemd/user/
48 /// For standalone mode, this is a no-op.
49 async fn install(&self, service: &str) -> Result<()>;
50
51 /// Uninstall unit files
52 ///
53 /// Stops the service, disables units, and removes unit files.
54 async fn uninstall(&self, service: &str) -> Result<()>;
55
56 /// Start a service
57 async fn start(&self, service: &str) -> Result<()>;
58
59 /// Stop a service
60 async fn stop(&self, service: &str) -> Result<()>;
61
62 /// Check if service is running
63 async fn is_active(&self, service: &str) -> Result<bool>;
64
65 /// Reload daemon configuration
66 ///
67 /// For systemd, this calls `daemon-reload`.
68 /// For standalone mode, this is a no-op.
69 async fn reload(&self) -> Result<()>;
70
71 /// Enable a service for autostart at boot (systemctl enable)
72 ///
73 /// For systemd, this calls `enable_unit_files` so the unit starts at boot.
74 /// For standalone mode, this is a no-op.
75 async fn enable(&self, _service: &str) -> Result<()> {
76 Ok(())
77 }
78
79 /// Ensure service is available (install + start if needed)
80 ///
81 /// This is the main entry point for CLI commands that need services.
82 async fn ensure(&self, service: &str) -> Result<()> {
83 self.install(service).await?;
84 if !self.is_active(service).await? {
85 self.start(service).await?;
86 }
87 Ok(())
88 }
89
90 /// Spawn a service (unified API for inproc and systemd)
91 ///
92 /// For inproc mode, spawns in current process.
93 /// For systemd mode, ensures systemd unit is running.
94 async fn spawn(&self, service: Box<dyn Spawnable>) -> Result<SpawnedService>;
95}
96
97/// Detect best available service manager (user private bus by default).
98pub async fn detect() -> Result<Box<dyn ServiceManager>> {
99 detect_with_mode(ServiceTarget::User).await
100}
101
102/// Detect best available service manager for the given target scope.
103pub async fn detect_with_mode(target: ServiceTarget) -> Result<Box<dyn ServiceManager>> {
104 #[cfg(feature = "systemd")]
105 {
106 if hyprstream_rpc::has_systemd() {
107 return match target {
108 ServiceTarget::System => Ok(Box::new(systemd::SystemdSystemManager::new().await?)),
109 ServiceTarget::UserSession => {
110 Ok(Box::new(systemd::SystemdManager::new_session().await?))
111 }
112 ServiceTarget::User => Ok(Box::new(systemd::SystemdManager::new().await?)),
113 };
114 }
115 }
116 let _ = target;
117 Ok(Box::new(standalone::StandaloneManager::new()))
118}
119
120// Re-exports
121pub use standalone::StandaloneManager;
122#[cfg(feature = "systemd")]
123pub use systemd::{SystemdManager, SystemdSystemManager};