Browse Source

Daemonize the process

pull/5/head
Rodolphe Breard 6 years ago
parent
commit
406424e932
  1. 1
      CHANGELOG.md
  2. 1
      acmed/Cargo.toml
  3. 39
      acmed/src/main.rs

1
CHANGELOG.md

@ -16,3 +16,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- `post_operation_hook` has been renamed `post_operation_hooks`. - `post_operation_hook` has been renamed `post_operation_hooks`.
- By default, logs are now sent to syslog instead of stderr. - By default, logs are now sent to syslog instead of stderr.
- The process is now daemonized by default. It is possible to still run it in the foreground using the `--foregroung` flag.

1
acmed/Cargo.toml

@ -13,6 +13,7 @@ include = ["src/**/*", "Cargo.toml", "LICENSE-*.txt"]
[dependencies] [dependencies]
acme-lib = "0.4" acme-lib = "0.4"
clap = "2.32" clap = "2.32"
daemonize = "0.3"
env_logger = "0.6" env_logger = "0.6"
handlebars = "2.0.0-beta.1" handlebars = "2.0.0-beta.1"
log = "0.4" log = "0.4"

39
acmed/src/main.rs

@ -1,4 +1,5 @@
use clap::{App, Arg}; use clap::{App, Arg};
use daemonize::Daemonize;
use log::{error, LevelFilter}; use log::{error, LevelFilter};
mod acmed; mod acmed;
@ -10,6 +11,7 @@ mod logs;
mod storage; mod storage;
pub const APP_NAME: &str = "acmed"; pub const APP_NAME: &str = "acmed";
pub const DEFAULT_PID_FILE: &str = "/var/run/admed.pid";
pub const DEFAULT_CONFIG_FILE: &str = "/etc/acmed/acmed.toml"; pub const DEFAULT_CONFIG_FILE: &str = "/etc/acmed/acmed.toml";
pub const DEFAULT_ACCOUNTS_DIR: &str = "/etc/acmed/accounts"; pub const DEFAULT_ACCOUNTS_DIR: &str = "/etc/acmed/accounts";
pub const DEFAULT_CERT_DIR: &str = "/etc/acmed/certs"; pub const DEFAULT_CERT_DIR: &str = "/etc/acmed/certs";
@ -31,14 +33,14 @@ fn main() {
Arg::with_name("config") Arg::with_name("config")
.short("c") .short("c")
.long("config") .long("config")
.help("Specify an alternative configuration file.")
.help("Specify an alternative configuration file")
.takes_value(true) .takes_value(true)
.value_name("FILE"), .value_name("FILE"),
) )
.arg( .arg(
Arg::with_name("log-level") Arg::with_name("log-level")
.long("log-level") .long("log-level")
.help("Specify the log level.")
.help("Specify the log level")
.takes_value(true) .takes_value(true)
.value_name("LEVEL") .value_name("LEVEL")
.possible_values(&["error", "warn", "info", "debug", "trace"]), .possible_values(&["error", "warn", "info", "debug", "trace"]),
@ -46,15 +48,30 @@ fn main() {
.arg( .arg(
Arg::with_name("to-syslog") Arg::with_name("to-syslog")
.long("log-syslog") .long("log-syslog")
.help("Send log messages via syslog.")
.help("Sends log messages via syslog")
.conflicts_with("to-stderr"), .conflicts_with("to-stderr"),
) )
.arg( .arg(
Arg::with_name("to-stderr") Arg::with_name("to-stderr")
.long("log-stderr") .long("log-stderr")
.help("Print log messages to the standard error output.")
.help("Prints log messages to the standard error output")
.conflicts_with("log-syslog"), .conflicts_with("log-syslog"),
) )
.arg(
Arg::with_name("foregroung")
.short("f")
.long("foregroung")
.help("Runs in the foregroung")
.conflicts_with("to-stderr"),
)
.arg(
Arg::with_name("pid-file")
.long("pid-file")
.help("Specifies the location of the PID file")
.takes_value(true)
.value_name("FILE")
.conflicts_with("foregroung"),
)
.get_matches(); .get_matches();
match logs::set_log_system( match logs::set_log_system(
@ -65,10 +82,22 @@ fn main() {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
eprintln!("Error: {}", e); eprintln!("Error: {}", e);
std::process::exit(1);
std::process::exit(2);
} }
}; };
if !matches.is_present("foregroung") {
let pid_file = matches.value_of("pid-file").unwrap_or(DEFAULT_PID_FILE);
let daemonize = Daemonize::new().pid_file(pid_file);
match daemonize.start() {
Ok(_) => {}
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(3);
}
}
}
let config_file = matches.value_of("config").unwrap_or(DEFAULT_CONFIG_FILE); let config_file = matches.value_of("config").unwrap_or(DEFAULT_CONFIG_FILE);
let mut srv = match acmed::Acmed::new(&config_file) { let mut srv = match acmed::Acmed::new(&config_file) {
Ok(s) => s, Ok(s) => s,

Loading…
Cancel
Save