Skip to content

Commit

Permalink
feat(octez): write node log to file
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 19, 2024
1 parent 75a7b20 commit 3cf7e48
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
11 changes: 5 additions & 6 deletions crates/jstzd/src/task/octez_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ impl Task for OctezNode {
Some(v) => Directory::Path(v.clone()),
None => Directory::default(),
};
let log_file = match &config.log_file {
let log_file = Arc::new(match &config.log_file {
Some(v) => FileWrapper::try_from(v.clone())?,
None => FileWrapper::default(),
};
});
let node = node::OctezNode {
octez_node_bin: Some(config.binary_path.clone()),
octez_node_dir: (&data_dir).into(),
log_file: log_file.clone(),
};

let status = node.generate_identity().await?.wait().await?;
Expand All @@ -71,11 +72,9 @@ impl Task for OctezNode {
}

Ok(OctezNode {
inner: ChildWrapper::new_shared(
node.run(log_file.as_file(), &config.run_options)?,
),
inner: ChildWrapper::new_shared(node.run(&config.run_options)?),
config,
_log_file: Arc::new(log_file),
_log_file: log_file,
_data_dir: Arc::new(data_dir),
})
}
Expand Down
26 changes: 15 additions & 11 deletions crates/octez/src/async/node.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
use std::{fs::File, path::PathBuf, process::Stdio};
use std::{path::PathBuf, process::Stdio, sync::Arc};

use tokio::process::{Child, Command};

use crate::path_or_default;

use anyhow::Result;

use super::{endpoint::Endpoint, node_config::OctezNodeRunOptions};
use super::{endpoint::Endpoint, file::FileWrapper, node_config::OctezNodeRunOptions};

pub struct OctezNode {
/// Path to the octez-node binary
/// If None, the binary will inside PATH will be used
pub octez_node_bin: Option<PathBuf>,
/// Path to the octez-node directory
pub octez_node_dir: PathBuf,
/// Path to the log file.
pub log_file: Arc<FileWrapper>,
}

impl OctezNode {
fn command(&self) -> Command {
Command::new(path_or_default(self.octez_node_bin.as_ref(), "octez-node"))
fn command(&self) -> Result<Command> {
let mut cmd =
Command::new(path_or_default(self.octez_node_bin.as_ref(), "octez-node"));
cmd.stdout(Stdio::from(self.log_file.as_file().try_clone()?))
.stderr(Stdio::from(self.log_file.as_file().try_clone()?));
Ok(cmd)
}

pub async fn config_init(
Expand All @@ -29,7 +35,7 @@ impl OctezNode {
num_connections: u32,
) -> Result<Child> {
Ok(self
.command()
.command()?
.args([
"config",
"init",
Expand All @@ -50,7 +56,7 @@ impl OctezNode {

pub async fn generate_identity(&self) -> Result<Child> {
Ok(self
.command()
.command()?
.args([
"identity",
"generate",
Expand All @@ -61,8 +67,8 @@ impl OctezNode {
.spawn()?)
}

pub fn run(&self, log_file: &File, options: &OctezNodeRunOptions) -> Result<Child> {
let mut command = self.command();
pub fn run(&self, options: &OctezNodeRunOptions) -> Result<Child> {
let mut command = self.command()?;

command
.args([
Expand All @@ -71,9 +77,7 @@ impl OctezNode {
self.octez_node_dir.to_str().expect("Invalid path"),
"--singleprocess",
])
.args(options.to_string().split(' '))
.stdout(Stdio::from(log_file.try_clone()?))
.stderr(Stdio::from(log_file.try_clone()?));
.args(options.to_string().split(' '));

Ok(command.spawn()?)
}
Expand Down

0 comments on commit 3cf7e48

Please sign in to comment.