This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
forked from contribsys/faktory_worker_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
148 lines (128 loc) · 3.53 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
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
// Faktory Enterprise:
// the BID of the Batch associated with this job
Bid() 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
}
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) JobType() string {
return h.job.Type
}
// 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(pool *faktory.Pool, job *faktory.Job) context.Context {
ctx := context.Background()
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)
}