update windows service with error handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
ee30646449
commit
83c4075803
@ -1,63 +1,82 @@
|
|||||||
#![windows_subsystem = "windows"]
|
#[cfg(windows)]
|
||||||
|
fn main() -> windows_service::Result<()> {
|
||||||
#[macro_use]
|
winserv::run()
|
||||||
extern crate windows_service;
|
|
||||||
|
|
||||||
use irc_rpc::start_loop;
|
|
||||||
|
|
||||||
use std::ffi::OsString;
|
|
||||||
use std::time::Duration;
|
|
||||||
use windows_service::{
|
|
||||||
define_windows_service,
|
|
||||||
service::{
|
|
||||||
ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
|
|
||||||
ServiceType,
|
|
||||||
},
|
|
||||||
service_control_handler::{self, ServiceControlHandlerResult},
|
|
||||||
service_dispatcher, Result,
|
|
||||||
};
|
|
||||||
|
|
||||||
const SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS;
|
|
||||||
|
|
||||||
define_windows_service!(ffi_service_main, my_service_main);
|
|
||||||
|
|
||||||
fn my_service_main(arguments: Vec<OsString>) {
|
|
||||||
// The entry point where execution will start on a background thread after a call to
|
|
||||||
// `service_dispatcher::start` from `main`.
|
|
||||||
|
|
||||||
let event_handler = move |control_event| -> ServiceControlHandlerResult {
|
|
||||||
match control_event {
|
|
||||||
// Notifies a service to report its current status information to the service
|
|
||||||
// control manager. Always return NoError even if not implemented.
|
|
||||||
ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
|
|
||||||
|
|
||||||
// Handle stop
|
|
||||||
ServiceControl::Stop => {
|
|
||||||
ServiceControlHandlerResult::NoError
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => ServiceControlHandlerResult::NotImplemented,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let status_handle = service_control_handler::register("irc-rpc", event_handler).unwrap();
|
|
||||||
|
|
||||||
status_handle.set_service_status(ServiceStatus {
|
|
||||||
service_type: SERVICE_TYPE,
|
|
||||||
current_state: ServiceState::Running,
|
|
||||||
controls_accepted: ServiceControlAccept::STOP,
|
|
||||||
exit_code: ServiceExitCode::Win32(0),
|
|
||||||
checkpoint: 0,
|
|
||||||
wait_hint: Duration::default(),
|
|
||||||
process_id: None,
|
|
||||||
}).unwrap();
|
|
||||||
|
|
||||||
start_loop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> windows_service::Result<()> {
|
#[cfg(not(windows))]
|
||||||
// Register generated `ffi_service_main` with the system and start the service, blocking
|
fn main() {
|
||||||
// this thread until the service is stopped.
|
//do nothing on *nix
|
||||||
service_dispatcher::start("irc-rpc", ffi_service_main)?;
|
}
|
||||||
Ok(())
|
|
||||||
}
|
#[cfg(windows)]
|
||||||
|
mod winserv {
|
||||||
|
|
||||||
|
use std::ffi::OsString;
|
||||||
|
use std::time::Duration;
|
||||||
|
use windows_service::{
|
||||||
|
define_windows_service,
|
||||||
|
service::{
|
||||||
|
ServiceControl, ServiceControlAccept, ServiceExitCode, ServiceState, ServiceStatus,
|
||||||
|
ServiceType,
|
||||||
|
},
|
||||||
|
service_control_handler::{self, ServiceControlHandlerResult},
|
||||||
|
service_dispatcher, Result,
|
||||||
|
};
|
||||||
|
use irc_rpc::start_loop;
|
||||||
|
|
||||||
|
const SERVICE_TYPE: ServiceType = ServiceType::OWN_PROCESS;
|
||||||
|
|
||||||
|
define_windows_service!(ffi_service_main, my_service_main);
|
||||||
|
|
||||||
|
pub fn run() -> Result<()> {
|
||||||
|
service_dispatcher::start("irc-rpc", ffi_service_main)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn my_service_main(arguments: Vec<OsString>) {
|
||||||
|
// The entry point where execution will start on a background thread after a call to
|
||||||
|
// `service_dispatcher::start` from `main`.
|
||||||
|
|
||||||
|
let event_handler = move |control_event| -> ServiceControlHandlerResult {
|
||||||
|
match control_event {
|
||||||
|
// Notifies a service to report its current status information to the service
|
||||||
|
// control manager. Always return NoError even if not implemented.
|
||||||
|
ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
|
||||||
|
|
||||||
|
// Handle stop
|
||||||
|
ServiceControl::Stop => ServiceControlHandlerResult::NoError,
|
||||||
|
|
||||||
|
_ => ServiceControlHandlerResult::NotImplemented,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let status_handle = service_control_handler::register("irc-rpc", event_handler).unwrap();
|
||||||
|
|
||||||
|
status_handle
|
||||||
|
.set_service_status(ServiceStatus {
|
||||||
|
service_type: SERVICE_TYPE,
|
||||||
|
current_state: ServiceState::Running,
|
||||||
|
controls_accepted: ServiceControlAccept::STOP,
|
||||||
|
exit_code: ServiceExitCode::Win32(0),
|
||||||
|
checkpoint: 0,
|
||||||
|
wait_hint: Duration::default(),
|
||||||
|
process_id: None,
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if let Err(_) = start_loop() {
|
||||||
|
//log error
|
||||||
|
}
|
||||||
|
|
||||||
|
status_handle
|
||||||
|
.set_service_status(ServiceStatus {
|
||||||
|
service_type: SERVICE_TYPE,
|
||||||
|
current_state: ServiceState::Stopped,
|
||||||
|
controls_accepted: ServiceControlAccept::empty(),
|
||||||
|
exit_code: ServiceExitCode::Win32(0),
|
||||||
|
checkpoint: 0,
|
||||||
|
wait_hint: Duration::default(),
|
||||||
|
process_id: None,
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -4,14 +4,12 @@ use std::{collections::HashMap, process};
|
|||||||
|
|
||||||
static ART: &str = include_str!("ouch.txt");
|
static ART: &str = include_str!("ouch.txt");
|
||||||
|
|
||||||
pub fn start_loop() {
|
pub fn start_loop() -> Result<(), failure::Error> {
|
||||||
tokio::runtime::Builder::new_multi_thread()
|
tokio::runtime::Builder::new_multi_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.block_on(async {
|
.block_on(async { event_loop().await })
|
||||||
event_loop().await.unwrap();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn event_loop() -> Result<(), failure::Error> {
|
async fn event_loop() -> Result<(), failure::Error> {
|
||||||
|
@ -3,5 +3,5 @@
|
|||||||
use irc_rpc::start_loop;
|
use irc_rpc::start_loop;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
start_loop();
|
start_loop().unwrap();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user