1 unstable release
new 0.1.1 | Mar 18, 2025 |
---|
#436 in Asynchronous
98 downloads per month
21KB
293 lines
Daemon Library
Daemon Base is a flexible and configurable Rust daemon library for managing background services. It supports lifecycle management, logging, callbacks, and optional async execution. Designed for cross-platform use, it runs on Linux, macOS, and Windows, making it ideal for system services, background tasks, and process management.
Features
- Cross-Platform – Works seamlessly on Linux, macOS, and Windows.
- Lifecycle Management – Start, stop, restart, and monitor the daemon’s state.
- Event Callbacks – Hook into lifecycle events to define custom behavior.
- Configurable – Load settings from a JSON file for easy customization.
- Logging & Error Handling – Built-in structured logging with configurable levels.
- Optional Async Support – Supports both synchronous and asynchronous execution (Tokio).
- Graceful Shutdown – Ensures safe cleanup and state persistence on exit.
- Process Control – Manage background tasks efficiently with threading support.
Usage
Add the library to your Cargo.toml
:
To use daemon-base, add the following to your Cargo.toml
:
[dependencies]
daemon-base = "0.1.1"
OR From GitHub (Latest Version)
[dependencies]
daemon-base = { git = "https://github.com/jamesgober/daemon-base", branch = "main" }
Usage
1. Basic Daemon Setup
This example initializes a simple daemon and starts it.
use daemon_base::{Daemon, DaemonState};
fn main() {
let daemon = Daemon::new();
println!("Daemon is in state: {:?}", daemon.get_state());
daemon.start();
println!("Daemon started. Current state: {:?}", daemon.get_state());
}
Expected Output:
Daemon is in state: Offline
Daemon started. Current state: Running
2. Handling Lifecycle Events (Callbacks)
Customize daemon behavior by hooking into lifecycle events.
use daemon_base::{Daemon, DaemonState};
fn on_start() {
println!("Daemon is starting...");
}
fn on_shutdown() {
println!("Daemon is shutting down.");
}
fn main() {
let mut daemon = Daemon::new();
daemon.on_start(on_start);
daemon.on_shutdown(on_shutdown);
daemon.start();
daemon.stop();
}
Expected Output:
Daemon is starting...
Daemon is shutting down.
3. Configuring the Daemon with JSON
You can load a configuration file instead of hardcoding values.
config.json
{
"log_level": "info",
"multi_threading": true,
"extensions": ["frag-indexer", "query-optimizer"]
}
Rust Code
use daemon_base::DaemonConfig;
fn main() {
let config = DaemonConfig::load("config.json").expect("Failed to load config");
println!("Loaded configuration: {:?}", config);
}
Expected Output:
Loaded configuration: DaemonConfig { log_level: "info", multi_threading: true, extensions: ["frag-indexer", "query-optimizer"] }
4. Running the Daemon as a Background Process
To run the daemon in the background and detach from the terminal:
use daemon_base::Daemon;
fn main() {
let daemon = Daemon::new();
daemon.start_detached();
println!("Daemon started in the background.");
}
Output: (Daemon keeps running in the background, no terminal output after start.)
5. Enabling Asynchronous Mode
If you're using Tokio async runtime, enable the async
feature in Cargo.toml
:
[dependencies]
daemon-base = { version = "0.1.1", features = ["async"] }
tokio = { version = "1", features = ["full"] }
Then, use async
methods in Rust:
use daemon_base::Daemon;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let daemon = Daemon::new();
daemon.start_async().await;
println!("Daemon started asynchronously.");
sleep(Duration::from_secs(5)).await;
daemon.stop_async().await;
println!("Daemon stopped.");
}
Output: (Runs asynchronously for 5 seconds before shutting down.)
License
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright © 2025 James Gober.
https://jamesgober.com
Dependencies
~1.3–8MB
~66K SLoC