This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.go
More file actions
146 lines (128 loc) · 3.39 KB
/
Copy pathoperator.go
File metadata and controls
146 lines (128 loc) · 3.39 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package gomultitask
import (
"context"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/andrskom/gomultitask/task"
)
const defaultShutdownDeadline = 30 * time.Second
// Operator is main struct for managing tasks
type Operator struct {
log Logger
tasks []*task.Task
notHandledErr chan task.Err
sigCh chan os.Signal
errCh chan error
quitCh chan struct{}
shutdownSignals []os.Signal
shutdownDeadline time.Duration
}
// NewOperator init default operator for tasks
func NewOperator(list ...task.Interface) *Operator {
taskList := make([]*task.Task, 0)
notHandledErr := make(chan task.Err, 5)
for _, t := range list {
taskList = append(taskList, task.NewFromInterface(notHandledErr, t))
}
return &Operator{
tasks: taskList,
notHandledErr: notHandledErr,
sigCh: make(chan os.Signal, 1),
errCh: make(chan error),
quitCh: make(chan struct{}),
shutdownSignals: []os.Signal{syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT},
shutdownDeadline: defaultShutdownDeadline,
}
}
// WithLogger add logger
func (o *Operator) WithLogger(log Logger) *Operator {
o.log = log
return o
}
// WithShutdownDeadline add shutdown deadline, default is 30s
func (o *Operator) WithShutdownDeadline(duration time.Duration) *Operator {
o.shutdownDeadline = duration
return o
}
// WithShutdownSignals set custom shutdown signals
func (o *Operator) WithShutdownSignals(signals []os.Signal) *Operator {
o.shutdownSignals = signals
return o
}
// Run tasks and wait while stop
func (o *Operator) Run(ctx context.Context) error {
// signals catcher
signal.Notify(o.sigCh, o.shutdownSignals...)
// internal context for supply routines
internalCtx, cancelF := context.WithCancel(ctx)
defer cancelF()
// init background notHandledErr logger
go o.logNotHandledErr(internalCtx)
// run all tasks
for _, t := range o.tasks {
go func(t *task.Task) {
if err := t.Run(ctx); err != nil {
o.errCh <- err
}
}(t)
}
// wait signal or error group
go o.waitEnd(context.Background())
// wait end of graceful shutdown
<-o.quitCh
return nil
}
func (o *Operator) waitEnd(ctx context.Context) {
select {
case sig := <-o.sigCh:
o.logInfof("Signal caught: %s", sig.String())
o.shutdown(ctx)
case err := <-o.errCh:
o.logErrorf("Error in group caught: %s", err.Error())
o.shutdown(ctx)
}
}
func (o *Operator) shutdown(ctx context.Context) {
var wg sync.WaitGroup
var shutdownErrCount int64
for _, t := range o.tasks {
wg.Add(1)
go func(t *task.Task) {
defer wg.Done()
if err := t.Shutdown(ctx); err != nil {
atomic.AddInt64(&shutdownErrCount, 1)
o.logErrorf("Shutdown task ID %s, err %s", t.GetID(), err.Error())
}
}(t)
}
shutdownFinishedCH := make(chan struct{})
go func() {
wg.Wait()
shutdownFinishedCH <- struct{}{}
}()
select {
case <-shutdownFinishedCH:
if shutdownErrCount > 0 {
o.logErrorf("Have got %d errors while shutdown tasks", shutdownErrCount)
break
}
o.logInfof("All graceful shutdowned")
case <-time.After(o.shutdownDeadline):
o.logErrorf("Deadline for graceful shutdown is reached")
}
o.quitCh <- struct{}{}
}
func (o *Operator) logNotHandledErr(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case err := <-o.notHandledErr:
o.logErrorf("ID: %s, FallNumber: %d, Err: %#v", err.ID, err.FallNumber, err.Err)
}
}
}