Skip to content

Commit

Permalink
refactor(service): remove the node service restart workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and joshuef committed Mar 11, 2024
1 parent fc47e60 commit 2b82061
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 86 deletions.
45 changes: 2 additions & 43 deletions sn_node_manager/src/add_services/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,56 +64,15 @@ impl InstallNodeServiceCtxBuilder {
args.push(OsString::from(peers_str));
}

let mut service_ctx = ServiceInstallCtx {
Ok(ServiceInstallCtx {
label: label.clone(),
program: self.safenode_path.to_path_buf(),
args,
contents: None,
username: Some(self.service_user.to_string()),
working_directory: None,
environment: self.env_variables,
};
// Temporary fix to enable the restart cmd to properly restart a running service.
// 'ServiceInstallCtx::content' will override the other passed in fields.
#[cfg(target_os = "linux")]
{
use std::fmt::Write;
let mut service = String::new();

let _ = writeln!(service, "[Unit]");
let _ = writeln!(
service,
"Description={}",
service_ctx.label.to_script_name()
);
let _ = writeln!(service, "[Service]");
let program = service_ctx.program.to_string_lossy();
let args = service_ctx
.args
.clone()
.into_iter()
.map(|a| a.to_string_lossy().to_string())
.collect::<Vec<String>>()
.join(" ");
let _ = writeln!(service, "ExecStart={program} {args}");
if let Some(env_vars) = &service_ctx.environment {
for (var, val) in env_vars {
let _ = writeln!(service, "Environment=\"{}={}\"", var, val);
}
}
let _ = writeln!(service, "Restart=on-failure");
let _ = writeln!(service, "User={}", self.service_user);
let _ = writeln!(service, "KillMode=process"); // fixes the restart issue
let _ = writeln!(service, "[Install]");
let _ = writeln!(service, "WantedBy=multi-user.target");

service_ctx.contents = Some(service);
}
#[cfg(not(target_os = "linux"))]
{
service_ctx.contents = None;
}
Ok(service_ctx)
})
}
}

Expand Down
45 changes: 2 additions & 43 deletions sn_service_management/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,56 +75,15 @@ impl ServiceStateActions for NodeService {
args.push(OsString::from(peers_str));
}

let mut service_ctx = ServiceInstallCtx {
Ok(ServiceInstallCtx {
label: label.clone(),
program: self.service_data.safenode_path.to_path_buf(),
args,
contents: None,
username: Some(self.service_data.user.to_string()),
working_directory: None,
environment: options.env_variables,
};
// Temporary fix to enable the restart cmd to properly restart a running service.
// 'ServiceInstallCtx::content' will override the other passed in fields.
#[cfg(target_os = "linux")]
{
use std::fmt::Write;
let mut service = String::new();

let _ = writeln!(service, "[Unit]");
let _ = writeln!(
service,
"Description={}",
service_ctx.label.to_script_name()
);
let _ = writeln!(service, "[Service]");
let program = service_ctx.program.to_string_lossy();
let args = service_ctx
.args
.clone()
.into_iter()
.map(|a| a.to_string_lossy().to_string())
.collect::<Vec<String>>()
.join(" ");
let _ = writeln!(service, "ExecStart={program} {args}");
if let Some(env_vars) = &service_ctx.environment {
for (var, val) in env_vars {
let _ = writeln!(service, "Environment=\"{}={}\"", var, val);
}
}
let _ = writeln!(service, "Restart=on-failure");
let _ = writeln!(service, "User={}", self.service_data.user);
let _ = writeln!(service, "KillMode=process"); // fixes the restart issue
let _ = writeln!(service, "[Install]");
let _ = writeln!(service, "WantedBy=multi-user.target");

service_ctx.contents = Some(service);
}
#[cfg(not(target_os = "linux"))]
{
service_ctx.contents = None;
}
Ok(service_ctx)
})
}

fn data_dir_path(&self) -> PathBuf {
Expand Down

0 comments on commit 2b82061

Please sign in to comment.