-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
823 lines (746 loc) · 19.4 KB
/
service.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
package api
import (
"fmt"
"log"
"time"
"github.com/voidshard/igor/internal/utils"
"github.com/voidshard/igor/pkg/database"
"github.com/voidshard/igor/pkg/database/changes"
"github.com/voidshard/igor/pkg/errors"
"github.com/voidshard/igor/pkg/queue"
"github.com/voidshard/igor/pkg/structs"
)
const (
// max values
maxNameLength = 500
maxTypeLength = 500
maxArgsLength = 10000
)
var (
incompleteStates = []structs.Status{
structs.PENDING,
structs.QUEUED,
structs.RUNNING,
}
defLimit = 2000
)
type Service struct {
db database.Database
qu queue.Queue
opts *Options
errs chan error
}
func New(dbopts *database.Options, queopts *queue.Options, opts *Options) (*Service, error) {
db, err := database.NewPostgres(dbopts)
if err != nil {
return nil, err
}
qu, err := queue.NewAsynqQueue(database.NewQueueDB(db), queopts)
if err != nil {
return nil, err
}
return newService(db, qu, opts)
}
func newService(db database.Database, qu queue.Queue, opts *Options) (*Service, error) {
me := &Service{db: db, qu: qu, opts: opts, errs: make(chan error)}
go func() {
for err := range me.errs {
if err != nil {
log.Println("[Service]", err)
}
}
}()
if opts.TidyRoutines > 0 {
me.startTidyRoutines()
}
if opts.EventRoutines > 0 {
me.startEventRoutines()
}
return me, nil
}
func (c *Service) startTidyRoutines() {
// Tidy work is internal busy work re-checking data to ensure it's in the correct state if it hasn't been
// updated in a while. This is guarding against dropped events / errors.
//
// The rate of re-processing here is limited to one routine that fetches data in batches and some number
// of worker routines.
//
// This means that hopefully even when processing at full speed we generate constant but a predictable
// load on the DB (and for the process as a whole).
//
// Note that these routines are seperate from dedicated event routines.
tidyLayerWork := make(chan *structs.Layer)
tidyTaskWork := make(chan *structs.Task)
go func() {
defer close(tidyLayerWork)
defer close(tidyTaskWork)
tickLyr := time.NewTicker(c.opts.TidyLayerFrequency)
tickTsk := time.NewTicker(c.opts.TidyTaskFrequency)
for {
select {
case <-tickLyr.C:
c.queueTidyLayerWork(c.errs, tidyLayerWork)
case <-tickTsk.C:
c.queueTidyTaskWork(c.errs, tidyTaskWork)
}
}
}()
for i := int64(0); i < c.opts.TidyRoutines; i++ {
go func() {
for {
// Nb. we could batch here, but since we simply re-call the handle event (which takes 1)
// the batch wouldn't acheive much.
select {
case w := <-tidyLayerWork:
c.errs <- c.handleLayerEvent(&changes.Change{Kind: structs.KindLayer, Old: w, New: w})
case w := <-tidyTaskWork:
c.errs <- c.handleTaskEvent(&changes.Change{Kind: structs.KindTask, Old: w, New: w})
}
}
}()
}
}
func (c *Service) startEventRoutines() {
// Rather than have each worker routine listen for events and deal with massive
// numbers of duplicates (and more work for the DB) we have a single routine
// that fetches events and passes them to a channel.
//
// We can still have multiple processes fetching events (ie. multiple igor services)
// but this greatly cuts down on duplicate work.
evtWork := make(chan *changes.Change)
go func() {
// Listen to changes. Retry on error(s) forever.
defer close(evtWork)
var stream changes.Stream
var err error
for {
stream, err = c.db.Changes()
if err != nil {
c.errs <- err
continue
}
for {
evt, err := stream.Next()
if err != nil {
c.errs <- err
break
}
if evt == nil {
return
}
evtWork <- evt
}
}
}()
for i := int64(0); i < c.opts.EventRoutines; i++ {
go func() {
c.handleEvents(c.errs, evtWork)
}()
}
}
func (c *Service) Close() error {
c.qu.Close()
c.db.Close()
return nil
}
func (c *Service) Register(task string, handler func(work []*queue.Meta) error) error {
return c.qu.Register(task, handler)
}
func (c *Service) Run() error {
return c.qu.Run()
}
func (c *Service) Pause(r []*structs.ObjectRef) (int64, error) {
return c.togglePause(time.Now().Unix(), r)
}
func (c *Service) Unpause(r []*structs.ObjectRef) (int64, error) {
return c.togglePause(0, r)
}
func (c *Service) togglePause(now int64, r []*structs.ObjectRef) (int64, error) {
byKind, err := validateToggles(r)
if err != nil {
return 0, err
}
var count int64
etag := utils.NewRandomID()
for k, toggles := range byKind {
switch k {
case structs.KindLayer:
delta, err := c.db.SetLayersPaused(now, etag, toggles)
count += delta
if err != nil {
return count, err
}
case structs.KindTask:
delta, err := c.db.SetTasksPaused(now, etag, toggles)
count += delta
if err != nil {
return count, err
}
default:
log.Println("pause not supported on kind", k)
}
}
return count, nil
}
func (c *Service) Retry(r []*structs.ObjectRef) (int64, error) {
byKind, err := validateToggles(r)
if err != nil {
return 0, err
}
var count int64
for k, toggles := range byKind {
switch k {
case structs.KindTask:
delta, err := c.retryTasks(toggles)
count += delta
if err != nil {
return count, err
}
default:
log.Println("retry not supported on kind", k)
}
}
return count, nil
}
func (c *Service) retryTasks(tt []*structs.ObjectRef) (int64, error) {
taskIds := []string{}
taskIdToEtag := map[string]string{}
for _, t := range tt {
taskIds = append(taskIds, t.ID)
taskIdToEtag[t.ID] = t.ETag
}
tasks, err := c.db.Tasks(&structs.Query{Limit: len(taskIds), TaskIDs: taskIds})
if err != nil {
return 0, err
}
enqueue := []*structs.Task{}
for _, t := range tasks {
expected, _ := taskIdToEtag[t.ID]
if t.ETag != expected { // task state has changed, user may no longer want to retry
continue
}
if t.QueueTaskID != "" { // task could be running: at least try to kill it
c.errs <- c.killTask(t)
}
enqueue = append(enqueue, t)
}
return int64(len(enqueue)), c.enqueueTasks(enqueue)
}
func (c *Service) Skip(r []*structs.ObjectRef) (int64, error) {
byKind, err := validateToggles(r)
if err != nil {
return 0, err
}
var count int64
etag := utils.NewRandomID()
for k, toggles := range byKind {
switch k {
case structs.KindLayer:
delta, err := c.db.SetLayersStatus(structs.SKIPPED, etag, toggles)
count += delta
if err != nil {
return count, err
}
case structs.KindTask:
delta, err := c.db.SetTasksStatus(structs.SKIPPED, etag, toggles)
count += delta
if err != nil {
return count, err
}
default:
log.Println("skip not supported on kind", k)
}
}
return count, nil
}
func (c *Service) Kill(r []*structs.ObjectRef) (int64, error) {
byKind, err := validateToggles(r)
if err != nil {
return 0, err
}
var count int64
etag := utils.NewRandomID()
for k, toggles := range byKind {
switch k {
case structs.KindTask:
delta, err := c.db.SetTasksStatus(structs.KILLED, etag, toggles)
count += delta
if err != nil {
return count, err
}
default:
log.Println("kill not supported on kind", k)
}
}
return count, nil
}
func (c *Service) Jobs(q *structs.Query) ([]*structs.Job, error) {
q.Sanitize()
return c.db.Jobs(q)
}
func (c *Service) Layers(q *structs.Query) ([]*structs.Layer, error) {
q.Sanitize()
return c.db.Layers(q)
}
func (c *Service) Tasks(q *structs.Query) ([]*structs.Task, error) {
q.Sanitize()
return c.db.Tasks(q)
}
func (c *Service) CreateTasks(in []*structs.CreateTaskRequest) ([]*structs.Task, error) {
// validate; the input must be valid and layers where given must exist
if in == nil || len(in) == 0 {
return nil, errors.ErrNoTasks
}
needNewJob := false
layersByID := map[string]*structs.Layer{}
layerIDs := []string{}
for _, t := range in {
err := validateTaskSpec(&t.TaskSpec)
if err != nil {
return nil, err
}
if t.LayerID == "" {
needNewJob = true
} else if utils.IsValidID(t.LayerID) {
if _, ok := layersByID[t.LayerID]; !ok {
layersByID[t.LayerID] = nil
layerIDs = append(layerIDs, t.LayerID)
}
} else {
return nil, fmt.Errorf("%w layer id %s is invalid", errors.ErrInvalidArg, t.LayerID)
}
}
// if we have layers to look up, do so
var layers []*structs.Layer
var err error
if len(layersByID) > 0 {
layers, err = c.db.Layers(&structs.Query{Limit: len(layerIDs), LayerIDs: layerIDs})
if err != nil {
return nil, err
}
if len(layers) != len(layerIDs) { // we require all the layers to exist
return nil, fmt.Errorf("%w expected %d layers, got %d", errors.ErrParentNotFound, len(layerIDs), len(layers))
}
for _, l := range layers {
layersByID[l.ID] = l
}
}
// now we can attempt to build everything
etag := utils.NewRandomID()
var newJob *structs.Job
var newLayer *structs.Layer
if needNewJob {
newJob = &structs.Job{
ID: utils.NewRandomID(),
Status: structs.PENDING,
ETag: etag,
}
newLayer = &structs.Layer{
ID: utils.NewRandomID(),
JobID: newJob.ID,
Status: structs.PENDING,
ETag: etag,
}
layersByID[newLayer.ID] = newLayer
layersByID[""] = newLayer
}
newTasks := []*structs.Task{}
addTasks := []*structs.Task{}
for _, t := range in {
parentLayer, ok := layersByID[t.LayerID]
if !ok || parentLayer == nil {
return nil, fmt.Errorf("%w parent layer %s not found for task", errors.ErrParentNotFound, t.LayerID)
}
if !layerCanHaveMoreTasks(parentLayer) {
return nil, fmt.Errorf("%w parent layer %s cannot have more tasks", errors.ErrInvalidState, t.LayerID)
}
tsk := &structs.Task{
TaskSpec: t.TaskSpec,
ID: utils.NewRandomID(),
JobID: parentLayer.JobID,
LayerID: parentLayer.ID,
Status: structs.PENDING,
ETag: etag,
}
if t.LayerID == "" {
newTasks = append(newTasks, tsk)
} else {
addTasks = append(addTasks, tsk)
}
}
// insert into db
if needNewJob {
err = c.db.InsertJob(newJob, []*structs.Layer{newLayer}, newTasks)
if err != nil {
return nil, err
}
}
return append(newTasks, addTasks...), c.db.InsertTasks(addTasks)
}
// CreateJob creates a new job with the given layers and tasks.
// Tasks may be enqueued into Layers
func (c *Service) CreateJob(cjr *structs.CreateJobRequest) (*structs.CreateJobResponse, error) {
// validate input
err := validateCreateJobRequest(cjr)
if err != nil {
return nil, err
}
// build structs
job, layers, tasks, tasks_by_layer := buildJob(cjr)
// insert into db
err = c.db.InsertJob(job, layers, tasks)
if err != nil {
return nil, err
}
// build response
resp_layers := []*structs.JobLayerResponse{}
for _, l := range layers {
resp_layers = append(resp_layers, &structs.JobLayerResponse{
Layer: l,
Tasks: tasks_by_layer[l.ID],
})
}
return &structs.CreateJobResponse{
Job: job,
Layers: resp_layers,
}, nil
}
func (c *Service) enqueueTasks(ts []*structs.Task) error {
etag := utils.NewRandomID()
for _, t := range ts {
if t.PausedAt > 0 {
continue
}
queueTaskID, err := c.qu.Enqueue(t)
if err != nil {
_, derr := c.db.SetTasksStatus(structs.ERRORED, etag, []*structs.ObjectRef{{ID: t.ID, ETag: t.ETag}}, err.Error())
if derr != nil {
c.errs <- fmt.Errorf("failed to set task %s status to ERRORED, database error %v, original queue error %v\n", t.ID, derr, err)
}
continue
}
err = c.db.SetTaskQueueID(t.ID, t.ETag, etag, queueTaskID, structs.QUEUED)
if err != nil {
c.qu.Kill(queueTaskID)
_, derr := c.db.SetTasksStatus(structs.ERRORED, etag, []*structs.ObjectRef{{ID: t.ID, ETag: t.ETag}}, err.Error())
if derr != nil {
c.errs <- fmt.Errorf("failed to set task %s queue id, database error %v, original queue error %v\n", t.ID, derr, err)
}
} else {
t.QueueTaskID = queueTaskID
t.Status = structs.QUEUED
t.ETag = etag
}
}
return nil
}
func (c *Service) handleEvents(errchan chan<- error, evtWork chan *changes.Change) {
for evt := range evtWork {
var err error
switch evt.Kind {
case structs.KindLayer:
err = c.handleLayerEvent(evt)
case structs.KindTask:
err = c.handleTaskEvent(evt)
default:
err = fmt.Errorf("%w %s unknown kind", errors.ErrNotSupported, evt.Kind)
}
if err != nil {
errchan <- err
}
}
}
func (c *Service) handleLayerEvent(evt *changes.Change) error {
if evt.New == nil { // deleted
return nil
}
eNew := evt.New.(*structs.Layer)
if structs.IsFinalStatus(eNew.Status) {
if eNew.Status == structs.SKIPPED {
c.errs <- c.skipLayerTasks(eNew.ID)
}
jobStatus, layers, err := c.determineJobStatus(eNew.JobID)
if err != nil {
return err
}
if structs.IsFinalStatus(jobStatus) {
jobs, err := c.db.Jobs(&structs.Query{JobIDs: []string{eNew.JobID}, Limit: 1})
if err != nil {
return err
}
if len(jobs) != 1 {
return fmt.Errorf("expected 1 job with id %s, found %d", eNew.JobID, len(jobs))
}
_, err = c.db.SetJobsStatus(
jobStatus,
utils.NewRandomID(),
[]*structs.ObjectRef{{ID: jobs[0].ID, ETag: jobs[0].ETag}},
)
return err
}
if len(layers) == 0 {
return nil
}
ids := []*structs.ObjectRef{}
for _, l := range layers {
ids = append(ids, &structs.ObjectRef{ID: l.ID, ETag: l.ETag})
}
_, err = c.db.SetLayersStatus(
structs.RUNNING,
utils.NewRandomID(),
ids,
)
return err
} else if eNew.PausedAt > 0 { // we're paused (even if we weren't just set paused)
return nil
} else if eNew.Status == structs.RUNNING {
// layer should be running; enqueue all tasks that need queuing
enqueued, err := c.enqueueLayerTasks(eNew.ID)
if err != nil {
return err
}
if enqueued > 0 {
return nil // running must be the correct status
}
desired, err := c.determineLayerStatus(eNew.ID)
if err != nil {
return err
}
if desired != eNew.Status {
_, err = c.db.SetLayersStatus(desired, utils.NewRandomID(), []*structs.ObjectRef{{ID: eNew.ID, ETag: eNew.ETag}})
return err
}
}
return nil
}
func (c *Service) enqueueLayerTasks(layerID string) (int, error) {
q := &structs.Query{
Limit: defLimit,
Offset: 0,
LayerIDs: []string{layerID},
Statuses: []structs.Status{structs.PENDING},
}
total := 0
for {
tasks, err := c.db.Tasks(q)
if err != nil {
return total, err
}
total += len(tasks)
err = c.enqueueTasks(tasks)
if err != nil {
return total, err
}
if len(tasks) < q.Limit {
return total, nil
}
q.Offset += q.Limit
}
}
func (c *Service) determineLayerStatus(layerID string) (structs.Status, error) {
q := &structs.Query{
Limit: defLimit,
Offset: 0,
LayerIDs: []string{layerID},
Statuses: append(incompleteStates, structs.ERRORED, structs.KILLED),
}
// metrics
finalStates := 0
nonFinalStates := 0
states := map[structs.Status]int64{
structs.PENDING: 0,
structs.QUEUED: 0,
structs.RUNNING: 0,
structs.KILLED: 0,
structs.ERRORED: 0,
}
// gather task metrics
for {
tasks, err := c.db.Tasks(q)
if err != nil {
return structs.PENDING, err
}
for _, t := range tasks {
if t.Status == structs.RUNNING || t.Status == structs.QUEUED {
// if any task is running, the layer is running
return structs.RUNNING, nil
}
states[t.Status]++
if structs.IsFinalStatus(t.Status) {
finalStates++
} else {
nonFinalStates++
}
}
if len(tasks) < q.Limit {
break
}
q.Offset += q.Limit
}
if nonFinalStates > 0 {
// since it can't be RUNNING or QUEUED it must be PENDING (only TASKs are KILLED)
return structs.PENDING, nil
}
if states[structs.ERRORED] > 0 || states[structs.KILLED] > 0 {
// if a task ERRORED & hasn't been skipped, the layer is ERRORED
return structs.ERRORED, nil
}
// This ignores 'SKIPPED' tasks, for the purpose of that layer status they don't exist.
return structs.COMPLETED, nil
}
func (c *Service) determineJobStatus(jobID string) (structs.Status, []*structs.Layer, error) {
q := &structs.Query{
Limit: defLimit,
JobIDs: []string{jobID},
Statuses: append(incompleteStates, structs.ERRORED, structs.KILLED),
}
all_layers := []*structs.Layer{}
for {
layers, err := c.db.Layers(q)
if err != nil {
return structs.RUNNING, nil, err
}
all_layers = append(all_layers, layers...)
if len(layers) < q.Limit {
break
}
q.Offset += q.Limit
}
jobState, canRun := determineJobStatus(all_layers)
return jobState, canRun, nil
}
func (c *Service) killTask(task *structs.Task) error {
if task.QueueTaskID == "" {
return nil
}
err := c.qu.Kill(task.QueueTaskID)
if err != nil {
return err
}
etag := utils.NewRandomID()
err = c.db.SetTaskQueueID(task.ID, task.ETag, etag, "", structs.ERRORED)
task.ETag = etag
task.QueueTaskID = ""
task.Status = structs.ERRORED
return err
}
func (c *Service) handleTaskEvent(evt *changes.Change) error {
// deleted or created
if evt.New == nil || evt.Old == nil {
return nil
}
// updated
eNew := evt.New.(*structs.Task)
parent, err := c.db.Layers(&structs.Query{Limit: 1, LayerIDs: []string{eNew.LayerID}})
if err != nil {
return err
}
if len(parent) != 1 {
return fmt.Errorf("expected 1 parent layer, got %d", len(parent))
}
if structs.IsFinalStatus(eNew.Status) { // task is finished
if eNew.Status == structs.KILLED {
err = c.killTask(eNew)
if err != nil {
return err
}
}
// Check: If I have to update the parent layer status?
// ie. COMPLETED | SKIPPED | ERRORED | KILLED
if parent[0].Status == structs.SKIPPED {
return nil
}
desired, err := c.determineLayerStatus(eNew.LayerID)
if err != nil {
return err
}
if parent[0].Status == desired {
return nil // no update is needed
}
_, err = c.db.SetLayersStatus(desired, utils.NewRandomID(), []*structs.ObjectRef{{ID: parent[0].ID, ETag: parent[0].ETag}})
return err
} else if eNew.PausedAt > 0 || parent[0].PausedAt > 0 {
return nil
} else if parent[0].Status == structs.SKIPPED { // implies we're not skipped since we're not in a final state
if eNew.QueueTaskID != "" {
c.killTask(eNew)
}
_, err := c.db.SetTasksStatus(structs.SKIPPED, utils.NewRandomID(), []*structs.ObjectRef{{ID: eNew.ID, ETag: eNew.ETag}})
return err
} else if parent[0].Status == structs.RUNNING && eNew.QueueTaskID == "" { // we should be queued but aren't
return c.enqueueTasks([]*structs.Task{eNew})
}
return nil
}
func (c *Service) skipLayerTasks(layerID string) error {
q := &structs.Query{
Limit: defLimit,
Offset: 0,
LayerIDs: []string{layerID},
Statuses: incompleteStates,
}
for {
tasks, err := c.db.Tasks(q)
if err != nil {
return err
}
toSkip := []*structs.ObjectRef{}
for _, t := range tasks {
toSkip = append(toSkip, &structs.ObjectRef{ID: t.ID, ETag: t.ETag})
}
if len(toSkip) > 0 {
_, err = c.db.SetTasksStatus(structs.SKIPPED, utils.NewRandomID(), toSkip)
if err != nil {
return err
}
}
if len(tasks) < q.Limit {
return nil
}
q.Offset += q.Limit
}
}
func (c *Service) queueTidyLayerWork(errchan chan<- error, work chan<- *structs.Layer) {
q := &structs.Query{
Limit: defLimit,
Offset: 0,
Statuses: incompleteStates,
UpdatedBefore: timeNow() - int64(c.opts.TidyUpdateThreshold.Seconds()),
}
for {
layers, err := c.db.Layers(q)
if err != nil {
errchan <- err
break
}
for _, l := range layers {
work <- l
}
if len(layers) < q.Limit {
return
}
q.Offset += q.Limit
}
}
func (c *Service) queueTidyTaskWork(errchan chan<- error, work chan<- *structs.Task) {
q := &structs.Query{
Limit: defLimit,
Offset: 0,
Statuses: incompleteStates,
UpdatedBefore: timeNow() - int64(c.opts.TidyUpdateThreshold.Seconds()),
}
for {
tasks, err := c.db.Tasks(q)
if err != nil {
errchan <- err
break
}
for _, t := range tasks {
work <- t
}
if len(tasks) < q.Limit {
return
}
q.Offset += q.Limit
}
}