-
Notifications
You must be signed in to change notification settings - Fork 230
/
batch.go
205 lines (174 loc) · 4.49 KB
/
batch.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
package client
import (
"encoding/json"
"fmt"
"github.com/contribsys/faktory/util"
)
type BatchStatus struct {
Bid string `json:"bid"`
ParentBid string `json:"parent_bid,omitempty"`
Description string `json:"description,omitempty"`
CreatedAt string `json:"created_at"`
Total int64 `json:"total"`
Pending int64 `json:"pending"`
Failed int64 `json:"failed"`
// "" if pending,
// "1" if callback enqueued,
// "2" if callback finished successfully
CompleteState string `json:"complete_st"`
SuccessState string `json:"success_st"`
}
type Batch struct {
// Unique identifier for each batch.
// NB: the caller should not set this, it is generated
// by Faktory when the batch is persisted to Redis.
Bid string `json:"bid"`
ParentBid string `json:"parent_bid,omitempty"`
Description string `json:"description,omitempty"`
Success *Job `json:"success,omitempty"`
Complete *Job `json:"complete,omitempty"`
faktory *Client
committed bool
new bool
}
// Allocate a new Batch.
// Caller must set one or more callbacks and
// push one or more jobs in the batch.
//
// b := faktory.NewBatch(cl)
// b.Success = faktory.NewJob("MySuccessCallback", 12345)
// b.Jobs(func() error {
// b.Push(...)
// })
func NewBatch(cl *Client) *Batch {
return &Batch{
committed: false,
new: true,
faktory: cl,
}
}
// Push one or more jobs within this function.
// Job processing will start **immediately**
// but callbacks will not fire until Commit()
// is called, allowing you to push jobs in slowly
// and avoid the obvious race condition.
func (b *Batch) Jobs(fn func() error) error {
if b.new {
if _, err := b.faktory.BatchNew(b); err != nil {
return fmt.Errorf("cannot create new batch: %w", err)
}
}
if b.faktory == nil || b.committed {
return ErrBatchNotOpen
}
if err := fn(); err != nil {
return fmt.Errorf("cannot push jobs in the %q batch: %w", b.Bid, err)
}
return b.Commit()
}
func (b *Batch) Push(job *Job) error {
if b.new {
return ErrBatchNotOpen
}
if b.faktory == nil || b.committed {
return ErrBatchAlreadyCommitted
}
job.SetCustom("bid", b.Bid)
return b.faktory.Push(job)
}
// Result is map[JID]ErrorMessage
func (b *Batch) PushBulk(jobs []*Job) (map[string]string, error) {
if b.new {
return nil, ErrBatchNotOpen
}
if b.faktory == nil || b.committed {
return nil, ErrBatchAlreadyCommitted
}
for _, job := range jobs {
job.SetCustom("bid", b.Bid)
}
return b.faktory.PushBulk(jobs)
}
// Commit any pushed jobs in the batch to Redis so they can fire callbacks.
// A Batch object can only be committed once.
// You must use client.BatchOpen to get a new copy if you want to commit more jobs.
func (b *Batch) Commit() error {
if b.new {
return ErrBatchNotOpen
}
if b.faktory == nil || b.committed {
return ErrBatchAlreadyCommitted
}
if err := b.faktory.BatchCommit(b.Bid); err != nil {
return fmt.Errorf("cannot commit %q batch: %w", b.Bid, err)
}
b.faktory = nil
b.committed = true
return nil
}
var (
ErrBatchAlreadyCommitted = fmt.Errorf("batch has already been committed, must reopen")
ErrBatchNotOpen = fmt.Errorf("batch must be opened before it can be used")
)
/////////////////////////////////////////////
// Low-level command API
func (c *Client) BatchCommit(bid string) error {
err := c.writeLine(c.wtr, "BATCH COMMIT", []byte(bid))
if err != nil {
return err
}
return c.ok(c.rdr)
}
func (c *Client) BatchNew(def *Batch) (*Batch, error) {
if def.Bid != "" {
return nil, fmt.Errorf("BID must be blank when creating a new Batch, cannot specify it")
}
bbytes, err := json.Marshal(def)
if err != nil {
return nil, err
}
err = c.writeLine(c.wtr, "BATCH NEW", bbytes)
if err != nil {
return nil, err
}
bid, err := c.readString(c.rdr)
if err != nil {
return nil, err
}
def.Bid = bid
def.new = false
def.faktory = c
return def, nil
}
func (c *Client) BatchStatus(bid string) (*BatchStatus, error) {
err := c.writeLine(c.wtr, "BATCH STATUS", []byte(bid))
if err != nil {
return nil, err
}
data, err := c.readResponse(c.rdr)
if err != nil {
return nil, err
}
var stat BatchStatus
err = util.JsonUnmarshal(data, &stat)
if err != nil {
return nil, err
}
return &stat, nil
}
func (c *Client) BatchOpen(bid string) (*Batch, error) {
err := c.writeLine(c.wtr, "BATCH OPEN", []byte(bid))
if err != nil {
return nil, err
}
bbid, err := c.readString(c.rdr)
if err != nil {
return nil, err
}
b := &Batch{
Bid: bbid,
new: false,
faktory: c,
}
return b, nil
}