ant_service_management/
auditor.rs1use crate::{
10 control::ServiceControl, error::Result, ServiceStateActions, ServiceStatus, UpgradeOptions,
11};
12use async_trait::async_trait;
13use serde::{Deserialize, Serialize};
14use service_manager::ServiceInstallCtx;
15use std::{ffi::OsString, path::PathBuf};
16
17#[derive(Clone, Debug, Serialize, Deserialize)]
18pub struct AuditorServiceData {
19 pub auditor_path: PathBuf,
20 pub log_dir_path: PathBuf,
21 pub pid: Option<u32>,
22 pub service_name: String,
23 pub status: ServiceStatus,
24 pub user: String,
25 pub version: String,
26}
27
28pub struct AuditorService<'a> {
29 pub service_data: &'a mut AuditorServiceData,
30 pub service_control: Box<dyn ServiceControl + Send>,
31}
32
33impl<'a> AuditorService<'a> {
34 pub fn new(
35 service_data: &'a mut AuditorServiceData,
36 service_control: Box<dyn ServiceControl + Send>,
37 ) -> AuditorService<'a> {
38 AuditorService {
39 service_data,
40 service_control,
41 }
42 }
43}
44
45#[async_trait]
46impl ServiceStateActions for AuditorService<'_> {
47 fn bin_path(&self) -> PathBuf {
48 self.service_data.auditor_path.clone()
49 }
50
51 fn build_upgrade_install_context(&self, options: UpgradeOptions) -> Result<ServiceInstallCtx> {
52 let mut args = vec![
53 OsString::from("--log-output-dest"),
54 OsString::from(self.service_data.log_dir_path.to_string_lossy().to_string()),
55 ];
56
57 args.push(OsString::from("server"));
58
59 Ok(ServiceInstallCtx {
60 args,
61 autostart: true,
62 contents: None,
63 environment: options.env_variables,
64 label: self
65 .service_data
66 .service_name
67 .parse()
68 .inspect_err(|err| error!("Failed to parse service name: {err:?}"))?,
69 program: self.service_data.auditor_path.to_path_buf(),
70 username: Some(self.service_data.user.to_string()),
71 working_directory: None,
72 disable_restart_on_failure: false,
73 })
74 }
75
76 fn data_dir_path(&self) -> PathBuf {
77 PathBuf::new()
78 }
79
80 fn is_user_mode(&self) -> bool {
81 false
83 }
84
85 fn log_dir_path(&self) -> PathBuf {
86 PathBuf::new()
87 }
88
89 fn name(&self) -> String {
90 self.service_data.service_name.clone()
91 }
92
93 fn pid(&self) -> Option<u32> {
94 self.service_data.pid
95 }
96
97 fn on_remove(&mut self) {
98 self.service_data.status = ServiceStatus::Removed;
99 }
100
101 async fn on_start(&mut self, pid: Option<u32>, _full_refresh: bool) -> Result<()> {
102 self.service_data.pid = pid;
103 self.service_data.status = ServiceStatus::Running;
104 Ok(())
105 }
106
107 async fn on_stop(&mut self) -> Result<()> {
108 self.service_data.pid = None;
109 self.service_data.status = ServiceStatus::Stopped;
110 Ok(())
111 }
112
113 fn set_version(&mut self, version: &str) {
114 self.service_data.version = version.to_string();
115 }
116
117 fn status(&self) -> ServiceStatus {
118 self.service_data.status.clone()
119 }
120
121 fn version(&self) -> String {
122 self.service_data.version.clone()
123 }
124}