Browse Source

Replacing the daemonize crate by the fork crate

ng
Rodolphe Bréard 1 week ago
parent
commit
49dfa09975
Failed to extract signature
  1. 20
      Cargo.lock
  2. 2
      Cargo.toml
  3. 47
      src/main.rs

20
Cargo.lock

@ -9,7 +9,7 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"config", "config",
"daemonize",
"fork",
"nom", "nom",
"reqwest", "reqwest",
"serde", "serde",
@ -221,15 +221,6 @@ dependencies = [
"winnow", "winnow",
] ]
[[package]]
name = "daemonize"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab8bfdaacb3c887a54d41bdf48d3af8873b3f5566469f8ba21b92057509f116e"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "displaydoc" name = "displaydoc"
version = "0.2.5" version = "0.2.5"
@ -262,6 +253,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "fork"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05dc8b302e04a1c27f4fe694439ef0f29779ca4edc205b7b58f00db04e29656d"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.2.1" version = "1.2.1"

2
Cargo.toml

@ -25,7 +25,7 @@ ed448 = []
anyhow = { version = "1.0.94", default-features = false, features = ["std"] } anyhow = { version = "1.0.94", default-features = false, features = ["std"] }
clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std", "string"] } clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std", "string"] }
config = { version = "0.15.6", default-features = false, features = ["toml"] } config = { version = "0.15.6", default-features = false, features = ["toml"] }
daemonize = { version = "0.5.0", default-features = false }
fork = { version = "0.2.0", default-features = false }
nom = { version = "7.1.3", default-features = false } nom = { version = "7.1.3", default-features = false }
reqwest = { version = "0.12.10", default-features = false, features = ["http2", "charset", "rustls-tls"] } reqwest = { version = "0.12.10", default-features = false, features = ["http2", "charset", "rustls-tls"] }
serde = { version = "1.0.216", default-features = false, features = ["derive"] } serde = { version = "1.0.216", default-features = false, features = ["derive"] }

47
src/main.rs

@ -7,7 +7,7 @@ use crate::config::AcmedConfig;
use crate::http::HttpRoutine; use crate::http::HttpRoutine;
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use daemonize::Daemonize;
use fork::{daemon, Fork};
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path; use std::path::Path;
@ -17,6 +17,20 @@ pub const APP_IDENTITY: &[u8] = b"acmed\0";
pub const APP_THREAD_NAME: &str = "acmed-runtime"; pub const APP_THREAD_NAME: &str = "acmed-runtime";
pub const INTERNAL_HOOK_PREFIX: &str = "internal:"; pub const INTERNAL_HOOK_PREFIX: &str = "internal:";
macro_rules! run_server {
($cfg: ident, $args: ident) => {
if let Some(pid_file_path) = $args.pid.get_pid_file() {
let _ = write_pid_file(pid_file_path);
}
let tokio_runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_name(APP_THREAD_NAME)
.build()
.unwrap()
.block_on(start($cfg));
};
}
fn main() { fn main() {
// Load the command-line interface // Load the command-line interface
let args = cli::CliArgs::parse(); let args = cli::CliArgs::parse();
@ -36,18 +50,14 @@ fn main() {
std::process::exit(3) std::process::exit(3)
} }
// Initialize the server (PID file and daemon)
if init_server(args.foreground, args.pid.get_pid_file()).is_err() {
std::process::exit(4);
}
// Starting ACMEd // Starting ACMEd
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_name(APP_THREAD_NAME)
.build()
.unwrap()
.block_on(start(cfg));
if args.foreground {
run_server!(cfg, args);
} else {
if let Ok(Fork::Child) = daemon(false, false) {
run_server!(cfg, args);
}
}
} }
async fn start(cnf: AcmedConfig) { async fn start(cnf: AcmedConfig) {
@ -89,19 +99,6 @@ async fn debug_remove_me(http_client: crate::http::HttpClient) {
// TODO: err(Alternate) // TODO: err(Alternate)
#[tracing::instrument(level = "trace", err)] #[tracing::instrument(level = "trace", err)]
fn init_server(foreground: bool, pid_file: Option<&Path>) -> Result<()> {
if !foreground {
let mut daemonize = Daemonize::new();
if let Some(f) = pid_file {
daemonize = daemonize.pid_file(f);
}
daemonize.start()?
} else if let Some(f) = pid_file {
write_pid_file(f)?
}
Ok(())
}
fn write_pid_file(pid_file: &Path) -> Result<()> { fn write_pid_file(pid_file: &Path) -> Result<()> {
let data = format!("{}\n", process::id()).into_bytes(); let data = format!("{}\n", process::id()).into_bytes();
let mut file = File::create(pid_file)?; let mut file = File::create(pid_file)?;

Loading…
Cancel
Save