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 <jhernandez@newrelic.com>
This commit is contained in:
jhvaras
2020-11-16 15:58:05 +01:00
committed by GitHub
parent 258d7b252b
commit 41add7e3c9
2 changed files with 24 additions and 1 deletions
+10
View File
@@ -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.
+14 -1
View File
@@ -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
}