From 41add7e3c93376a631ebcfb4a7d79414dba7dbae Mon Sep 17 00:00:00 2001 From: jhvaras Date: Mon, 16 Nov 2020 15:58:05 +0100 Subject: [PATCH] Shutdowner interface and Windows trigger (#225) * Shutdowner interface and Windows trigger * Updating Shutdown doc Stop won't be called when Shutdown is. Co-authored-by: Juan Hernandez --- service.go | 10 ++++++++++ service_windows.go | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/service.go b/service.go index 2583bb1f..91a69944 100644 --- a/service.go +++ b/service.go @@ -328,6 +328,16 @@ type Interface interface { Stop(s Service) error } +// Shutdowner represents a service interface for a program that differentiates between "stop" and +// "shutdown". A shutdown is triggered when the whole box (not just the service) is stopped. +type Shutdowner interface { + Interface + // Shutdown provides a place to clean up program execution when the system is being shutdown. + // It is essentially the same as Stop but for the case where machine is being shutdown/restarted + // instead of just normally stopping the service. Stop won't be called when Shutdown is. + Shutdown(s Service) error +} + // TODO: Add Configure to Service interface. // Service represents a service that can be run or controlled. diff --git a/service_windows.go b/service_windows.go index 8a67ae0f..c6ee3a6c 100644 --- a/service_windows.go +++ b/service_windows.go @@ -176,13 +176,26 @@ loop: switch c.Cmd { case svc.Interrogate: changes <- c.CurrentStatus - case svc.Stop, svc.Shutdown: + case svc.Stop: changes <- svc.Status{State: svc.StopPending} if err := ws.i.Stop(ws); err != nil { ws.setError(err) return true, 2 } break loop + case svc.Shutdown: + changes <- svc.Status{State: svc.StopPending} + var err error + if wsShutdown, ok := ws.i.(Shutdowner); ok { + err = wsShutdown.Shutdown(ws) + } else { + err = ws.i.Stop(ws) + } + if err != nil { + ws.setError(err) + return true, 2 + } + break loop default: continue loop }