Skip to content

Commit

Permalink
Shutdowner interface and Windows trigger (#225)
Browse files Browse the repository at this point in the history
* Shutdowner interface and Windows trigger

* Updating Shutdown doc

Stop won't be called when Shutdown is.

Co-authored-by: Juan Hernandez <jhernandez@newrelic.com>
  • Loading branch information
varas and varas committed Nov 16, 2020
1 parent 258d7b2 commit 41add7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 10 additions & 0 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 41add7e

Please sign in to comment.