Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Handle() callback to support Service-specific signal handling #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 15 additions & 3 deletions svc_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,23 @@ 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 {
// this maintains backwards compatibility for Services that do not implement Handle()
goto stop
}
case <-ctx.Done():
goto stop
}
}

stop:
return service.Stop()
}

Expand Down
14 changes: 6 additions & 8 deletions svc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,11 @@ func Run(service Service, sig ...os.Signal) error {
sig = []os.Signal{syscall.SIGINT}
}

var ctx context.Context
if s, ok := service.(Context); ok {
ctx = s.Context()
} else {
ctx = context.Background()
}

ws := &windowsService{
i: service,
isWindowsService: isWindowsService,
signals: sig,
ctx: ctx,
ctx: context.Background(),
}

if ws.IsWindowsService() {
Expand All @@ -79,6 +72,11 @@ func Run(service Service, sig ...os.Signal) error {
return err
}

// call service.Context() after service.Init()
if s, ok := service.(Context); ok {
ws.ctx = s.Context()
}

return ws.run()
}

Expand Down