forked from mmanda-extr/cdule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_watcher.go
49 lines (42 loc) · 948 Bytes
/
worker_watcher.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
package cdule
import (
"sync"
"time"
"github.com/gagasdiv/cdule/pkg/model"
log "github.com/sirupsen/logrus"
)
// WorkerWatcher struct
type WorkerWatcher struct {
Closed chan struct{}
WG sync.WaitGroup
Ticker *time.Ticker
}
// Run to run watcher in a continuous loop
func (t *WorkerWatcher) Run() {
for {
select {
case <-t.Closed:
return
case <-t.Ticker.C:
healthCheckUpdate()
}
}
}
// Stop to stop worker watcher
func (t *WorkerWatcher) Stop() {
close(t.Closed)
t.WG.Wait()
}
func healthCheckUpdate() {
worker, err := model.CduleRepos.CduleRepository.GetWorker(WorkerID)
if nil != err {
log.Errorf("Error getting workder %s ", err.Error())
}
if nil != worker {
worker.UpdatedAt = time.Now()
model.CduleRepos.CduleRepository.UpdateWorker(worker)
log.Debugf("Health check updated for worker_id %s updated", WorkerID)
return
}
log.Warningf("Health check update failed for worker_id %s", WorkerID)
}