-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.go
More file actions
168 lines (138 loc) · 2.82 KB
/
runner.go
File metadata and controls
168 lines (138 loc) · 2.82 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package runner
import (
"errors"
"fmt"
"log"
"sync"
"sync/atomic"
)
type Closer func() error
type Runner func() error
type App struct {
Runners []Runner
Slams []Closer
Started chan struct{}
Done chan int
shutdowning chan struct{}
down *sync.WaitGroup
onceShutdown *sync.Once
onceSetExitCode *sync.Once
closed int32
exitCode uint8
errs chan error
}
func New() *App {
return &App{
Runners: make([]Runner, 0),
Slams: make([]Closer, 0),
Started: make(chan struct{}),
Done: make(chan int, 1),
down: &sync.WaitGroup{},
onceShutdown: &sync.Once{},
onceSetExitCode: &sync.Once{},
shutdowning: make(chan struct{}),
errs: make(chan error, 1),
closed: -1,
}
}
func (app *App) setExitCode(code uint8) {
app.onceSetExitCode.Do(func() {
app.exitCode = code
})
}
func (app *App) Run() {
var (
runnersLength = len(app.Runners)
up = &sync.WaitGroup{}
)
if len(app.Slams) == 0 {
log.Println(`WARN: your app doesn't have functions for close`)
}
app.errs = make(chan error, runnersLength)
app.down.Add(runnersLength)
up.Add(runnersLength)
for _, r := range app.Runners {
go func(run Runner) {
up.Done()
defer app.down.Done()
defer func() {
if e := recover(); e != nil {
app.errs <- errors.New(fmt.Sprint(e))
}
}()
if err := run(); err != nil {
app.errs <- err
}
}(r)
}
up.Wait()
if app.Started != nil {
close(app.Started)
}
// run catcher of error for shutdown application
go func() {
select {
case <-app.shutdowning:
return
case err, ok := <-app.errs:
app.setExitCode(1)
if ok {
// check shutdowning. may be it will be
select {
case <-app.shutdowning:
log.Println("ERR:", err)
return
default:
}
log.Println("ERR:", err)
app.Shutdown()
}
return
}
}()
}
func safelyCallCloser(fn Closer) (err error) {
defer func() {
if e := recover(); e != nil {
err = errors.New(fmt.Sprint(e))
}
}()
err = fn()
return err
}
func (app *App) Shutdown() {
if atomic.LoadInt32(&app.closed) != -1 {
select {
case app.Done <- int(app.closed):
default:
log.Println(`WARN: channel "Done" don't listen`)
}
log.Println(`WARN: app already closed`)
return
}
app.onceShutdown.Do(app.shutdown)
}
func (app *App) shutdown() {
close(app.shutdowning)
var err error
for i := len(app.Slams) - 1; i >= 0; i -= 1 {
if err = safelyCallCloser(app.Slams[i]); err != nil {
app.setExitCode(1)
log.Println("ERR:", err)
}
}
app.down.Wait()
close(app.errs)
for err = range app.errs {
if err != nil {
log.Println("ERR:", err)
}
}
if err != nil || app.exitCode == 1 {
atomic.AddInt32(&app.closed, 2)
app.Done <- 1
} else {
atomic.AddInt32(&app.closed, 1)
app.Done <- 0
}
}