-
Notifications
You must be signed in to change notification settings - Fork 42
/
context.go
169 lines (146 loc) · 4.17 KB
/
context.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
package faktory_worker
import (
"context"
"fmt"
"log"
"time"
faktory "github.com/contribsys/faktory/client"
)
// internal keys for context value storage
type valueKey int
const (
poolKey valueKey = 2
jobKey valueKey = 3
)
var (
NoAssociatedBatchError = fmt.Errorf("No batch associated with this job")
)
// The Helper provides access to valuable data and APIs
// within an executing job.
//
// We're pretty strict about what's exposed in the Helper
// because execution should be orthogonal to
// most of the Job payload contents.
//
// func myJob(ctx context.Context, args ...interface{}) error {
// helper := worker.HelperFor(ctx)
// jid := helper.Jid()
//
// helper.With(func(cl *faktory.Client) error {
// cl.Push("anotherJob", 4, "arg")
// })
type Helper interface {
Jid() string
JobType() string
// Custom provides access to the job custom hash.
// Returns the value and `ok=true` if the key was found.
// If not, returns `nil` and `ok=false`.
//
// No type checking is performed, please use with caution.
Custom(key string) (value interface{}, ok bool)
// Faktory Enterprise:
// the BID of the Batch associated with this job
Bid() string
// Faktory Enterprise:
// the BID of the Batch associated with this callback (complete or success) job
CallbackBid() string
// Faktory Enterprise:
// open the batch associated with this job so we can add more jobs to it.
//
// func myJob(ctx context.Context, args ...interface{}) error {
// helper := worker.HelperFor(ctx)
// helper.Batch(func(b *faktory.Batch) error {
// return b.Push(faktory.NewJob("sometype", 1, 2, 3))
// })
Batch(func(*faktory.Batch) error) error
// allows direct access to the Faktory server from the job
With(func(*faktory.Client) error) error
// Faktory Enterprise:
// this method integrates with Faktory Enterprise's Job Tracking feature.
// `reserveUntil` is optional, only needed for long jobs which have more dynamic
// lifetimes.
//
// helper.TrackProgress(10, "Updating code...", nil)
// helper.TrackProgress(20, "Cleaning caches...", &time.Now().Add(1 * time.Hour)))
//
TrackProgress(percent int, desc string, reserveUntil *time.Time) error
}
type jobHelper struct {
job *faktory.Job
pool *faktory.Pool
}
// ensure type compatibility
var _ Helper = &jobHelper{}
func (h *jobHelper) Jid() string {
return h.job.Jid
}
func (h *jobHelper) Bid() string {
if b, ok := h.job.GetCustom("bid"); ok {
return b.(string)
}
return ""
}
func (h *jobHelper) CallbackBid() string {
if b, ok := h.job.GetCustom("_bid"); ok {
return b.(string)
}
return ""
}
func (h *jobHelper) JobType() string {
return h.job.Type
}
func (h *jobHelper) Custom(key string) (value interface{}, ok bool) {
return h.job.GetCustom(key)
}
// Caution: this method must only be called within the
// context of an executing job. It will panic if it cannot
// create a Helper due to missing context values.
func HelperFor(ctx context.Context) Helper {
if j := ctx.Value(jobKey); j != nil {
job := j.(*faktory.Job)
if p := ctx.Value(poolKey); p != nil {
pool := p.(*faktory.Pool)
return &jobHelper{
job: job,
pool: pool,
}
}
}
log.Panic("Invalid job context, cannot create faktory_worker_go job helper")
return nil
}
func jobContext(ctx context.Context, pool *faktory.Pool, job *faktory.Job) context.Context {
ctx = context.WithValue(ctx, poolKey, pool)
ctx = context.WithValue(ctx, jobKey, job)
return ctx
}
// requires Faktory Enterprise
func (h *jobHelper) TrackProgress(percent int, desc string, reserveUntil *time.Time) error {
return h.With(func(cl *faktory.Client) error {
return cl.TrackSet(h.Jid(), percent, desc, reserveUntil)
})
}
// requires Faktory Enterprise
// Open the current batch so we can add more jobs to it.
func (h *jobHelper) Batch(fn func(*faktory.Batch) error) error {
bid := h.Bid()
if bid == "" {
return NoAssociatedBatchError
}
var b *faktory.Batch
var err error
err = h.pool.With(func(cl *faktory.Client) error {
b, err = cl.BatchOpen(bid)
if err != nil {
return err
}
return fn(b)
})
if err != nil {
return err
}
return nil
}
func (h *jobHelper) With(fn func(*faktory.Client) error) error {
return h.pool.With(fn)
}