{"id":26495331,"url":"https://github.com/jamesgober/daemon-base","last_synced_at":"2025-03-20T10:38:05.288Z","repository":{"id":283089070,"uuid":"950491391","full_name":"jamesgober/daemon-base","owner":"jamesgober","description":"A flexible and configurable Rust daemon library with lifecycle management, logging, callbacks, and optional async support. Works on Linux, macOS, and Windows.","archived":false,"fork":false,"pushed_at":"2025-03-18T13:52:56.000Z","size":1,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-18T14:36:59.657Z","etag":null,"topics":["async","background","background-service","cross-platform","daemon","lifecycle","multithreading","process","process-control","rust","systemd","tokio"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamesgober.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-18T08:46:24.000Z","updated_at":"2025-03-18T13:52:59.000Z","dependencies_parsed_at":"2025-03-18T14:37:43.627Z","dependency_job_id":"cc926949-537f-4ff1-a408-960a433fcbf7","html_url":"https://github.com/jamesgober/daemon-base","commit_stats":null,"previous_names":["jamesgober/daemon-base"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesgober%2Fdaemon-base","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesgober%2Fdaemon-base/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesgober%2Fdaemon-base/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesgober%2Fdaemon-base/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamesgober","download_url":"https://codeload.github.com/jamesgober/daemon-base/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244268579,"owners_count":20426060,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","background","background-service","cross-platform","daemon","lifecycle","multithreading","process","process-control","rust","systemd","tokio"],"created_at":"2025-03-20T10:38:04.756Z","updated_at":"2025-03-20T10:38:05.280Z","avatar_url":"https://github.com/jamesgober.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eDAEMON\u003cbr\u003e\u003csup\u003eRUST LIBRARY\u003c/sup\u003e\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://crates.io/crates/daemon-base\"\u003e\u003cimg alt=\"Crates.io\" src=\"https://img.shields.io/crates/v/daemon-base\"\u003e\u003c/a\u003e\n    \u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/docsrs/daemon-base\"\u003e\n\u003ca href=\"https://crates.io/crates/daemon-base\"\u003e\n    \u003cimg alt=\"Crates.io Downloads\" src=\"https://img.shields.io/crates/d/daemon-base\"\u003e\u003c/a\u003e\n    \u003cimg alt=\"GitHub last commit\" src=\"https://img.shields.io/github/last-commit/jamesgober/daemon-base?color=%23347d39\" alt=\"last commit badge\"\u003e\n    \u003cimg alt=\"GitHub\" src=\"https://img.shields.io/github/license/jamesgober/daemon-base\"\u003e\n\u003c/p\u003e\n\n**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.\n\n\n\n## Features\n\n- **Cross-Platform** – Works seamlessly on Linux, macOS, and Windows.\n- **Lifecycle Management** – Start, stop, restart, and monitor the daemon’s state.\n- **Event Callbacks** – Hook into lifecycle events to define custom behavior.\n- **Configurable** – Load settings from a JSON file for easy customization.\n- **Logging \u0026 Error Handling** – Built-in structured logging with configurable levels.\n- **Optional Async Support** – Supports both synchronous and asynchronous execution (Tokio).\n- **Graceful Shutdown** – Ensures safe cleanup and state persistence on exit.\n- **Process Control** – Manage background tasks efficiently with threading support.\n\n\u0026nbsp;\n\n---\n\n## Usage\n\nAdd the library to your `Cargo.toml`:\nTo use **daemon-base**, add the following to your `Cargo.toml`:\n```toml\n[dependencies]\ndaemon-base = \"0.1.1\"\n```\n\u0026nbsp;\n\nOR From GitHub (Latest Version)\n```toml\n[dependencies]\ndaemon-base = { git = \"https://github.com/jamesgober/daemon-base\", branch = \"main\" }\n```\n\n\u0026nbsp;\n\n\n\u0026nbsp;\n\n## Usage\n\n### 1. Basic Daemon Setup\nThis example initializes a simple daemon and starts it.\n```rust\nuse daemon_base::{Daemon, DaemonState};\n\nfn main() {\n    let daemon = Daemon::new();\n\n    println!(\"Daemon is in state: {:?}\", daemon.get_state());\n\n    daemon.start();\n    println!(\"Daemon started. Current state: {:?}\", daemon.get_state());\n}\n```\n\n*Expected Output:*\n```\nDaemon is in state: Offline\nDaemon started. Current state: Running\n```\n\n\u0026nbsp;\n\n### 2. Handling Lifecycle Events (Callbacks)\nCustomize daemon behavior by hooking into lifecycle events.\n```rust\nuse daemon_base::{Daemon, DaemonState};\n\nfn on_start() {\n    println!(\"Daemon is starting...\");\n}\n\nfn on_shutdown() {\n    println!(\"Daemon is shutting down.\");\n}\n\nfn main() {\n    let mut daemon = Daemon::new();\n\n    daemon.on_start(on_start);\n    daemon.on_shutdown(on_shutdown);\n\n    daemon.start();\n    daemon.stop();\n}\n```\n\n\n*Expected Output:*\n```\nDaemon is starting...\nDaemon is shutting down.\n```\n\n\u0026nbsp;\n\n### 3. Configuring the Daemon with JSON\nYou can load a configuration file instead of hardcoding values.\n\nconfig.json\n```json\n{\n    \"log_level\": \"info\",\n    \"multi_threading\": true,\n    \"extensions\": [\"frag-indexer\", \"query-optimizer\"]\n}\n\n```\n\nRust Code\n```rust\nuse daemon_base::DaemonConfig;\n\nfn main() {\n    let config = DaemonConfig::load(\"config.json\").expect(\"Failed to load config\");\n    println!(\"Loaded configuration: {:?}\", config);\n}\n```\n\n\n*Expected Output:*\n```\nLoaded configuration: DaemonConfig { log_level: \"info\", multi_threading: true, extensions: [\"frag-indexer\", \"query-optimizer\"] }\n\n```\n\n\u0026nbsp;\n\n### 4. Running the Daemon as a Background Process\nTo run the daemon in the background and detach from the terminal:\n```rust\nuse daemon_base::Daemon;\n\nfn main() {\n    let daemon = Daemon::new();\n    daemon.start_detached();\n    println!(\"Daemon started in the background.\");\n}\n\n```\n**Output:** *(Daemon keeps running in the background, no terminal output after start.)*\n\n\u0026nbsp;\n\n### 5. Enabling Asynchronous Mode\nIf you're using Tokio async runtime, enable the `async` feature in` Cargo.toml`:\n```toml\n[dependencies]\ndaemon-base = { version = \"0.1.1\", features = [\"async\"] }\ntokio = { version = \"1\", features = [\"full\"] }\n```\n\nThen, use `async` methods in Rust:\n```rust\nuse daemon_base::Daemon;\nuse tokio::time::{sleep, Duration};\n\n#[tokio::main]\nasync fn main() {\n    let daemon = Daemon::new();\n\n    daemon.start_async().await;\n    println!(\"Daemon started asynchronously.\");\n\n    sleep(Duration::from_secs(5)).await;\n\n    daemon.stop_async().await;\n    println!(\"Daemon stopped.\");\n}\n```\n**Output:** *(Runs asynchronously for 5 seconds before shutting down.)*\n\n\n---\n\n\u0026nbsp;\n\n## License\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n---\n\n*Copyright \u0026copy; 2025 James Gober.*   \nhttps://jamesgober.com","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesgober%2Fdaemon-base","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamesgober%2Fdaemon-base","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesgober%2Fdaemon-base/lists"}