-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shutdown.go
79 lines (66 loc) · 1.68 KB
/
Shutdown.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
package test
import (
"context"
"io"
"math/rand"
"os"
"time"
"github.com/pip-services3-gox/pip-services3-commons-gox/config"
)
type Shutdown struct {
started bool
runCode int
mode string
minTimeout int
maxTimeout int
}
func NewShutdown() *Shutdown {
return &Shutdown{
started: false,
runCode: 0,
mode: "exception",
minTimeout: 300000,
maxTimeout: 900000,
}
}
func (c *Shutdown) Configure(ctx context.Context, config config.ConfigParams) {
c.mode = config.GetAsStringWithDefault("mode", c.mode)
c.minTimeout = config.GetAsIntegerWithDefault("min_timeout", c.minTimeout)
c.maxTimeout = config.GetAsIntegerWithDefault("max_timeout", c.maxTimeout)
}
func (c *Shutdown) IsOpen() bool {
return c.started
}
func (c *Shutdown) Open(ctx context.Context, correlationId string) error {
if c.started {
return nil
}
delay := int(float32(c.maxTimeout-c.minTimeout)*rand.Float32() + float32(c.minTimeout))
c.runCode++
go c.doShutdown(ctx, delay, c.runCode)
c.started = true
return nil
}
func (c *Shutdown) Close(ctx context.Context, correlationId string) error {
// Todo: Properly interrupt the go proc
c.started = false
return nil
}
func (c *Shutdown) Shutdown(ctx context.Context) {
if c.mode == "null" || c.mode == "nullpointer" {
var obj io.Writer
obj.Write([]byte{})
} else if c.mode == "zero" || c.mode == "dividebyzero" {
_ = 0 / 100
} else if c.mode == "exit" || c.mode == "processexit" {
os.Exit(1)
} else {
panic("Crash test exception")
}
}
func (c *Shutdown) doShutdown(ctx context.Context, delay int, runCode int) {
time.Sleep(time.Duration(delay) * time.Millisecond)
if c.started && c.runCode == runCode {
c.Shutdown(ctx)
}
}