-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
44 lines (40 loc) · 931 Bytes
/
actions.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
package group
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
)
// Block creates a blocking executional group that waits until some other group
// action finishing, causing the closing of the group action.
func Block(g *Group) {
cancel := make(chan struct{})
g.Add(func(ctx context.Context) error {
<-cancel
return nil
}, func(error) {
close(cancel)
})
}
// Interrupt creates a blocking executional group that becomes free if a
// interupt or terminate os signal is received.
func Interrupt(g *Group) {
cancel := make(chan struct{})
g.Add(func(ctx context.Context) error {
return interrupt(cancel)
}, func(error) {
close(cancel)
})
}
func interrupt(cancel <-chan struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-c:
return fmt.Errorf("received signal %s", sig)
case <-cancel:
return errors.New("canceled")
}
}