From 386ac5645d4a74d1a7ce1da963f17b54c11841d1 Mon Sep 17 00:00:00 2001 From: Nadja Reitzenstein Date: Wed, 18 Jan 2023 16:59:36 +0100 Subject: [PATCH] Do not do raw processing on restores --- bffhd/actors/process.rs | 97 ++++++++++++++++++++++++++--------------- examples/actor.py | 4 ++ 2 files changed, 65 insertions(+), 36 deletions(-) diff --git a/bffhd/actors/process.rs b/bffhd/actors/process.rs index bf57a79..2dca118 100644 --- a/bffhd/actors/process.rs +++ b/bffhd/actors/process.rs @@ -34,6 +34,67 @@ impl Process { I: IntoIterator, S: AsRef, { + } +} + +impl Actor for Process { + fn restore(&mut self, state: ArchivedValue) -> BoxFuture<'static, ()> { + tracing::debug!(name=%self.name, cmd=%self.cmd, ?state, + "Process actor updating state"); + let mut command = Command::new(&self.cmd); + command + .stdin(Stdio::null()) + .args(self.args.iter()) + .arg(&self.name); + + match &state.as_ref().inner.state { + ArchivedStatus::Free => { + command.arg("free"); + } + ArchivedStatus::InUse(by) => { + command.arg("inuse").arg(by.id.as_str()); + } + ArchivedStatus::ToCheck(by) => { + command.arg("tocheck").arg(by.id.as_str()); + } + ArchivedStatus::Blocked(by) => { + command.arg("blocked").arg(by.id.as_str()); + } + ArchivedStatus::Disabled => { + command.arg("disabled"); + } + ArchivedStatus::Reserved(by) => { + command.arg("reserved").arg(by.id.as_str()); + } + } + + let name = self.name.clone(); + Box::pin(async move { + match command.output() { + Ok(retv) if retv.status.success() => { + tracing::trace!("Actor was restored"); + let outstr = String::from_utf8_lossy(&retv.stdout); + for line in outstr.lines() { + tracing::debug!(%name, %line, "actor stdout"); + } + } + Ok(retv) => { + tracing::warn!(%name, ?state, code=?retv.status, + "Actor failed to restore: nonzero exitcode" + ); + if !retv.stderr.is_empty() { + let errstr = String::from_utf8_lossy(&retv.stderr); + for line in errstr.lines() { + tracing::warn!(%name, %line, "actor stderr"); + } + } + } + Err(error) => tracing::warn!(%name, ?error, "process actor failed to run cmd"), + } + }) + } + + fn apply(&mut self, state: ArchivedValue) -> BoxFuture<'static, ()> { tracing::debug!(name=%self.name, cmd=%self.cmd, ?state, "Process actor updating state"); let mut command = Command::new(&self.cmd); @@ -68,42 +129,6 @@ impl Process { command.arg("raw").arg(b64); } - command - } -} - -impl Actor for Process { - fn restore(&mut self, state: ArchivedValue) -> BoxFuture<'static, ()> { - let mut command = self.build_command(&state, ["--restore"]); - let name = self.name.clone(); - Box::pin(async move { - match command.output() { - Ok(retv) if retv.status.success() => { - tracing::trace!("Actor was restored"); - let outstr = String::from_utf8_lossy(&retv.stdout); - for line in outstr.lines() { - tracing::debug!(%name, %line, "actor stdout"); - } - } - Ok(retv) => { - tracing::warn!(%name, ?state, code=?retv.status, - "Actor failed to restore: nonzero exitcode" - ); - if !retv.stderr.is_empty() { - let errstr = String::from_utf8_lossy(&retv.stderr); - for line in errstr.lines() { - tracing::warn!(%name, %line, "actor stderr"); - } - } - } - Err(error) => tracing::warn!(%name, ?error, "process actor failed to run cmd"), - } - }) - } - - fn apply(&mut self, state: ArchivedValue) -> BoxFuture<'static, ()> { - let empty: [&str; 0] = []; - let mut command = self.build_command(&state, empty); let name = self.name.clone(); Box::pin(async move { match command.output() { diff --git a/examples/actor.py b/examples/actor.py index c2824f5..5f03ae9 100755 --- a/examples/actor.py +++ b/examples/actor.py @@ -129,6 +129,9 @@ def main(args): else: args.verbose = 0 + if args.restore and args.verbose > 0: + print("running in restore mode") + # You could also check the actor name here and call different functions # depending on that variable instead of passing it to the state change # methods. @@ -160,6 +163,7 @@ if __name__ == "__main__": parser.add_argument("-q", "--quiet", help="be less verbose", action="store_true") parser.add_argument("-v", "--verbose", help="be more verbose", action="count") + parser.add_argument("-r", "--restore", help="run in restore mode", action="store_true") parser.add_argument("name", help="name of this actor as configured in bffh.dhall"