This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
243 lines (198 loc) · 4.51 KB
/
app.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package worq
import (
"errors"
"fmt"
"sync"
"golang.org/x/sync/errgroup"
"github.com/gofrs/uuid"
"github.com/sirupsen/logrus"
)
type TaskRejected struct {
Requeue bool
}
func (t TaskRejected) Error() string {
return fmt.Sprintf("worq: task rejected; requeue: %v", t.Requeue)
}
type TaskNotFound struct {
Name string
}
func (t TaskNotFound) Error() string {
return "worq: cannot find task " + t.Name
}
// OptionFunc is a function that configures the App.
type OptionFunc func(*App) error
type App struct {
logger logrus.FieldLogger
broker Broker
protocol Protocol
binder Binder
concurrency int
defaultQueue string
idFunc func() string
taskMap sync.Map // map[string]TaskFunc
}
func New(options ...OptionFunc) (*App, error) {
// Default logger
logger := logrus.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
app := new(App)
app.logger = logger
app.concurrency = 4
app.defaultQueue = "worq"
app.idFunc = func() string {
return uuid.Must(uuid.NewV4()).String()
}
// Apply option functions
for _, option := range options {
if err := option(app); err != nil {
return nil, err
}
}
return app, nil
}
func (app *App) Context() Context {
return &context{
app: app,
}
}
func (app *App) Protocol() Protocol {
return app.protocol
}
func (app *App) Register(name string, f TaskFunc) error {
if name == "" {
return errors.New("worq.Register: task name is empty")
}
if f == nil {
return errors.New("worq.Register: task function is nil")
}
if _, dup := app.taskMap.LoadOrStore(name, f); dup {
return errors.New("worq.Register: task already defined: " + name)
}
return nil
}
func (app *App) Start() error {
app.logger.Info("Watch this space")
g, ctx := errgroup.WithContext(app.Context())
for i := 0; i < 10; i++ {
g.Go(func() error {
consumer, err := app.broker.Consume(app.Context(), app.defaultQueue)
if err != nil {
return err
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if !consumer.Next() {
return consumer.Err()
}
if err := app.consumerOnNext(consumer); err != nil {
app.logger.Errorf("error consuming message: %v", err)
}
}
}
})
}
return g.Wait()
}
func (app *App) consumerOnNext(consumer Consumer) error {
// TODO: Convert this into a consumer struct
if msg, err := consumer.Message(); err == nil {
// TODO: message specific context?
ctx := &context{
app: app,
logger: app.logger.WithFields(logrus.Fields{
"id": msg.ID(),
"task": msg.Task(),
}),
consumer: consumer,
msg: msg,
}
ctx.Logger().Info("Task received")
switch err := app.processMessage(ctx).(type) {
case nil:
return consumer.Ack(msg)
case *TaskNotFound:
ctx.logger.Error(err)
return consumer.Nack(msg, false)
case *TaskRejected:
ctx.logger.Warn(err)
return consumer.Nack(msg, err.Requeue)
default:
ctx.logger.Error(err)
return consumer.Nack(msg, true) // or requeue = false?
}
} else {
return consumer.Nack(msg, false)
}
}
func (app *App) processMessage(ctx Context) error {
f, ok := app.taskMap.Load(ctx.Message().Task())
if !ok {
return &TaskNotFound{ctx.Message().Task()}
}
return f.(TaskFunc)(ctx)
}
func (app *App) Enqueue(sig *Signature) (*AsyncResult, error) {
var err error
queue := app.queueForSignature(sig)
id := app.idFunc()
publishing, err := app.binder.Unbind(app.Context(), id, queue, sig)
if err != nil {
return nil, err
}
err = app.broker.Enqueue(publishing)
if err != nil {
return nil, err
}
result := new(AsyncResult)
result.ID = id
return result, nil
}
func (app *App) queueForSignature(sig *Signature) string {
// TODO: proper routing
return app.defaultQueue
}
func (app *App) Binder() Binder {
return app.binder
}
// SetLogger sets the logger that the app will use.
func SetLogger(logger logrus.FieldLogger) OptionFunc {
return func(app *App) error {
app.logger = logger
return nil
}
}
func SetBroker(broker Broker) OptionFunc {
return func(app *App) error {
app.broker = broker
return nil
}
}
func SetProtocol(protocol Protocol) OptionFunc {
return func(app *App) error {
app.protocol = protocol
return nil
}
}
func SetBinder(binder Binder) OptionFunc {
return func(app *App) error {
app.binder = binder
return nil
}
}
func SetConcurrency(concurrency int) OptionFunc {
return func(app *App) error {
app.concurrency = concurrency
return nil
}
}
func SetDefaultQueue(queue string) OptionFunc {
return func(app *App) error {
app.defaultQueue = queue
return nil
}
}