forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ocr_rpc_worker.go
323 lines (283 loc) · 8.77 KB
/
ocr_rpc_worker.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package ocrworker
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"time"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/segmentio/ksuid"
)
type OcrRpcWorker struct {
workerConfig WorkerConfig
conn *amqp.Connection
channel *amqp.Channel
tag string
Done chan error
}
// tag is based on K-Sortable Globally Unique IDs
var tag = ksuid.New().String()
// NewOcrRpcWorker is needed to establish a connection to a message broker
func NewOcrRpcWorker(wc *WorkerConfig) (*OcrRpcWorker, error) {
ocrRpcWorker := &OcrRpcWorker{
workerConfig: *wc,
conn: nil,
channel: nil,
tag: tag,
Done: make(chan error),
}
return ocrRpcWorker, nil
}
func (w *OcrRpcWorker) Run() error {
var err error
queueArgs := make(amqp.Table)
queueArgs["x-max-priority"] = uint8(9)
log.Debug().
Str("component", "OCR_WORKER").
Str("tag", tag).
Msg("Run() called...")
urlToLog, _ := url.Parse(w.workerConfig.AmqpURI)
log.Info().
Str("component", "OCR_WORKER").
Str("tag", tag).
Str("amqp", urlToLog.Scheme+"://"+urlToLog.Host+urlToLog.Path).
Msg("dialing rabbitMQ")
w.conn, err = amqp.Dial(w.workerConfig.AmqpURI)
if err != nil {
log.Warn().
Str("component", "OCR_WORKER").
Err(err).
Str("tag", tag).
Msg("error connecting to rabbitMQ")
return err
}
go func() {
fmt.Printf("closing: %s", <-w.conn.NotifyClose(make(chan *amqp.Error)))
}()
log.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Msg("got Connection, getting channel")
w.channel, err = w.conn.Channel()
if err != nil {
return err
}
// setting the prefetchCount to 1 reduces the Memory Consumption by the worker
err = w.channel.Qos(int(w.workerConfig.NumParallelJobs), 0, true)
if err != nil {
return err
}
if err := w.channel.ExchangeDeclare(
w.workerConfig.Exchange, // name of the exchange
w.workerConfig.ExchangeType, // type
true, // durable
false, // delete when complete
false, // internal
false, // noWait
nil, // arguments
); err != nil {
return err
}
// just use the routing key as the queue name, since there's no reason
// to have a different name
queueName := w.workerConfig.RoutingKey
queue, err := w.channel.QueueDeclare(
queueName, // name of the queue
true, // durable
false, // delete when unused
false, // exclusive
false, // noWait
queueArgs, // arguments
)
if err != nil {
return err
}
log.Info().Str("component", "OCR_WORKER").Str("RoutingKey", w.workerConfig.RoutingKey).
Str("tag", tag).
Msg("binding to routing key")
if err := w.channel.QueueBind(
queue.Name, // name of the queue
w.workerConfig.RoutingKey, // bindingKey
w.workerConfig.Exchange, // sourceExchange
false, // noWait
queueArgs, // arguments
); err != nil {
return err
}
log.Info().Str("component", "OCR_WORKER").Str("tag", tag).
Msg("Queue bound to Exchange, starting Consume tag")
deliveries, err := w.channel.Consume(
queue.Name, // name
tag, // consumerTag,
false, // noAck
false, // exclusive
false, // noLocal
false, // noWait
queueArgs, // arguments
)
if err != nil {
return err
}
go w.handle(deliveries, w.Done)
return nil
}
func (w *OcrRpcWorker) Shutdown() error {
// will close() the deliveries channel
if err := w.channel.Cancel(w.tag, true); err != nil {
return fmt.Errorf("worker with tag %s cancel failed: %s", tag, err)
}
if err := w.conn.Close(); err != nil {
return fmt.Errorf("AMQP connection with worker %s close error: %s", tag, err)
}
defer log.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Msg("Shutdown OK")
// wait for handle() to exit
return <-w.Done
}
func (w *OcrRpcWorker) handle(deliveries <-chan amqp.Delivery, done chan error) {
for d := range deliveries {
log.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Int("msg_size", len(d.Body)).
Uint8("DeliveryMode", d.DeliveryMode).
Uint8("Priority", d.Priority).
Str("RequestID", d.CorrelationId).
Str("ReplyToQueue", d.ReplyTo).
Str("ConsumerTag", d.ConsumerTag).
Uint64("DeliveryTag", d.DeliveryTag).
Str("Exchange", d.Exchange).
Str("RoutingKey", d.RoutingKey).
Msg("worker got delivery, starting processing")
// reply from engine here
// id is not set, Text is set, Status is set
ocrResult, err := w.resultForDelivery(&d)
if err != nil {
log.Error().Err(err).Str("component", "OCR_WORKER").
Str("RequestID", d.CorrelationId).
Str("tag", tag).
Msg("Error generating ocr result")
}
err = w.sendRpcResponse(ocrResult, d.ReplyTo, d.CorrelationId)
if err != nil {
log.Error().Err(err).Str("component", "OCR_WORKER").
Str("RequestID", ocrResult.ID).
Str("tag", tag).
Msg("Error generating ocr result, sendRpcResponse failed")
// if we can't send our response, let's just abort
done <- err
break
}
err = d.Ack(false)
if err != nil {
log.Warn().Str("component", "OCR_WORKER").Err(err).
Str("tag", tag).
Msg("Ack() was not successful")
}
}
log.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Msg("handle: deliveries channel closed")
done <- fmt.Errorf("handle: deliveries channel closed")
}
func (w *OcrRpcWorker) resultForDelivery(d *amqp.Delivery) (OcrResult, error) {
ocrRequest := OcrRequest{}
ocrResult := OcrResult{ID: d.CorrelationId}
err := json.Unmarshal(d.Body, &ocrRequest)
if err != nil {
msg := "Error unmarshalling json: %v. Error: %v"
errMsg := fmt.Sprintf(msg, d.CorrelationId, err)
log.Error().Err(err).Caller().
Str("RequestID", ocrResult.ID).
Str("tag", tag).
Msg("error unmarshalling json delivery")
ocrResult.Text = errMsg
ocrResult.Status = "error"
return ocrResult, err
}
ocrEngine := NewOcrEngine(ocrRequest.EngineType)
ocrResult, err = ocrEngine.ProcessRequest(&ocrRequest, &w.workerConfig)
if err != nil {
msg := "Error processing image url: %v. Error: %v"
errMsg := fmt.Sprintf(msg, ocrRequest.RequestID, err)
log.Error().Err(err).
Str("RequestID", ocrRequest.RequestID).
Str("tag", tag).
Str("ImgUrl", ocrRequest.ImgUrl).
Msg("Error processing image")
ocrResult.Text = errMsg
ocrResult.Status = "error"
return ocrResult, err
}
return ocrResult, nil
}
func (w *OcrRpcWorker) sendRpcResponse(r OcrResult, replyTo, correlationId string) error {
// RequestID is the same as correlationId
logger := zerolog.New(os.Stdout).With().
Str("RequestID", correlationId).Timestamp().Logger()
if w.workerConfig.Reliable {
// Do not use w.workerConfig.Reliable=true due to major issues
// that will completely wedge the rpc worker. Setting the
// buffered channels length higher would delay the problem,
// but then it would still happen later.
if err := w.channel.Confirm(false); err != nil {
return err
}
ack, nack := w.channel.NotifyConfirm(make(chan uint64, 100), make(chan uint64, 100))
defer confirmDeliveryWorker(ack, nack)
}
logger.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Str("replyTo", replyTo).Msg("sendRpcResponse to")
// ocr worker is publishing back the decoded text
body, err := json.Marshal(r)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := w.channel.PublishWithContext(
ctx,
w.workerConfig.Exchange, // publish to an exchange
replyTo, // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "text/plain",
ContentEncoding: "",
Body: body,
// Body: []byte(r.Text),
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
Priority: 0, // 0-9
CorrelationId: correlationId,
// a bunch of application/implementation-specific fields
},
); err != nil {
return err
}
logger.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Str("replyTo", replyTo).
Msg("sendRpcResponse succeeded")
return nil
}
func confirmDeliveryWorker(ack, nack chan uint64) {
log.Info().Str("component", "OCR_WORKER").
Str("tag", tag).
Msg("awaiting delivery confirmation...")
select {
case tag := <-ack:
log.Info().Str("component", "OCR_WORKER").Uint64("tag", tag).
Msg("confirmed delivery")
case tag := <-nack:
log.Info().Str("component", "OCR_WORKER").Uint64("tag", tag).
Msg("failed to confirm delivery")
case <-time.After(rpcResponseTimeout):
// this is bad, the worker will probably be dysfunctional
// at this point, so panic
log.Panic().Str("component", "OCR_WORKER").Msg("timeout trying to confirm delivery. Worker panic")
}
}