pmdaemon/lib.rs
1//! # PMDaemon - Advanced Process Manager
2//!
3//! PMDaemon - A high-performance, cross-platform process manager built in Rust with advanced capabilities.
4//! PMDaemon is designed as a general-purpose process manager with innovative features for
5//! modern application deployment and monitoring.
6//!
7//! ## Key Features
8//!
9//! ### Core Process Management
10//! - **Process lifecycle management** - Start, stop, restart, reload, delete operations
11//! - **Clustering support** - Run multiple instances with automatic load balancing
12//! - **Auto-restart** - Automatic restart on crashes with configurable limits
13//! - **Signal handling** - Graceful shutdown with SIGTERM/SIGINT and custom signals
14//! - **Configuration persistence** - Process configs saved and restored between sessions
15//!
16//! ### Advanced Monitoring
17//! - **Real-time monitoring** - CPU, memory, uptime tracking with system metrics
18//! - **Memory limit enforcement** - Automatic restart when processes exceed memory limits
19//! - **Process health checks** - Continuous monitoring with automatic failure detection
20//! - **Log management** - Separate stdout/stderr files with viewing and following
21//!
22//! ### Innovative Port Management (Beyond PM2)
23//! - **Port range distribution** - Automatically distribute consecutive ports to cluster instances
24//! - **Auto-assignment from ranges** - Find first available port in specified range
25//! - **Built-in conflict detection** - Prevent port conflicts at the process manager level
26//! - **Runtime port overrides** - Change ports during restart without modifying saved config
27//! - **Port visibility** - Display assigned ports in process listings
28//!
29//! ### Web API & Real-time Updates
30//! - **Comprehensive REST API** - Full process management via HTTP with PM2-compatible responses
31//! - **Real-time WebSocket updates** - Live process status and system metrics streaming
32//! - **Production-ready web server** - Built on Axum with CORS and security headers
33//!
34//! ## Library Usage
35//!
36//! ### Basic Process Management
37//!
38//! ```rust,no_run
39//! use pmdaemon::{ProcessManager, ProcessConfig};
40//! use tokio;
41//!
42//! #[tokio::main]
43//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
44//! let mut manager = ProcessManager::new().await?;
45//!
46//! // Start a simple process
47//! let config = ProcessConfig::builder()
48//! .name("my-app")
49//! .script("node")
50//! .args(vec!["server.js"])
51//! .build()?;
52//!
53//! let process_id = manager.start(config).await?;
54//! println!("Started process with ID: {}", process_id);
55//!
56//! // List all processes
57//! let processes = manager.list().await?;
58//! for process in processes {
59//! println!("Process: {} ({})", process.name, process.state);
60//! }
61//!
62//! Ok(())
63//! }
64//! ```
65//!
66//! ### Advanced Features - Clustering with Port Management
67//!
68//! ```rust,no_run
69//! use pmdaemon::{ProcessManager, ProcessConfig, config::PortConfig};
70//!
71//! #[tokio::main]
72//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
73//! let mut manager = ProcessManager::new().await?;
74//!
75//! // Start a cluster with port range distribution
76//! let config = ProcessConfig::builder()
77//! .name("web-cluster")
78//! .script("node")
79//! .args(vec!["app.js"])
80//! .instances(4)
81//! .port(PortConfig::Range(3000, 3003)) // Ports 3000-3003
82//! .max_memory_restart(512 * 1024 * 1024) // 512MB limit
83//! .build()?;
84//!
85//! manager.start(config).await?;
86//! println!("Started 4-instance cluster on ports 3000-3003");
87//!
88//! Ok(())
89//! }
90//! ```
91//!
92//! ## CLI Usage
93//!
94//! ```bash
95//! # Basic process management
96//! pmdaemon start app.js --name myapp
97//! pmdaemon stop myapp
98//! pmdaemon restart myapp
99//! pmdaemon delete myapp
100//!
101//! # Clustering with port management
102//! pmdaemon start server.js --instances 4 --port 4000-4003
103//! pmdaemon start worker.js --port auto:5000-5100
104//!
105//! # Runtime port override (without modifying saved config)
106//! pmdaemon restart myapp --port 3001
107//!
108//! # Memory limits and monitoring
109//! pmdaemon start app.js --max-memory 100M
110//! pmdaemon list # Shows ports, memory usage, CPU, etc.
111//! pmdaemon monit # Real-time monitoring
112//!
113//! # Web API server for remote monitoring
114//! pmdaemon web --port 9615 --host 127.0.0.1
115//! ```
116
117pub mod config;
118pub mod error;
119pub mod health;
120pub mod manager;
121pub mod monitoring;
122pub mod process;
123pub mod signals;
124pub mod web;
125
126// Re-export main types for convenience
127pub use config::{EcosystemConfig, ProcessConfig, ProcessConfigBuilder};
128pub use error::{Error, Result};
129pub use health::{HealthCheck, HealthCheckConfig, HealthCheckType, HealthState, HealthStatus};
130pub use manager::ProcessManager;
131pub use monitoring::{MonitoringData, SystemMetrics};
132pub use process::{Process, ProcessId, ProcessState, ProcessStatus};
133pub use signals::{ProcessSignal, SignalHandler};
134
135/// Version of the PMDaemon library
136pub const VERSION: &str = env!("CARGO_PKG_VERSION");
137
138/// Default configuration directory name
139pub const CONFIG_DIR: &str = ".pmdaemon";
140
141/// Default log directory name
142pub const LOG_DIR: &str = "logs";
143
144/// Default PID directory name
145pub const PID_DIR: &str = "pids";
146
147/// Default web server port for monitoring API
148pub const DEFAULT_WEB_PORT: u16 = 9615;
149
150/// Default kill timeout in milliseconds
151pub const DEFAULT_KILL_TIMEOUT: u64 = 1600;
152
153/// Default restart delay in milliseconds
154pub const DEFAULT_RESTART_DELAY: u64 = 0;
155
156/// Maximum number of restart attempts
157pub const DEFAULT_MAX_RESTARTS: u32 = 16;
158
159/// Minimum uptime in milliseconds before considering a process stable
160pub const DEFAULT_MIN_UPTIME: u64 = 1000;