-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworker.go
More file actions
155 lines (121 loc) · 3 KB
/
worker.go
File metadata and controls
155 lines (121 loc) · 3 KB
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
package worker
import (
"context"
"encoding/json"
"exesh/internal/api/heartbeat"
"exesh/internal/config"
"exesh/internal/domain/execution"
"exesh/internal/lib/queue"
"log/slog"
"sync"
"time"
)
type (
Worker struct {
log *slog.Logger
cfg config.WorkConfig
heartbeatClient heartbeatClient
jobExecutor jobExecutor
jobs queue.Queue[execution.Job]
mu sync.Mutex
doneJobs []execution.Result
freeSlots int
}
heartbeatClient interface {
Heartbeat(context.Context, string, []execution.Result, int) ([]execution.Job, error)
}
jobExecutor interface {
Execute(context.Context, execution.Job) execution.Result
}
)
func NewWorker(log *slog.Logger, cfg config.WorkConfig, jobExecutor jobExecutor) *Worker {
return &Worker{
log: log,
cfg: cfg,
heartbeatClient: heartbeat.NewHeartbeatClient(cfg.CoordinatorEndpoint),
jobExecutor: jobExecutor,
jobs: *queue.NewQueue[execution.Job](),
mu: sync.Mutex{},
doneJobs: make([]execution.Result, 0),
freeSlots: cfg.FreeSlots,
}
}
func (w *Worker) Start(ctx context.Context) {
go w.runHeartbeat(ctx)
for range w.cfg.FreeSlots {
go w.runWorker(ctx)
}
}
func (w *Worker) runHeartbeat(ctx context.Context) {
for {
timer := time.NewTicker(w.cfg.HeartbeatDelay)
select {
case <-ctx.Done():
w.log.Info("exit heartbeat loop")
return
case <-timer.C:
break
}
if w.getFreeSlots() == 0 {
w.log.Debug("skip heartbeat loop (no free slots)")
continue
}
w.log.Debug("begin heartbeat loop")
w.mu.Lock()
doneJobs := make([]execution.Result, len(w.doneJobs))
copy(doneJobs, w.doneJobs)
w.doneJobs = make([]execution.Result, 0)
freeSlots := w.freeSlots - w.jobs.Size()
w.mu.Unlock()
jobs, err := w.heartbeatClient.Heartbeat(ctx, w.cfg.WorkerID, doneJobs, freeSlots)
if err != nil {
w.log.Error("failed to do heartbeat request", slog.Any("err", err))
w.mu.Lock()
w.doneJobs = append(w.doneJobs, doneJobs...)
w.mu.Unlock()
continue
}
for _, job := range jobs {
w.jobs.Enqueue(job)
}
}
}
func (w *Worker) runWorker(ctx context.Context) {
for {
timer := time.NewTicker(w.cfg.WorkerDelay)
select {
case <-ctx.Done():
w.log.Info("exit worker")
return
case <-timer.C:
break
}
w.changeFreeSlots(-1)
job := w.jobs.Dequeue()
if job == nil {
w.log.Debug("skip worker loop (no jobs to do)")
w.changeFreeSlots(+1)
continue
}
js, _ := json.Marshal(job)
w.log.Debug("picked job", slog.Any("job_id", (*job).GetID()), slog.String("job", string(js)))
result := w.jobExecutor.Execute(ctx, *job)
w.mu.Lock()
w.doneJobs = append(w.doneJobs, result)
w.mu.Unlock()
js, _ = json.Marshal(result)
w.log.Info("done job", slog.Any("job_id", (*job).GetID()), slog.Any("error", result.GetError()),
slog.Any("result", js))
w.changeFreeSlots(+1)
}
}
func (w *Worker) getFreeSlots() int {
w.mu.Lock()
defer w.mu.Unlock()
return w.freeSlots
}
func (w *Worker) changeFreeSlots(delta int) {
w.mu.Lock()
defer w.mu.Unlock()
w.freeSlots += delta
}