Skip to content

Commit

Permalink
Keep track of restarts
Browse files Browse the repository at this point in the history
  • Loading branch information
jfldde committed Nov 11, 2024
1 parent 7d69687 commit e09fc1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ where
&self.dir
}

fn set_dir(&mut self, new_dir: PathBuf) {
self.dir = new_dir
}

fn rpc_bind_host(&self) -> &str {
&self.rollup.rpc.bind_host
}
Expand Down
24 changes: 22 additions & 2 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{
fs::File,
path::PathBuf,
process::Stdio,
sync::Arc,
sync::{
atomic::{AtomicU8, Ordering},
Arc,
},
time::{Duration, SystemTime},
};

Expand All @@ -23,7 +26,7 @@ use crate::{
docker::DockerEnv,
log_provider::LogPathProvider,
traits::{NodeT, Restart, SpawnOutput},
utils::{get_citrea_path, get_genesis_path},
utils::{copy_directory, get_citrea_path, get_genesis_path},
Result,
};

Expand Down Expand Up @@ -64,6 +67,7 @@ pub trait Config: Clone {
type NodeConfig: Serialize;

fn dir(&self) -> &PathBuf;
fn set_dir(&mut self, new_dir: PathBuf);
fn rpc_bind_host(&self) -> &str;
fn rpc_bind_port(&self) -> u16;
fn env(&self) -> Vec<(&'static str, &'static str)>;
Expand Down Expand Up @@ -248,9 +252,25 @@ where

async fn start(&mut self, new_config: Option<Self::Config>) -> Result<()> {
let config = self.config_mut();

if let Some(new_config) = new_config {
*config = new_config;
}

// Update and copy to new dir in order not to overwrite the previous datadir when re-spawning
// Keep track of multiple restarts by creating {node_kind}-{INDEX} directories per restart
static INDEX: AtomicU8 = AtomicU8::new(0);
INDEX.fetch_add(1, Ordering::SeqCst);

let old_dir = config.dir();
let new_dir = old_dir.parent().unwrap().join(format!(
"{}-{}",
Self::Config::node_kind(),
INDEX.load(Ordering::SeqCst)
));
copy_directory(old_dir, &new_dir)?;
config.set_dir(new_dir);

*self.spawn_output() = Self::spawn(config)?;
self.wait_for_ready(None).await
}
Expand Down

0 comments on commit e09fc1a

Please sign in to comment.