Skip to content

Commit

Permalink
fix(dgw): error code on service startup failure (#816)
Browse files Browse the repository at this point in the history
Instead of panicking when failing to start the service, we instead
attempt to log the error to the log file and return an error code.

Issue: DGW-174
  • Loading branch information
CBenoit authored Apr 18, 2024
1 parent 9512ada commit 66e7ce2
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions devolutions-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum CliAction {
fn main() -> anyhow::Result<()> {
let mut args = std::env::args();

let executable = args.next().unwrap();
let executable = args.next().context("executable name is missing from the environment")?;

let action = match args.next().as_deref() {
Some("--service") => CliAction::Run { service_mode: true },
Expand Down Expand Up @@ -108,7 +108,7 @@ fn main() -> anyhow::Result<()> {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_io()
.build()
.unwrap();
.context("failed to build the async runtime")?;
rt.block_on(build_signals_fut())?;

service.stop();
Expand All @@ -125,22 +125,36 @@ fn service_controller() -> Controller {

enum GatewayServiceEvent {}

const BAD_CONFIG_ERR_CODE: u32 = 1;
const START_FAILED_ERR_CODE: u32 = 2;

fn gateway_service_main(
rx: mpsc::Receiver<ServiceEvent<GatewayServiceEvent>>,
_tx: mpsc::Sender<ServiceEvent<GatewayServiceEvent>>,
args: Vec<String>,
_standalone_mode: bool,
) -> u32 {
let conf_handle = ConfHandle::init().expect("unable to initialize configuration");
let mut service = GatewayService::load(conf_handle).expect("unable to load service");
let Ok(conf_handle) = ConfHandle::init() else {
// At this point, the logger is not yet initialized.
return BAD_CONFIG_ERR_CODE;
};

info!("{} service started", SERVICE_NAME);
info!("args: {:?}", args);
let mut service = match GatewayService::load(conf_handle) {
Ok(service) => service,
Err(error) => {
// At this point, the logger may or may not be initialized.
error!(error = format!("{error:#}"), "Failed to load service");
return START_FAILED_ERR_CODE;
}
};

service
.start()
.tap_err(|error| error!(error = format!("{error:#}"), "Failed to start"))
.expect("start service");
match service.start() {
Ok(()) => info!("{} service started", SERVICE_NAME),
Err(error) => {
error!(error = format!("{error:#}"), "Failed to start");
return START_FAILED_ERR_CODE;
}
}

loop {
if let Ok(control_code) = rx.recv() {
Expand Down

0 comments on commit 66e7ce2

Please sign in to comment.