Skip to content

Commit

Permalink
refactor internal of ExitCode to ExitStatus to support the windows se…
Browse files Browse the repository at this point in the history
…rvice api
  • Loading branch information
DominicBurkart committed Jun 20, 2023
1 parent 7ab837b commit ae758a6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![allow(missing_docs)]
use std::{
collections::HashMap, num::NonZeroUsize, path::PathBuf, process::ExitCode as Exit,
time::Duration,
collections::HashMap, num::NonZeroUsize, path::PathBuf, process::ExitStatus, time::Duration,
};

use exitcode::ExitCode;
Expand Down Expand Up @@ -35,6 +34,11 @@ use crate::{
trace,
};

#[cfg(unix)]
use std::os::unix::process::ExitStatusExt;
#[cfg(windows)]
use std::os::windows::process::ExitStatusExt;

pub static WORKER_THREADS: OnceNonZeroUsize = OnceNonZeroUsize::new();

use crate::internal_events::{VectorQuit, VectorStarted, VectorStopped};
Expand Down Expand Up @@ -148,7 +152,7 @@ impl ApplicationConfig {
}

impl Application {
pub fn run() -> Exit {
pub fn run() -> ExitStatus {
let (runtime, app) = Self::prepare_start().unwrap_or_else(|code| std::process::exit(code));

runtime.block_on(app.run())
Expand Down Expand Up @@ -245,7 +249,7 @@ pub struct StartedApplication {
}

impl StartedApplication {
pub async fn run(self) -> Exit {
pub async fn run(self) -> ExitStatus {
self.main().await.shutdown().await
}

Expand Down Expand Up @@ -320,7 +324,7 @@ pub struct FinishedApplication {
}

impl FinishedApplication {
pub async fn shutdown(self) -> Exit {
pub async fn shutdown(self) -> ExitStatus {
let FinishedApplication {
signal,
mut signal_rx,
Expand All @@ -338,20 +342,20 @@ impl FinishedApplication {
SignalTo::Shutdown => {
emit!(VectorStopped);
tokio::select! {
_ = topology_controller.stop() => Exit::SUCCESS, // Graceful shutdown finished
_ = topology_controller.stop() => ExitStatus::from_raw(exitcode::OK as u32), // Graceful shutdown finished
_ = signal_rx.recv() => {
// It is highly unlikely that this event will exit from topology.
emit!(VectorQuit);
// Dropping the shutdown future will immediately shut the server down
Exit::FAILURE
ExitStatus::from_raw(exitcode::UNAVAILABLE as u32)
}
}
}
SignalTo::Quit => {
// It is highly unlikely that this event will exit from topology.
emit!(VectorQuit);
drop(topology_controller);
Exit::FAILURE
ExitStatus::from_raw(exitcode::UNAVAILABLE as u32)
}
_ => unreachable!(),
}
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn main() -> ExitCode {
}
}

Application::run()
let exit_code = Application::run().code().unwrap_or(exitcode::UNAVAILABLE) as u8;
ExitCode::from(exit_code)
}

#[cfg(windows)]
Expand All @@ -46,5 +47,7 @@ pub fn main() -> ExitCode {
// to run vector as a service. If we fail, we consider that we are in
// interactive mode and then fallback to console mode. See
// https://docs.microsoft.com/en-us/dotnet/api/system.environment.userinteractive?redirectedfrom=MSDN&view=netcore-3.1#System_Environment_UserInteractive
vector::vector_windows::run().unwrap_or_else(|_| Application::run())
let exit_code = vector::vector_windows::run()
.unwrap_or_else(|_| Application::run().code().unwrap_or(exitcode::UNAVAILABLE));
ExitCode::from(exit_code as u8)
}
9 changes: 5 additions & 4 deletions src/vector_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,9 @@ fn win_main(arguments: Vec<OsString>) {
if let Err(_e) = run_service(arguments) {}
}

pub fn run() -> Result<()> {
service_dispatcher::start(SERVICE_NAME, ffi_service_main)
pub fn run() -> Result<i32> {
service_dispatcher::start(SERVICE_NAME, ffi_service_main).map(|()| 0_i32)
// Always returns 0 exit code as errors are handled by the service dispatcher.
}

fn run_service(_arguments: Vec<OsString>) -> Result<()> {
Expand Down Expand Up @@ -407,10 +408,10 @@ fn run_service(_arguments: Vec<OsString>) -> Result<()> {
current_state: ServiceState::Stopped,
controls_accepted: ServiceControlAccept::empty(),
exit_code: {
if program_completion_status.id() == 0 {
if program_completion_status.success() {
ServiceExitCode::Win32(NO_ERROR)
} else {
// we could not gracefully shut down in time, likely due to timeout.
// we didn't gracefully shutdown within grace period.
ServiceExitCode::Win32(ERROR)
}
},
Expand Down

0 comments on commit ae758a6

Please sign in to comment.