forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocessor_rpc_worker.go
295 lines (251 loc) · 8.15 KB
/
preprocessor_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
package ocrworker
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"time"
"github.com/rs/zerolog/log"
"github.com/segmentio/ksuid"
amqp "github.com/rabbitmq/amqp091-go"
)
type PreprocessorRpcWorker struct {
rabbitConfig RabbitConfig
conn *amqp.Connection
channel *amqp.Channel
tag string
Done chan error
bindingKey string
preprocessorMap map[string]Preprocessor
}
var preprocessorTag = ksuid.New().String()
func NewPreprocessorRpcWorker(rc *RabbitConfig, preprocessor string) (*PreprocessorRpcWorker, error) {
preprocessorMap := make(map[string]Preprocessor)
preprocessorMap[PreprocessorStrokeWidthTransform] = StrokeWidthTransformer{}
preprocessorMap[PreprocessorIdentity] = IdentityPreprocessor{}
preprocessorMap[PreprocessorConvertPdf] = ConvertPdf{}
_, ok := preprocessorMap[preprocessor]
if !ok {
return nil, fmt.Errorf("no preprocessor found for: %q", preprocessor)
}
preprocessorRpcWorker := &PreprocessorRpcWorker{
rabbitConfig: *rc,
conn: nil,
channel: nil,
tag: preprocessorTag,
Done: make(chan error),
bindingKey: preprocessor,
preprocessorMap: preprocessorMap,
}
return preprocessorRpcWorker, nil
}
func (w *PreprocessorRpcWorker) Run() error {
var err error
log.Info().Str("component", "PREPROCESSOR_WORKER").Msg("Run() called...")
log.Info().Str("component", "PREPROCESSOR_WORKER").
Str("AmqpURI", w.rabbitConfig.AmqpURI).
Msg("dialing amqpURI...")
w.conn, err = amqp.Dial(w.rabbitConfig.AmqpURI)
if err != nil {
return err
}
go func() {
fmt.Printf("closing: %s", <-w.conn.NotifyClose(make(chan *amqp.Error)))
}()
log.Info().Str("component", "PREPROCESSOR_WORKER").Msg("got Connection, getting Channel")
w.channel, err = w.conn.Channel()
if err != nil {
return err
}
if err := w.channel.ExchangeDeclare(
w.rabbitConfig.Exchange, // name of the exchange
w.rabbitConfig.ExchangeType, // type
true, // durable
false, // delete when complete
false, // internal
false, // noWait
nil, // arguments
); err != nil {
return err
}
// just call the queue the same name as the binding key, since
// there is no reason to have a different name.
queueName := w.bindingKey
queue, err := w.channel.QueueDeclare(
queueName, // name of the queue
true, // durable
false, // delete when usused
false, // exclusive
false, // noWait
nil, // arguments
)
if err != nil {
return err
}
if err := w.channel.QueueBind(
queue.Name, // name of the queue
w.bindingKey, // bindingKey
w.rabbitConfig.Exchange, // sourceExchange
false, // noWait
nil, // arguments
); err != nil {
return err
}
log.Info().Str("component", "PREPROCESSOR_WORKER").
Str("preprocessorTag", preprocessorTag).
Str("bindingKey", w.bindingKey).
Msg("Queue bound to Exchange, starting Consume")
deliveries, err := w.channel.Consume(
queue.Name, // name
preprocessorTag, // consumerTag,
true, // noAck
false, // exclusive
false, // noLocal
false, // noWait
nil, // arguments
)
if err != nil {
return err
}
go w.handle(deliveries, w.Done)
return nil
}
func (w *PreprocessorRpcWorker) Shutdown() error {
// will close() the deliveries channel
if err := w.channel.Cancel(w.tag, true); err != nil {
return fmt.Errorf("worker cancel failed: %s", err)
}
if err := w.conn.Close(); err != nil {
return fmt.Errorf("AMQP connection close error: %s", err)
}
defer log.Info().Str("component", "PREPROCESSOR_WORKER").Msg("Shutdown OK")
// wait for handle() to exit
return <-w.Done
}
func (w *PreprocessorRpcWorker) handle(deliveries <-chan amqp.Delivery, done chan error) {
for d := range deliveries {
log.Info().Str("component", "PREPROCESSOR_WORKER").
Int("size", len(d.Body)).
Uint64("DeliveryTag", d.DeliveryTag).
Str("ReplyTo", d.ReplyTo).
Msg("got delivery")
err := w.handleDelivery(&d)
if err != nil {
log.Error().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg("Error handling delivery in preprocessor.")
}
}
log.Info().Str("component", "PREPROCESSOR_WORKER").Msg("handle: deliveries channel closed")
done <- fmt.Errorf("handle: deliveries channel closed")
}
func (w *PreprocessorRpcWorker) preprocessImage(ocrRequest *OcrRequest) error {
descriptor := w.bindingKey // eg, "stroke-width-transform"
preprocessor := w.preprocessorMap[descriptor]
log.Info().Str("component", "PREPROCESSOR_WORKER").
Str("ocrRequest", ocrRequest.RequestID).Str("descriptor", descriptor).
Msg("Preprocess request via descriptor")
err := preprocessor.preprocess(ocrRequest)
if err != nil {
msg := "Error doing %s on: %v."
errMsg := fmt.Sprintf(msg, descriptor, ocrRequest)
log.Error().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg(errMsg)
return err
}
return nil
}
func (*PreprocessorRpcWorker) strokeWidthTransform(ocrRequest *OcrRequest) error {
// write bytes to a temp file
tmpFileNameInput, err := createTempFileName("")
if err != nil {
return err
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Warn().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg(name + " could not be removed")
}
}(tmpFileNameInput)
tmpFileNameOutput, err := createTempFileName("")
if err != nil {
return err
}
defer func(name string) {
err := os.Remove(name)
if err != nil {
log.Warn().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg(name + " could not be removed")
}
}(tmpFileNameOutput)
err = saveBytesToFileName(ocrRequest.ImgBytes, tmpFileNameInput)
if err != nil {
return err
}
// run DecodeText binary on it (if not in path, print warning and do nothing)
darkOnLightSetting := "1" // todo: this should be passed as a param.
out, err := exec.Command(
"DetectText",
tmpFileNameInput,
tmpFileNameOutput,
darkOnLightSetting,
).CombinedOutput()
if err != nil {
log.Error().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg(string(out))
}
log.Info().Str("component", "PREPROCESSOR_WORKER").Msg("finish DetectText")
// read bytes from output file into ocrRequest.ImgBytes
resultBytes, err := os.ReadFile(tmpFileNameOutput)
if err != nil {
return err
}
ocrRequest.ImgBytes = resultBytes
return nil
}
func (w *PreprocessorRpcWorker) handleDelivery(d *amqp.Delivery) error {
ocrRequest := OcrRequest{}
err := json.Unmarshal(d.Body, &ocrRequest)
if err != nil {
msg := "Error unmarshaling json: %v."
errMsg := fmt.Sprintf(msg, string(d.Body))
log.Error().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg(errMsg)
return err
}
routingKey := ocrRequest.nextPreprocessor(w.rabbitConfig.RoutingKey)
log.Info().Str("component", "PREPROCESSOR_WORKER").Str("routingKey", routingKey).
Msg("publishing with routing key")
err = w.preprocessImage(&ocrRequest)
if err != nil {
msg := "Error preprocessing image: %v."
errMsg := fmt.Sprintf(msg, ocrRequest)
log.Error().Err(err).Str("component", "PREPROCESSOR_WORKER").Msg(errMsg)
return err
}
ocrRequestJson, err := json.Marshal(ocrRequest)
if err != nil {
return err
}
log.Info().Str("component", "PREPROCESSOR_WORKER").Str("routingKey", routingKey).
Msg("sendRpcResponse via routingKey")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := w.channel.PublishWithContext(
ctx,
w.rabbitConfig.Exchange, // publish to an exchange
routingKey, // routing to 0 or more queues
false, // mandatory
false, // immediate
amqp.Publishing{
Headers: amqp.Table{},
ContentType: "text/plain",
ContentEncoding: "",
Body: ocrRequestJson,
DeliveryMode: amqp.Transient, // 1=non-persistent, 2=persistent
Priority: 0, // 0-9
ReplyTo: d.ReplyTo,
CorrelationId: d.CorrelationId,
// a bunch of application/implementation-specific fields
},
); err != nil {
return err
}
log.Info().Str("component", "PREPROCESSOR_WORKER").Msg("handleDelivery succeeded")
return nil
}