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