"Fossies" - the Fresh Open Source Software Archive

Member "mattermost-server-6.0.1/vendor/github.com/oklog/run/actors.go" (18 Oct 2021, 949 Bytes) of package /linux/www/mattermost-server-6.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package run
    2 
    3 import (
    4     "context"
    5     "fmt"
    6     "os"
    7     "os/signal"
    8 )
    9 
   10 // SignalHandler returns an actor, i.e. an execute and interrupt func, that
   11 // terminates with SignalError when the process receives one of the provided
   12 // signals, or the parent context is canceled.
   13 func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() error, interrupt func(error)) {
   14     ctx, cancel := context.WithCancel(ctx)
   15     return func() error {
   16             c := make(chan os.Signal, 1)
   17             signal.Notify(c, signals...)
   18             select {
   19             case sig := <-c:
   20                 return SignalError{Signal: sig}
   21             case <-ctx.Done():
   22                 return ctx.Err()
   23             }
   24         }, func(error) {
   25             cancel()
   26         }
   27 }
   28 
   29 // SignalError is returned by the signal handler's execute function
   30 // when it terminates due to a received signal.
   31 type SignalError struct {
   32     Signal os.Signal
   33 }
   34 
   35 // Error implements the error interface.
   36 func (e SignalError) Error() string {
   37     return fmt.Sprintf("received signal %s", e.Signal)
   38 }