github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runners/clean/stop.go (about)

     1  package clean
     2  
     3  import (
     4  	"github.com/ActiveState/cli/internal/constants"
     5  	"github.com/ActiveState/cli/internal/errs"
     6  	"github.com/ActiveState/cli/internal/fileutils"
     7  	"github.com/ActiveState/cli/internal/installation"
     8  	"github.com/ActiveState/cli/internal/locale"
     9  	"github.com/ActiveState/cli/internal/osutils"
    10  	"github.com/ActiveState/cli/internal/output"
    11  	"github.com/ActiveState/cli/internal/svcctl"
    12  )
    13  
    14  func stopServices(cfg configurable, out output.Outputer, ipComm svcctl.IPCommunicator, ignoreErrors bool) error {
    15  	cleanForceTip := locale.Tl("clean_force_tip", "You can re-run the command with the [ACTIONABLE]--force[/RESET] flag.")
    16  
    17  	// On Windows we need to halt the state service before we can remove them
    18  	svcExec, err := installation.ServiceExec()
    19  	if err != nil {
    20  		return locale.WrapError(err, "err_service_exec")
    21  	}
    22  
    23  	// Stop state-svc before accessing its files
    24  	if fileutils.FileExists(svcExec) {
    25  		code, _, err := osutils.Execute(svcExec, []string{"stop"}, nil)
    26  		if err != nil {
    27  			if !ignoreErrors {
    28  				return errs.AddTips(
    29  					locale.WrapError(err, "clean_stop_svc_failure", "Cleanup interrupted, because a running {{.V0}} process could not be stopped.", constants.SvcAppName),
    30  					cleanForceTip)
    31  			}
    32  			out.Error(locale.Tl("clean_stop_svc_warning", "Failed to stop running {{.V0}} process. Continuing anyway because --force flag was provided.", constants.SvcAppName))
    33  		}
    34  		if code != 0 {
    35  			if !ignoreErrors {
    36  				return errs.AddTips(
    37  					locale.WrapError(err, "clean_stop_svc_failure_code", "Cleanup interrupted, because a running {{.V0}} process could not be stopped (invalid exit code).", constants.SvcAppName),
    38  					cleanForceTip)
    39  			}
    40  			out.Error(locale.Tl("clean_stop_svc_warning_code", "Failed to stop running {{.V0}} process (invalid exit code). Continuing anyway because --force flag was provided.", constants.SvcAppName))
    41  		}
    42  
    43  		if err := svcctl.StopServer(ipComm); err != nil {
    44  			if !ignoreErrors {
    45  				return errs.AddTips(
    46  					locale.WrapError(err, "clean_stop_svc_failure_wait", "Cleanup interrupted, because a running {{.V0}} process failed to stop due to a timeout.", constants.SvcAppName),
    47  					cleanForceTip)
    48  			}
    49  			out.Error(locale.Tl("clean_stop_svc_warning_code", "Failed to stop running {{.V0}} process due to a timeout. Continuing anyway because --force flag was provided.", constants.SvcAppName))
    50  		}
    51  	}
    52  	return nil
    53  }