Skip to content

Commit

Permalink
Feature: daemonize (#225)
Browse files Browse the repository at this point in the history
* feat: daemonize florestad

This commit introduces a `-daemon` cli option that starts florestad as a
background process, and optionally writes the PID to a specified file
  • Loading branch information
Davidson-Souza authored Aug 27, 2024
1 parent c8bd499 commit 0cc4b9c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ After building, florestad and floresta-cli will be available in the target direc
florestad
```

You may run it as a background process with the `--daemon` flag.

```bash
florestad --daemon
```

This will start the full node, and you can connect to it with an Electrum wallet or with the `floresta-cli` tool.

```bash
Expand Down
3 changes: 3 additions & 0 deletions florestad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jsonrpc-core-client = { version = "18.0.0", features = [
zmq = { version = "0.10.0", optional = true }
latest = "0.1.1"

[target.'cfg(unix)'.dependencies]
daemonize = { version = "0.5.0" }

[lib]
name = "florestad"
path = "src/lib.rs"
Expand Down
16 changes: 15 additions & 1 deletion florestad/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct Cli {
/// Option for saving log into data_Dir
///
/// if set, log will be saved into $DATA_DIR/output.log.
pub log_file: bool,
pub log_to_file: bool,

#[arg(long, value_name = "PATH")]
/// Where should we store data. This is the directory where we'll store the chainstate,
Expand Down Expand Up @@ -161,4 +161,18 @@ pub struct Cli {
/// trust in the developer that the utreexo state is correct. Everything after the assumed
/// height will be fully validated.
pub assume_utreexo: bool,

#[cfg(unix)]
#[arg(long, default_value = "false")]
/// Whether we should run as a daemon
pub daemon: bool,

#[cfg(unix)]
#[arg(long, value_name = "FILE", requires = "daemon")]
/// A file to write the process id to
///
/// In case you're using the daemon option, and you want to know the process id, you can
/// write it to a file. This option should be an absolute path to a file. Usually, you'd
/// write it to $DATA_DIR/florestad.pid
pub pid_file: Option<String>,
}
1 change: 0 additions & 1 deletion florestad/src/florestad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ impl Florestad {
})
})
.unwrap_or("floresta".into());

let data_dir = match self.config.network {
crate::Network::Bitcoin => data_dir,
crate::Network::Signet => data_dir + "/signet/",
Expand Down
19 changes: 17 additions & 2 deletions florestad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use std::time::Duration;

use clap::Parser;
use cli::Cli;
#[cfg(unix)]
use daemonize::Daemonize;
use florestad::Config;
use florestad::Florestad;
use futures::executor::block_on;
Expand All @@ -35,15 +37,18 @@ fn main() {
let config = Config {
network: params.network.into(),
debug: params.debug,
data_dir: params.data_dir,
data_dir: params.data_dir.clone(),
cfilters: params.cfilters,
proxy: params.proxy,
rescan: params.rescan,
assume_utreexo: params.assume_utreexo,
connect: params.connect,
wallet_xpub: params.wallet_xpub,
config_file: params.config_file,
log_to_file: params.log_file,
#[cfg(unix)]
log_to_file: params.log_to_file || params.daemon,
#[cfg(not(unix))]
log_to_file: params.log_to_file,
assume_valid: params.assume_valid,
log_to_stdout: true,
json_rpc_address: params.rpc_address,
Expand All @@ -53,6 +58,16 @@ fn main() {
user_agent: format!("/Floresta:{}/", env!("GIT_DESCRIBE")),
assumeutreexo_value: None,
};

#[cfg(unix)]
if params.daemon {
let mut daemon = Daemonize::new();
if let Some(pid_file) = params.pid_file {
daemon = daemon.pid_file(pid_file);
}
daemon.start().expect("Failed to daemonize");
}

let _rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.worker_threads(4)
Expand Down

0 comments on commit 0cc4b9c

Please sign in to comment.