-
Notifications
You must be signed in to change notification settings - Fork 43
/
main.go
212 lines (186 loc) · 4.83 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"strings"
"time"
faktory "github.com/contribsys/faktory/client"
worker "github.com/contribsys/faktory_worker_go"
)
func someFunc(ctx context.Context, args ...interface{}) error {
help := worker.HelperFor(ctx)
log.Printf("Working on job %s\n", help.Jid())
// log.Printf("Context %v\n", ctx)
// log.Printf("Args %v\n", args)
time.Sleep(1 * time.Second)
return nil
}
func batchFunc(ctx context.Context, args ...interface{}) error {
help := worker.HelperFor(ctx)
log.Printf("Working on job %s\n", help.Jid())
if help.Bid() != "" {
log.Printf("within %s...\n", help.Bid())
}
// log.Printf("Context %v\n", ctx)
// log.Printf("Args %v\n", args)
return nil
}
func fastFunc(ctx context.Context, args ...interface{}) error {
return nil
}
func longFunc(ctx context.Context, args ...interface{}) error {
help := worker.HelperFor(ctx)
log.Printf("Working on job %s\n", help.Jid())
select {
case <-ctx.Done():
fmt.Printf("Context closed, SUCCESS")
case <-time.After(30 * time.Second):
fmt.Printf("30 sec timeout, FAIL")
}
return nil
}
func main() {
flags := log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC
log.SetFlags(flags)
mgr := worker.NewManager()
mgr.Use(func(ctx context.Context, job *faktory.Job, next func(ctx context.Context) error) error {
log.Printf("Starting work on job %s of type %s with custom %v\n", job.Jid, job.Type, job.Custom)
err := next(ctx)
log.Printf("Finished work on job %s with error %v\n", job.Jid, err)
return err
})
// register job types and the function to execute them
mgr.Register("SomeJob", someFunc)
mgr.Register("SomeWorker", someFunc)
mgr.Register("ImportImageJob", batchFunc)
mgr.Register("ImportImageSuccess", batchFunc)
mgr.Register("Long", longFunc)
mgr.Register("fast", fastFunc)
//mgr.Register("AnotherJob", anotherFunc)
// use up to N goroutines to execute jobs
mgr.Concurrency = 20
// pull jobs from these queues, in this order of precedence
mgr.ProcessStrictPriorityQueues("critical", "default", "bulk")
var quit bool
mgr.On(worker.Shutdown, func(*worker.Manager) error {
quit = true
return nil
})
go func() {
batch()
unique()
for {
if quit {
return
}
produce(mgr)
time.Sleep(10 * time.Second)
}
}()
// Start processing jobs, this method does not return
_ = mgr.Run()
}
func unique() {
pool, err := faktory.NewPool(5)
if err != nil {
panic(err)
}
_ = pool.With(func(cl *faktory.Client) error {
if err != nil {
panic(err)
}
if !isEnt(cl) {
return nil
}
job := faktory.NewJob("Long", 1, 2, 3)
job.SetCustom("unique_for", 0.5)
err := cl.Push(job)
if err != nil {
panic(err)
}
err = cl.Push(job)
if err != nil {
if e, ok := err.(*faktory.ProtocolError); ok {
fmt.Printf("%+v\n", *e)
return e
}
}
panic(fmt.Sprintf("Expected: %+v", err))
})
}
func isEnt(cl *faktory.Client) bool {
hash, err := cl.Info()
if err != nil {
panic(err)
}
desc := hash["server"].(map[string]interface{})["description"].(string)
return strings.Contains(desc, "Enterprise")
}
func batch() {
cl, err := faktory.Open()
if err != nil {
return
}
if !isEnt(cl) {
return
}
// Batch example
// We want to import all images associated with user 1234.
// Once we've imported those two images, we want to fire
// a success callback so we can notify user 1234.
b := faktory.NewBatch(cl)
b.Description = "Import images for user 1234"
b.Success = faktory.NewJob("ImportImageSuccess", "parent", "1234")
// Once we call Jobs(), the batch is off and running
err = b.Jobs(func() error {
err := b.Push(faktory.NewJob("ImportImageJob", "1"))
if err != nil {
return err
}
fmt.Println("Creating jobs")
for i := 1; i <= 10000; i++ {
err = b.Push(faktory.NewJob("fast", []interface{}{}))
if err != nil {
return err
}
}
// a child batch represents a set of jobs which can be monitored
// separately from the parent batch's jobs. parent success won't
// fire until child success runs without error.
child := faktory.NewBatch(cl)
child.ParentBid = b.Bid
child.Description = "Child of " + b.Bid
child.Success = faktory.NewJob("ImportImageSuccess", "child", "1234")
err = child.Jobs(func() error {
return child.Push(faktory.NewJob("ImportImageJob", "2"))
})
if err != nil {
return err
}
return b.Push(faktory.NewJob("ImportImageJob", "3"))
})
if err != nil {
panic(err)
}
st, err := cl.BatchStatus(b.Bid)
if err != nil {
panic(err)
}
fmt.Printf("%+v", st)
}
// Push something for us to work on.
func produce(mgr *worker.Manager) {
j1 := faktory.NewJob("SomeJob", 1, 2, "hello")
j1.Custom = map[string]interface{}{
"hello": "world",
}
j2 := faktory.NewJob("Long", 3, 2, 1)
err := mgr.Pool.With(func(cl *faktory.Client) error {
_, err := cl.PushBulk([]*faktory.Job{j1, j2})
return err
})
if err != nil {
fmt.Printf("produce: %v\n", err)
}
}