/
broker.go
526 lines (460 loc) · 13.4 KB
/
broker.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package job
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"time"
"github.com/cozy/cozy-stack/model/permission"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/couchdb/mango"
"github.com/cozy/cozy-stack/pkg/logger"
"github.com/cozy/cozy-stack/pkg/prefixer"
"github.com/cozy/cozy-stack/pkg/realtime"
)
const (
// Queued state
Queued State = "queued"
// Running state
Running State = "running"
// Done state
Done State = "done"
// Errored state
Errored State = "errored"
)
// defaultMaxLimits defines the maximum limit of how much jobs will be returned
// for each job state
var defaultMaxLimits map[State]int = map[State]int{
Queued: 50,
Running: 50,
Done: 50,
Errored: 50,
}
type (
// Broker interface is used to represent a job broker associated to a
// particular domain. A broker can be used to create jobs that are pushed in
// the job system.
//
// This interface is matched by several implementations:
// - [BrokerMock] a mock implementation used for the tests.
Broker interface {
StartWorkers(workersList WorkersList) error
ShutdownWorkers(ctx context.Context) error
// PushJob will push try to push a new job from the specified job request.
// This method is asynchronous.
PushJob(db prefixer.Prefixer, request *JobRequest) (*Job, error)
// WorkerQueueLen returns the total element in the queue of the specified
// worker type.
WorkerQueueLen(workerType string) (int, error)
// WorkerIsReserved returns true if the given worker type is reserved
// (ie clients should not push jobs to it, only the stack).
WorkerIsReserved(workerType string) (bool, error)
// WorkersTypes returns the list of registered workers types.
WorkersTypes() []string
}
// State represent the state of a job.
State string
// Message is a json encoded job message.
Message json.RawMessage
// Event is a json encoded value of a realtime.Event.
Event json.RawMessage
// Payload is a json encode value of a webhook payload.
Payload json.RawMessage
// Job contains all the metadata informations of a Job. It can be
// marshalled in JSON.
Job struct {
JobID string `json:"_id,omitempty"`
JobRev string `json:"_rev,omitempty"`
Cluster int `json:"couch_cluster,omitempty"`
Domain string `json:"domain"`
Prefix string `json:"prefix,omitempty"`
WorkerType string `json:"worker"`
TriggerID string `json:"trigger_id,omitempty"`
Message Message `json:"message"`
Event Event `json:"event"`
Payload Payload `json:"payload,omitempty"`
Manual bool `json:"manual_execution,omitempty"`
Debounced bool `json:"debounced,omitempty"`
Options *JobOptions `json:"options,omitempty"`
State State `json:"state"`
QueuedAt time.Time `json:"queued_at"`
StartedAt time.Time `json:"started_at"`
FinishedAt time.Time `json:"finished_at"`
Error string `json:"error,omitempty"`
ForwardLogs bool `json:"forward_logs,omitempty"`
}
// JobRequest struct is used to represent a new job request.
JobRequest struct {
WorkerType string
TriggerID string
Trigger Trigger
Message Message
Event Event
Payload Payload
Manual bool
Debounced bool
ForwardLogs bool
Options *JobOptions
}
// JobOptions struct contains the execution properties of the jobs.
JobOptions struct {
MaxExecCount int `json:"max_exec_count"`
Timeout time.Duration `json:"timeout"`
}
)
var joblog = logger.WithNamespace("jobs")
// DBCluster implements the prefixer.Prefixer interface.
func (j *Job) DBCluster() int {
return j.Cluster
}
// DBPrefix implements the prefixer.Prefixer interface.
func (j *Job) DBPrefix() string {
if j.Prefix != "" {
return j.Prefix
}
return j.Domain
}
// DomainName implements the prefixer.Prefixer interface.
func (j *Job) DomainName() string {
return j.Domain
}
// ID implements the couchdb.Doc interface
func (j *Job) ID() string { return j.JobID }
// Rev implements the couchdb.Doc interface
func (j *Job) Rev() string { return j.JobRev }
// Clone implements the couchdb.Doc interface
func (j *Job) Clone() couchdb.Doc {
cloned := *j
if j.Options != nil {
tmp := *j.Options
cloned.Options = &tmp
}
if j.Message != nil {
tmp := j.Message
j.Message = make([]byte, len(tmp))
copy(j.Message[:], tmp)
}
if j.Event != nil {
tmp := j.Event
j.Event = make([]byte, len(tmp))
copy(j.Event[:], tmp)
}
if j.Payload != nil {
tmp := j.Payload
j.Payload = make([]byte, len(tmp))
copy(j.Payload[:], tmp)
}
return &cloned
}
// DocType implements the couchdb.Doc interface
func (j *Job) DocType() string { return consts.Jobs }
// SetID implements the couchdb.Doc interface
func (j *Job) SetID(id string) { j.JobID = id }
// SetRev implements the couchdb.Doc interface
func (j *Job) SetRev(rev string) { j.JobRev = rev }
// Fetch implements the permission.Fetcher interface
func (j *Job) Fetch(field string) []string {
switch field {
case "worker":
return []string{j.WorkerType}
case "state":
return []string{fmt.Sprintf("%v", j.State)}
}
return nil
}
// ID implements the permission.Getter interface
func (jr *JobRequest) ID() string { return "" }
// DocType implements the permission.Getter interface
func (jr *JobRequest) DocType() string { return consts.Jobs }
// Fetch implements the permission.Fetcher interface
func (jr *JobRequest) Fetch(field string) []string {
switch field {
case "worker":
return []string{jr.WorkerType}
default:
return nil
}
}
// Logger returns a logger associated with the job domain
func (j *Job) Logger() *logger.Entry {
return logger.WithDomain(j.Domain).WithNamespace("jobs")
}
// AckConsumed sets the job infos state to Running an sends the new job infos
// on the channel.
func (j *Job) AckConsumed() error {
j.Logger().Debugf("ack_consume %s", j.ID())
j.StartedAt = time.Now()
j.State = Running
return j.Update()
}
// Ack sets the job infos state to Done an sends the new job infos on the
// channel.
func (j *Job) Ack() error {
j.Logger().Debugf("ack %s", j.ID())
j.FinishedAt = time.Now()
j.State = Done
j.Event = nil
j.Payload = nil
return j.Update()
}
// Nack sets the job infos state to Errored, set the specified error has the
// error field and sends the new job infos on the channel.
func (j *Job) Nack(errorMessage string) error {
j.Logger().Debugf("nack %s", j.ID())
j.FinishedAt = time.Now()
j.State = Errored
j.Error = errorMessage
j.Event = nil
j.Payload = nil
return j.Update()
}
// Update updates the job in couchdb
func (j *Job) Update() error {
err := couchdb.UpdateDoc(j, j)
// XXX When a job for an import runs, the database for io.cozy.jobs is
// deleted, and we need to recreate the job, not just update it.
if couchdb.IsNotFoundError(err) {
j.SetID("")
j.SetRev("")
return j.Create()
}
return err
}
// Create creates the job in couchdb
func (j *Job) Create() error {
return couchdb.CreateDoc(j, j)
}
// WaitUntilDone will wait until the job is done. It will return an error if
// the job has failed. And there is a timeout (10 minutes).
func (j *Job) WaitUntilDone(db prefixer.Prefixer) error {
sub := realtime.GetHub().Subscriber(db)
defer sub.Close()
sub.Watch(j.DocType(), j.ID())
timeout := time.After(10 * time.Minute)
for {
select {
case e := <-sub.Channel:
state := Queued
if doc, ok := e.Doc.(*couchdb.JSONDoc); ok {
stateStr, _ := doc.M["state"].(string)
state = State(stateStr)
} else if doc, ok := e.Doc.(*realtime.JSONDoc); ok {
stateStr, _ := doc.M["state"].(string)
state = State(stateStr)
} else if doc, ok := e.Doc.(*Job); ok {
state = doc.State
}
switch state {
case Done:
return nil
case Errored:
return errors.New("The konnector failed on account deletion")
}
case <-timeout:
return nil
}
}
}
// UnmarshalJSON implements json.Unmarshaler on Message. It should be retro-
// compatible with the old Message representation { Data, Type }.
func (m *Message) UnmarshalJSON(data []byte) error {
// For retro-compatibility purposes
var mm struct {
Data []byte `json:"Data"`
Type string `json:"Type"`
}
if err := json.Unmarshal(data, &mm); err == nil && mm.Type == "json" {
var v json.RawMessage
if err = json.Unmarshal(mm.Data, &v); err != nil {
return err
}
*m = Message(v)
return nil
}
var v json.RawMessage
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*m = Message(v)
return nil
}
// MarshalJSON implements json.Marshaler on Message.
func (m Message) MarshalJSON() ([]byte, error) {
v := json.RawMessage(m)
return json.Marshal(v)
}
// NewJob creates a new Job instance from a job request.
func NewJob(db prefixer.Prefixer, req *JobRequest) *Job {
return &Job{
Cluster: db.DBCluster(),
Domain: db.DomainName(),
Prefix: db.DBPrefix(),
WorkerType: req.WorkerType,
TriggerID: req.TriggerID,
Manual: req.Manual,
Message: req.Message,
Debounced: req.Debounced,
Event: req.Event,
Payload: req.Payload,
Options: req.Options,
ForwardLogs: req.ForwardLogs,
State: Queued,
QueuedAt: time.Now(),
}
}
// Get returns the informations about a job.
func Get(db prefixer.Prefixer, jobID string) (*Job, error) {
var job Job
if err := couchdb.GetDoc(db, consts.Jobs, jobID, &job); err != nil {
if couchdb.IsNotFoundError(err) {
return nil, ErrNotFoundJob
}
return nil, err
}
return &job, nil
}
// GetQueuedJobs returns the list of jobs which states is "queued" or "running"
func GetQueuedJobs(db prefixer.Prefixer, workerType string) ([]*Job, error) {
var results []*Job
req := &couchdb.FindRequest{
UseIndex: "by-worker-and-state",
Selector: mango.And(
mango.Equal("worker", workerType),
mango.Exists("state"), // XXX it is needed by couchdb to use the index
mango.Or(
mango.Equal("state", Queued),
mango.Equal("state", Running),
),
),
Limit: 200,
}
err := couchdb.FindDocs(db, consts.Jobs, req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// GetAllJobs returns the list of all the jobs on the given instance.
func GetAllJobs(db prefixer.Prefixer) ([]*Job, error) {
var startkey string
var lastJob *Job
finalJobs := []*Job{}
remainingJobs := true
for remainingJobs {
jobs := []*Job{}
req := &couchdb.AllDocsRequest{
Limit: 10001,
StartKey: startkey,
}
err := couchdb.GetAllDocs(db, consts.Jobs, req, &jobs)
if err != nil {
return nil, err
}
if len(jobs) == 0 {
return finalJobs, nil
}
lastJob, jobs = jobs[len(jobs)-1], jobs[:len(jobs)-1]
// Startkey for the next request
startkey = lastJob.JobID
// Appending to the final jobs
finalJobs = append(finalJobs, jobs...)
// Only the startkey is present: we are in the last lap of the loop
// We have to append the startkey as the last element
if len(jobs) == 0 {
remainingJobs = false
finalJobs = append(finalJobs, lastJob)
}
}
return finalJobs, nil
}
// FilterJobsBeforeDate returns alls jobs queued before the specified date
func FilterJobsBeforeDate(jobs []*Job, date time.Time) []*Job {
b := []*Job{}
for _, x := range jobs {
if x.QueuedAt.Before(date) {
b = append(b, x)
}
}
return b
}
// FilterByWorkerAndState filters a job slice by its workerType and State
func FilterByWorkerAndState(jobs []*Job, workerType string, state State, limit int) []*Job {
returned := []*Job{}
for _, j := range jobs {
if j.WorkerType == workerType && j.State == state {
returned = append(returned, j)
if len(returned) == limit {
return returned
}
}
}
return returned
}
// GetLastsJobs returns the N lasts job of each state for an instance/worker
// type pair
func GetLastsJobs(jobs []*Job, workerType string) ([]*Job, error) {
var result []*Job
// Ordering by QueuedAt before filtering jobs
sort.Slice(jobs, func(i, j int) bool { return jobs[i].QueuedAt.Before(jobs[j].QueuedAt) })
for _, state := range []State{Queued, Running, Done, Errored} {
limit := defaultMaxLimits[state]
filtered := FilterByWorkerAndState(jobs, workerType, state, limit)
result = append(result, filtered...)
}
return result, nil
}
// NewMessage returns a json encoded data
func NewMessage(data interface{}) (Message, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
return Message(b), nil
}
// NewEvent return a json encoded realtime.Event
func NewEvent(data *realtime.Event) (Event, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
return Event(b), nil
}
// Unmarshal can be used to unmarshal the encoded message value in the
// specified interface's type.
func (m Message) Unmarshal(msg interface{}) error {
if m == nil {
return ErrMessageNil
}
if err := json.Unmarshal(m, &msg); err != nil {
return ErrMessageUnmarshal
}
return nil
}
// Unmarshal can be used to unmarshal the encoded message value in the
// specified interface's type.
func (e Event) Unmarshal(evt interface{}) error {
if e == nil {
return ErrMessageNil
}
if err := json.Unmarshal(e, &evt); err != nil {
return ErrMessageUnmarshal
}
return nil
}
// Unmarshal can be used to unmarshal the encoded message value in the
// specified interface's type.
func (p Payload) Unmarshal(evt interface{}) error {
if p == nil {
return ErrMessageNil
}
if err := json.Unmarshal(p, &evt); err != nil {
return ErrMessageUnmarshal
}
return nil
}
var (
_ permission.Fetcher = (*JobRequest)(nil)
_ permission.Fetcher = (*Job)(nil)
)