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