server-manager is a rust library for managing server processes. It encapsulates service startup, shutdown, and background daemon mode. Users can specify the PID file, log file paths, and other configurations through custom settings, while also passing in their own asynchronous server function for execution. The library supports both synchronous and asynchronous operations. On Unix and Windows platforms, it enables background daemon processes.
To use this crate, you can run cmd:
cargo add server-manager
use server_manager::*;
use std::fs;
use std::time::Duration;
let pid_file: String = "./process/test_pid.pid".to_string();
let _ = fs::remove_file(&pid_file);
let server = || async {
tokio::time::sleep(Duration::from_secs(1)).await;
};
let mut manager: ServerManager = ServerManager::new();
manager
.set_pid_file(&pid_file)
.set_start_hook(|| async {
println!("Before start daemon hook executed");
})
.set_server_hook(server)
.set_stop_hook(|| async {
println!("Before stop hook executed");
});
let res: ServerManagerResult = manager.start_daemon().await;
println!("start_daemon {:?}", res);
let res: ServerManagerResult = manager.stop().await;
println!("stop {:?}", res);
manager.start().await;
let _ = fs::remove_file(&pid_file);
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.
For any inquiries, please reach out to the author at root@ltpp.vip.