forked from mongodb/amboy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.go
More file actions
79 lines (62 loc) · 1.67 KB
/
meta.go
File metadata and controls
79 lines (62 loc) · 1.67 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
package amboy
import (
"context"
"github.com/mongodb/grip"
)
// ResolveErrors takes a queue object and iterates over the results
// and returns a single aggregated error for the queue's job. The
// completeness of this operation depends on the implementation of a
// the queue implementation's Results() method.
func ResolveErrors(ctx context.Context, q Queue) error {
catcher := grip.NewCatcher()
for result := range q.Results(ctx) {
if err := ctx.Err(); err != nil {
catcher.Add(err)
break
}
catcher.Add(result.Error())
}
return catcher.Resolve()
}
// PopulateQueue adds jobs from a channel to a queue and returns an
// error with the aggregated results of these operations.
func PopulateQueue(ctx context.Context, q Queue, jobs <-chan Job) error {
catcher := grip.NewCatcher()
for j := range jobs {
if err := ctx.Err(); err != nil {
catcher.Add(err)
break
}
catcher.Add(q.Put(j))
}
return catcher.Resolve()
}
// QueueReport holds the ids of all tasks in a queue by state.
type QueueReport struct {
Completed []string `json:"completed"`
InProgress []string `json:"in_progress"`
Pending []string `json:"pending"`
}
// Report returns a QueueReport status for the state of a queue.
func Report(ctx context.Context, q Queue, limit int) QueueReport {
var out QueueReport
if limit == 0 {
return out
}
var count int
for stat := range q.JobStats(ctx) {
switch {
case stat.Completed:
out.Completed = append(out.Completed, stat.ID)
case stat.InProgress:
out.InProgress = append(out.InProgress, stat.ID)
default:
out.Pending = append(out.Pending, stat.ID)
}
count++
if limit > 0 && count >= limit {
break
}
}
return out
}