Skip to content

Commit

Permalink
add Handle() callback to support Service-specific signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Aug 15, 2021
1 parent a2deda2 commit 3bf765d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 12 additions & 1 deletion svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ package svc

import (
"context"
"errors"
"os"
"os/signal"
)

// Create variable signal.Notify function so we can mock it in tests
var signalNotify = signal.Notify

var ErrStop = errors.New("stopping service")

// Service interface contains Start and Stop methods which are called
// when the service is started and stopped. The Init method is called
// before the service is started, and after it's determined if the program
Expand All @@ -41,11 +45,18 @@ type Service interface {
// Start is called after Init. This method must be non-blocking.
Start() error

// Stop is called in response to syscall.SIGINT, syscall.SIGTERM, or when a
// Stop is called when Handle() returns ErrStop or when a
// Windows Service is stopped.
Stop() error
}

// Handler is an optional interface a Service can implement.
// When implemented, Handle() is called when a signal is received.
// Returning ErrStop from this method will result in Service.Stop() being called.
type Handler interface {
Handle(os.Signal) error
}

// Context interface contains an optional Context function which a Service can implement.
// When implemented the context.Done() channel will be used in addition to signal handling
// to exit a process.
Expand Down
17 changes: 14 additions & 3 deletions svc_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ func Run(service Service, sig ...os.Signal) error {
ctx = context.Background()
}

select {
case <-signalChan:
case <-ctx.Done():
for {
select {
case s := <-signalChan:
if h, ok := service.(Handler); ok {
if err := h.Handle(s); err == ErrStop {
goto stop
}
} else {
goto stop
}
case <-ctx.Done():
goto stop
}
}

stop:
return service.Stop()
}

Expand Down

0 comments on commit 3bf765d

Please sign in to comment.