This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
supervisor.go
115 lines (98 loc) · 2.72 KB
/
supervisor.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
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
// Package supervisor provides supervision utilities for running a function,
// and retry it if there is an error or panic, for a certain number of times (or infinitely),
// periodically, at the specified periods.
package supervisor
import (
"fmt"
"time"
"github.com/pkg/errors"
)
//-----------------------------------------------------------------------------
type supervisorConf struct {
intensity int
period time.Duration
onError func(error)
}
//-----------------------------------------------------------------------------
// Option represents a supervision option
type Option func(supervisorConf) supervisorConf
// Intensity of -1 means run worker forever.
func Intensity(intensity int) Option {
return func(sc supervisorConf) supervisorConf {
sc.intensity = intensity
return sc
}
}
// Period of time to sleep between restarts
func Period(period time.Duration) Option {
return func(sc supervisorConf) supervisorConf {
sc.period = period
return sc
}
}
// OnError will get called in case of an error
func OnError(onError func(error)) Option {
return func(sc supervisorConf) supervisorConf {
sc.onError = onError
return sc
}
}
//-----------------------------------------------------------------------------
// Supervise a helper method, runs in sync, use as "go Supervise(...)" if should run concurrently,
// takes care of restarts (in case of panic or error),
// can be used as a SimpleOneForOne supervisor, an intensity < 0 means restart forever.
// It is sync because in most cases we do not need to escape the scope of current
// goroutine.
func Supervise(
action func() error,
options ...Option) {
intensity, period, onError := validateAndRefine(options...)
for intensity != 0 {
if intensity > 0 {
intensity--
}
if err := run(action); err != nil {
if onError != nil {
onError(err)
}
if intensity != 0 {
time.Sleep(period)
}
} else {
break
}
}
}
// run calls the function, does captures panics
func run(action func() error) (errRun error) {
defer func() {
if e := recover(); e != nil {
if err, ok := e.(error); ok {
errRun = err
return
}
errRun = errors.Errorf("UNKNOWN: %+v", e)
}
}()
return action()
}
func validateAndRefine(options ...Option) (int, time.Duration, func(error)) {
var sc supervisorConf
for _, opt := range options {
sc = opt(sc)
}
if sc.intensity == 0 {
sc.intensity = 1
}
if sc.intensity != 1 && sc.period <= 0 {
sc.period = time.Second * 5
}
return sc.intensity,
sc.period,
sc.onError
}
//-----------------------------------------------------------------------------
func init() {
fmt.Println("warning: use package github.com/dc0d/retry instead.")
}
//-----------------------------------------------------------------------------