| Crates.io | daemon-base |
| lib.rs | daemon-base |
| version | 0.1.1 |
| created_at | 2025-03-18 13:53:21.486058+00 |
| updated_at | 2025-03-18 13:53:21.486058+00 |
| description | A flexible and configurable Rust daemon library with lifecycle management, logging, callbacks, and optional async support. Works on Linux, macOS, and Windows. |
| homepage | |
| repository | https://github.com/jamesgober/daemon-base |
| max_upload_size | |
| id | 1596595 |
| size | 59,725 |
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.
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" }
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
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.
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"] }
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.)
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.)
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