service_failure_actions/
service_failure_actions.rs

1#[cfg(windows)]
2fn main() -> windows_service::Result<()> {
3    use std::ffi::OsString;
4    use std::time::Duration;
5    use windows_service::{
6        service::{
7            ServiceAccess, ServiceAction, ServiceActionType, ServiceErrorControl,
8            ServiceFailureActions, ServiceFailureResetPeriod, ServiceInfo, ServiceStartType,
9            ServiceType,
10        },
11        service_manager::{ServiceManager, ServiceManagerAccess},
12    };
13
14    const SERVICE_NAME: &str = "service_failure_actions_example";
15
16    let manager_access = ServiceManagerAccess::CONNECT | ServiceManagerAccess::CREATE_SERVICE;
17    let service_manager = ServiceManager::local_computer(None::<&str>, manager_access)?;
18
19    let service_binary_path = ::std::env::current_exe()
20        .unwrap()
21        .with_file_name("service_failure_actions.exe");
22
23    let service_info = ServiceInfo {
24        name: OsString::from(SERVICE_NAME),
25        display_name: OsString::from("Service Failure Actions Example"),
26        service_type: ServiceType::OWN_PROCESS,
27        start_type: ServiceStartType::OnDemand,
28        error_control: ServiceErrorControl::Normal,
29        executable_path: service_binary_path,
30        launch_arguments: vec![],
31        dependencies: vec![],
32        account_name: None, // run as System
33        account_password: None,
34    };
35
36    let service_access = ServiceAccess::QUERY_CONFIG
37        | ServiceAccess::CHANGE_CONFIG
38        | ServiceAccess::START
39        | ServiceAccess::DELETE;
40
41    println!("Create or open the service {}", SERVICE_NAME);
42    let service = service_manager
43        .create_service(&service_info, service_access)
44        .or(service_manager.open_service(SERVICE_NAME, service_access))?;
45
46    let actions = vec![
47        ServiceAction {
48            action_type: ServiceActionType::Restart,
49            delay: Duration::from_secs(5),
50        },
51        ServiceAction {
52            action_type: ServiceActionType::RunCommand,
53            delay: Duration::from_secs(10),
54        },
55        ServiceAction {
56            action_type: ServiceActionType::None,
57            delay: Duration::default(),
58        },
59    ];
60
61    println!("Update failure actions");
62    let failure_actions = ServiceFailureActions {
63        reset_period: ServiceFailureResetPeriod::After(Duration::from_secs(86400 * 2)),
64        reboot_msg: None,
65        command: Some(OsString::from("ping 127.0.0.1")),
66        actions: Some(actions),
67    };
68    service.update_failure_actions(failure_actions)?;
69
70    println!("Query failure actions");
71    let updated_failure_actions = service.get_failure_actions()?;
72    println!("{:#?}", updated_failure_actions);
73
74    println!("Enable failure actions on non-crash failures");
75    service.set_failure_actions_on_non_crash_failures(true)?;
76
77    println!("Query failure actions on non-crash failures enabled");
78    let failure_actions_flag = service.get_failure_actions_on_non_crash_failures()?;
79    println!(
80        "Failure actions on non-crash failures enabled: {}",
81        failure_actions_flag
82    );
83
84    println!("Delete the service {}", SERVICE_NAME);
85    service.delete()?;
86
87    Ok(())
88}
89
90#[cfg(not(windows))]
91fn main() {
92    panic!("This program is only intended to run on Windows.");
93}