Skip to content

Commit c9deb0e

Browse files
committed
add stop delegate
1 parent f609963 commit c9deb0e

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

Service/system-service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type SystemService interface {
1212
Start() error
1313
Restart() error
1414
Stop() error
15-
StopWithDelegate(onStopDelegate func()) error
1615
Uninstall() error
1716
Status() (ServiceStatus, error)
1817
Exists() bool
@@ -40,6 +39,7 @@ type ServiceCommand struct {
4039
StartDelayInSeconds int
4140
RunAsUser string
4241
RunAsGroup string
42+
OnStopDelegate func()
4343
}
4444

4545
func (thisRef ServiceCommand) String() string {

Service/system-service_windows.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,12 @@ import (
2121
// *WindowsService - Represents Windows service
2222
type *WindowsService struct {
2323
command ServiceCommand
24-
25-
onStopDelegate func()
26-
onStopDelegateSync *sync.RWMutex
2724
}
2825

2926
// New -
3027
func New(command ServiceCommand) SystemService {
3128
return &*WindowsService{
32-
command: command,
33-
onStopDelegate: nil,
34-
onStopDelegateSync: &sync.RWMutex{},
29+
command: command,
3530
}
3631
}
3732

@@ -222,16 +217,12 @@ func (thisRef *WindowsService) Restart() error {
222217
}
223218

224219
// Stop -
225-
func (thisRef **WindowsService) Stop(onStopDelegate func()) error {
220+
func (thisRef **WindowsService) Stop() error {
226221
logging.Instance().LogDebugWithFields(loggingC.Fields{
227222
"method": helpersReflect.GetThisFuncName(),
228223
"message": fmt.Sprint("attempting to stop: ", thisRef.command.Name),
229224
})
230225

231-
thisRef.onStopDelegateSync.Lock()
232-
thisRef.onStopDelegate = onStopDelegate
233-
thisRef.onStopDelegateSync.Unlock()
234-
235226
err := thisRef.control(svc.Stop, svc.Stopped)
236227
if err != nil {
237228
e := err.Error()
@@ -404,11 +395,9 @@ loop:
404395

405396
case svc.Stop, svc.Shutdown:
406397

407-
thisRef.onStopDelegateSync.Lock()
408-
defer thisRef.onStopDelegateSync.Unlock()
409-
if thisRef.onStopDelegate != nil {
410-
thisRef.onStopDelegate()
411-
thisRef.onStopDelegate = nil
398+
if thisRef.command.OnStopDelegate != nil {
399+
thisRef.command.OnStopDelegate()
400+
thisRef.command.OnStopDelegate = nil
412401
}
413402

414403
// golang.org/x/sys/windows/svc.TestExample is verifying this output.

0 commit comments

Comments
 (0)