-
Notifications
You must be signed in to change notification settings - Fork 115
/
dummy_nats_job_supervisor.go
167 lines (140 loc) · 3.44 KB
/
dummy_nats_job_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
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
package jobsupervisor
import (
"encoding/json"
boshalert "github.com/cloudfoundry/bosh-agent/agent/alert"
boshhandler "github.com/cloudfoundry/bosh-agent/handler"
bosherror "github.com/cloudfoundry/bosh-utils/errors"
)
type dummyNatsJobSupervisor struct {
mbusHandler boshhandler.Handler
status string
processes []Process
jobFailureHandler JobFailureHandler
}
func NewDummyNatsJobSupervisor(mbusHandler boshhandler.Handler) JobSupervisor {
return &dummyNatsJobSupervisor{
mbusHandler: mbusHandler,
status: "running",
processes: []Process{
Process{
Name: "process-1",
State: "running",
Uptime: UptimeVitals{
Secs: 144987,
},
Memory: MemoryVitals{
Kb: 100,
Percent: 0.1,
},
CPU: CPUVitals{
Total: 0.1,
},
},
Process{
Name: "process-2",
State: "running",
Uptime: UptimeVitals{
Secs: 144988,
},
Memory: MemoryVitals{
Kb: 200,
Percent: 0.2,
},
CPU: CPUVitals{
Total: 0.2,
},
},
Process{
Name: "process-3",
State: "failing",
Uptime: UptimeVitals{
Secs: 144989,
},
Memory: MemoryVitals{
Kb: 300,
Percent: 0.3,
},
CPU: CPUVitals{
Total: 0.3,
},
},
},
}
}
func (d *dummyNatsJobSupervisor) Reload() error {
return nil
}
func (d *dummyNatsJobSupervisor) AddJob(jobName string, jobIndex int, configPath string) error {
return nil
}
func (d *dummyNatsJobSupervisor) Start() error {
if d.status == "fail_task" {
return bosherror.Error("fake-task-fail-error")
}
if d.status != "failing" {
d.status = "running"
}
return nil
}
func (d *dummyNatsJobSupervisor) Stop() error {
if d.status != "failing" && d.status != "fail_task" {
d.status = "stopped"
}
return nil
}
func (d *dummyNatsJobSupervisor) StopAndWait() error {
return d.Stop()
}
func (d *dummyNatsJobSupervisor) Unmonitor() error {
return nil
}
func (d *dummyNatsJobSupervisor) RemoveAllJobs() error {
return nil
}
func (d *dummyNatsJobSupervisor) Status() string {
return d.status
}
func (d *dummyNatsJobSupervisor) Processes() ([]Process, error) {
return d.processes, nil
}
func (d *dummyNatsJobSupervisor) MonitorJobFailures(handler JobFailureHandler) error {
d.jobFailureHandler = handler
d.mbusHandler.RegisterAdditionalFunc(d.statusHandler)
return nil
}
func (d *dummyNatsJobSupervisor) statusHandler(req boshhandler.Request) boshhandler.Response {
switch req.Method {
case "set_dummy_status":
// Do not unmarshal message until determining its method
var body map[string]string
err := json.Unmarshal(req.GetPayload(), &body)
if err != nil {
return boshhandler.NewExceptionResponse(err)
}
d.status = body["status"]
if d.status == "failing" && d.jobFailureHandler != nil {
_ = d.jobFailureHandler(boshalert.MonitAlert{
ID: "fake-monit-alert",
Service: "fake-monit-service",
Event: "failing",
Action: "start",
Date: "Sun, 22 May 2011 20:07:41 +0500",
Description: "fake-monit-description",
})
}
return boshhandler.NewValueResponse("ok")
case "set_task_fail":
// Do not unmarshal message until determining its method
var body map[string]string
err := json.Unmarshal(req.GetPayload(), &body)
if err != nil {
return boshhandler.NewExceptionResponse(err)
}
d.status = body["status"]
return nil
default:
return nil
}
}
func (d *dummyNatsJobSupervisor) HealthRecorder(status string) {
}