forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper_job_supervisor.go
90 lines (78 loc) · 2.48 KB
/
wrapper_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
package jobsupervisor
import (
//boshmonit "github.com/cloudfoundry/bosh-agent/jobsupervisor/monit"
//boshdir "github.com/cloudfoundry/bosh-agent/settings/directories"
//boshlog "github.com/cloudfoundry/bosh-utils/logger"
//boshsys "github.com/cloudfoundry/bosh-utils/system"
"encoding/json"
"github.com/cloudfoundry/bosh-agent/settings/directories"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
"github.com/cloudfoundry/bosh-utils/system"
"path/filepath"
)
const wrapperJobSupervisorLogTag = "wrapperJobSupervisor"
type wrapperJobSupervisor struct {
delegate JobSupervisor
fs system.FileSystem
dirProvider directories.Provider
logger boshlog.Logger
pollRunning bool
pollUnmonitor bool
}
func NewWrapperJobSupervisor(delegate JobSupervisor, fs system.FileSystem, dirProvider directories.Provider, logger boshlog.Logger) JobSupervisor {
return &wrapperJobSupervisor{
delegate: delegate,
fs: fs,
dirProvider: dirProvider,
logger: logger,
}
}
func (w *wrapperJobSupervisor) Reload() error {
return w.delegate.Reload()
}
func (w *wrapperJobSupervisor) Start() error {
err := w.delegate.Start()
w.HealthRecorder(w.delegate.Status())
return err
}
func (w *wrapperJobSupervisor) Stop() error {
err := w.delegate.Stop()
w.HealthRecorder(w.delegate.Status())
return err
}
func (w *wrapperJobSupervisor) StopAndWait() error {
return w.delegate.StopAndWait()
}
func (w *wrapperJobSupervisor) Unmonitor() error {
err := w.delegate.Unmonitor()
if err != nil {
return err
}
w.HealthRecorder(w.delegate.Status())
return err
}
func (w *wrapperJobSupervisor) Status() string {
return w.delegate.Status()
}
func (w *wrapperJobSupervisor) Processes() ([]Process, error) {
return w.delegate.Processes()
}
func (w *wrapperJobSupervisor) AddJob(jobName string, jobIndex int, configPath string) error {
return w.delegate.AddJob(jobName, jobIndex, configPath)
}
func (w *wrapperJobSupervisor) RemoveAllJobs() error {
return w.delegate.RemoveAllJobs()
}
func (w *wrapperJobSupervisor) MonitorJobFailures(handler JobFailureHandler) error {
return w.delegate.MonitorJobFailures(handler)
}
func (w *wrapperJobSupervisor) HealthRecorder(status string) {
healthRaw, err := json.Marshal(Health{State: status})
if err != nil {
w.logger.Error(wrapperJobSupervisorLogTag, err.Error())
}
err = w.fs.WriteFile(filepath.Join(w.dirProvider.InstanceDir(), "health.json"), healthRaw)
if err != nil {
w.logger.Error(wrapperJobSupervisorLogTag, err.Error())
}
}