-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctrlc.go
60 lines (52 loc) · 1.35 KB
/
ctrlc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Package ctrlc provides an easy way of having a task that is
// context-aware and that also deals with interrupt and term signals.
package ctrlc
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
)
// Task is function that can be executed by a ctrlc
type Task func() error
// Ctrlc is the task ctrlc
type Ctrlc struct {
signals chan os.Signal
errs chan error
}
// New returns a new ctrlc with its internals setup.
func New() *Ctrlc {
return &Ctrlc{
signals: make(chan os.Signal, 1),
errs: make(chan error, 1),
}
}
// Default ctrlc instance
var Default = New()
// ErrorCtrlC happens when ctrlc gets a signal.
type ErrorCtrlC struct {
sig os.Signal
}
func (e ErrorCtrlC) Error() string {
return fmt.Sprintf("ctrlc: received signal: %s", e.sig)
}
// Run executes a given task with a given context, dealing with its timeouts,
// cancels and SIGTERM and SIGINT signals.
// It will return an error if the context is canceled, if deadline exceeds,
// if a SIGTERM or SIGINT is received and of course if the task itself fails.
func (c *Ctrlc) Run(ctx context.Context, task Task) error {
go func() {
c.errs <- task()
}()
signal.Notify(c.signals, syscall.SIGINT, syscall.SIGTERM)
defer signal.Stop(c.signals)
select {
case err := <-c.errs:
return err
case <-ctx.Done():
return ctx.Err()
case sig := <-c.signals:
return ErrorCtrlC{sig}
}
}