forked from tleyden/open-ocr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker_config.go
128 lines (117 loc) · 3.43 KB
/
worker_config.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
package ocrworker
import (
"flag"
"fmt"
)
// WorkerConfig will be passed to ocr engines and is used to establish connection to a message broker
type WorkerConfig struct {
AmqpURI string
Exchange string
ExchangeType string
RoutingKey string
Reliable bool
AmqpAPIURI string
APIPathQueue string
APIQueueName string
APIPathStats string
SaveFiles bool
Debug bool
Tiff2pdfConverter string
NumParallelJobs uint
FlgVersion bool
}
// DefaultWorkerConfig will set the default set of worker parameters which are needed for testing and connecting to a broker
func DefaultWorkerConfig() WorkerConfig {
// Reliable: false due to major issues that would completely
// wedge the rpc worker. Setting the buffered channels length
// higher would delay the problem, but then it would still happen later.
workerConfig := WorkerConfig{
AmqpURI: "amqp://guest:guest@localhost:5672/",
Exchange: "open-ocr-exchange",
ExchangeType: "direct",
RoutingKey: "decode-ocr",
Reliable: false, // setting to false because of observed issues
AmqpAPIURI: "http://guest:guest@localhost:15672",
APIPathQueue: "/api/queues/%2f/",
APIQueueName: "decode-ocr",
APIPathStats: "/api/nodes",
SaveFiles: false,
Debug: false,
Tiff2pdfConverter: "convert",
NumParallelJobs: 1,
FlgVersion: false,
}
return workerConfig
}
// FlagFunctionWorker will be used as argument type for DefaultConfigFlagsWorkerOverride
type FlagFunctionWorker func()
// NoOpFlagFunctionWorker will return an empty set of cli parameters.
// In this case default parameter will be used
func NoOpFlagFunctionWorker() FlagFunctionWorker {
return func() {}
}
func DefaultConfigFlagsWorkerOverride(flagFunction FlagFunctionWorker) (WorkerConfig, error) {
workerConfig := DefaultWorkerConfig()
flagFunction()
var (
amqpURI string
saveFiles bool
debug bool
tiff2pdfConverter string
flgVersion bool
numParJobs uint
)
flag.StringVar(
&amqpURI,
"amqp_uri",
"",
"The Amqp URI, eg: amqp://guest:guest@localhost:5672/",
)
flag.BoolVar(
&saveFiles,
"save_files",
false,
"if set there will be no clean up of temporary files",
)
flag.BoolVar(
&debug,
"debug",
false,
"sets debug flag, program will print more messages",
)
flag.StringVar(
&tiff2pdfConverter,
"image_converter",
"convert",
"use convert or tiff2pdf for converting incoming tiff files, e.g. -image_converter {convert,tiff2pdf},"+
"tools must be installed on system",
)
flag.UintVar(
&numParJobs,
"num_parallel_jobs",
1,
"how many messages will be preloaded from a message broker. Can be used to saturate the load"+
" Set the value to 1 for round robbin distribution of messages across workers.",
)
flag.BoolVar(
&flgVersion,
"version",
false,
"show version and exit",
)
flag.Parse()
workerConfig.FlgVersion = flgVersion
if len(amqpURI) > 0 {
workerConfig.AmqpURI = amqpURI
}
if len(tiff2pdfConverter) > 0 {
workerConfig.Tiff2pdfConverter = tiff2pdfConverter
if tiff2pdfConverter != "convert" && tiff2pdfConverter != "tiff2pdf" {
return workerConfig, fmt.Errorf("please choose convert of tiff2pdf as image converter")
}
}
workerConfig.SaveFiles = saveFiles
workerConfig.Debug = debug
workerConfig.NumParallelJobs = numParJobs
return workerConfig, nil
}