Skip to content

Commit

Permalink
WinSW uninstall deletes service directories (#24)
Browse files Browse the repository at this point in the history
With the WinSW service manager, each service definition has its own directory. In addition to the
service definition, WinSW also produces logs that get output to this directory. In our product, we
frequently create and delete services with the same names, so we found it problematic for these
directories to remain around.

The service manager is now updated to delete the service directory upon uninstall.
  • Loading branch information
jacderida committed May 22, 2024
1 parent 0329f6f commit 2cb4ca9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.6.2] - 2024-05-22

- The WinSW service manager will delete service directories upon uninstall

## [0.6.1] - 2024-05-03

- Fix issue where calling stop on MacOS service does not halt the service due to the service's default auto-restart setting. (#19)
Expand Down
19 changes: 13 additions & 6 deletions src/winsw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
use std::ffi::OsString;
use std::fs::File;
use std::io::{self, BufWriter, Cursor, Write};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use xml::common::XmlVersion;
use xml::reader::EventReader;
Expand Down Expand Up @@ -358,7 +358,7 @@ impl ServiceManager for WinSwServiceManager {
let service_config_path = service_instance_path.join(format!("{service_name}.xml"));
Self::write_service_configuration(&service_config_path, &ctx, &self.config)?;

winsw_exe("install", &service_name, service_instance_path)
winsw_exe("install", &service_name, &service_instance_path)
}

fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
Expand All @@ -367,7 +367,14 @@ impl ServiceManager for WinSwServiceManager {
.config
.service_definition_dir_path
.join(service_name.clone());
winsw_exe("uninstall", &service_name, service_instance_path)
winsw_exe("uninstall", &service_name, &service_instance_path)?;

// The service directory is populated with the service definition, and other log files that
// get generated by WinSW. It can be problematic if a service is later created with the
// same name. Things are easier to manage if the directory is deleted.
std::fs::remove_dir_all(service_instance_path)?;

Ok(())
}

fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
Expand All @@ -376,7 +383,7 @@ impl ServiceManager for WinSwServiceManager {
.config
.service_definition_dir_path
.join(service_name.clone());
winsw_exe("start", &service_name, service_instance_path)
winsw_exe("start", &service_name, &service_instance_path)
}

fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
Expand All @@ -385,7 +392,7 @@ impl ServiceManager for WinSwServiceManager {
.config
.service_definition_dir_path
.join(service_name.clone());
winsw_exe("stop", &service_name, service_instance_path)
winsw_exe("stop", &service_name, &service_instance_path)
}

fn level(&self) -> ServiceLevel {
Expand All @@ -403,7 +410,7 @@ impl ServiceManager for WinSwServiceManager {
}
}

fn winsw_exe(cmd: &str, service_name: &str, working_dir_path: PathBuf) -> io::Result<()> {
fn winsw_exe(cmd: &str, service_name: &str, working_dir_path: &Path) -> io::Result<()> {
let mut command = Command::new(WINSW_EXE);
command
.stdin(Stdio::null())
Expand Down

0 comments on commit 2cb4ca9

Please sign in to comment.