-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_docx.go
298 lines (276 loc) · 7.7 KB
/
convert_docx.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
package es
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
"github.com/Bnei-Baruch/sqlboiler/queries"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
)
var httpClient = &http.Client{
Timeout: 600 * time.Second,
}
func DocText(uid string) (string, error) {
resp, err := httpClient.Get(fmt.Sprintf("%s/doc2text/%s", unzipUrl, uid))
if err != nil {
log.Warnf("Error preparing docs, Error: %+v", err)
return "", err
}
if resp.StatusCode == http.StatusOK {
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
bodyString := string(bodyBytes)
return bodyString, nil
} else {
log.Warnf("Response code %d for %s, skip.", resp.StatusCode, uid)
return "", nil
}
}
type unzipResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
func Prepare(uids []string) (error, bool, map[string]int) {
successMap := make(map[string]int)
for _, uid := range uids {
successMap[uid] = -1
}
// Prepare docs batch.
log.Debugf("Preparing %s docs to docx.", strings.Join(uids, ","))
resp, err := httpClient.Get(fmt.Sprintf("%s/prepare/%s", unzipUrl, strings.Join(uids, ",")))
log.Debug("Finished Trying Get.")
if err != nil {
log.Warnf("Error preparing docs, Error: %+v.", err)
return err, false, successMap
}
if resp.StatusCode != http.StatusOK { // OK
log.Errorf("Response code %d for %s, error.", resp.StatusCode, strings.Join(uids, ","))
return err, false, successMap
}
body, err := ioutil.ReadAll(resp.Body)
log.Debug("Finished reading body.")
if err != nil {
log.Error("Could not read response body.")
return err, false, successMap
}
var data []unzipResponse
json.Unmarshal(body, &data)
if len(data) != len(uids) {
return errors.New(fmt.Sprintf("Response length is not as request uids length. Expected %d, got %d", len(uids), len(data))), false, successMap
}
backoff := false
var errors []string
log.Debugf("Ranging over data: %v", data)
for i, internalResponse := range data {
successMap[uids[i]] = internalResponse.Code
if internalResponse.Code == http.StatusServiceUnavailable {
// Backoff
backoff = true
} else if internalResponse.Code != http.StatusOK {
// Don't repeat request, continue.
errors = append(errors, internalResponse.Message)
}
}
if len(errors) > 0 {
log.Warn(strings.Join(errors, ","))
}
if backoff {
log.Debug("Successfully done, backoff: true.")
return nil, true, successMap
} else {
log.Debug("Successfully done, backoff: false.")
return nil, false, successMap
}
}
func loadDocs(db *sql.DB) ([]string, error) {
rows, err := queries.Raw(db, `
SELECT
f.uid
FROM files f
INNER JOIN content_units cu ON f.content_unit_id = cu.id
AND f.name ~ '.docx?$'
AND f.language NOT IN ('zz', 'xx')
AND f.secure = 0
AND f.published IS TRUE
AND cu.secure = 0
AND cu.published IS TRUE
AND cu.type_id != 42;`).Query()
if err != nil {
return nil, errors.Wrap(err, "Load docs")
}
defer rows.Close()
return loadMap(rows)
}
func loadMap(rows *sql.Rows) ([]string, error) {
var m []string
for rows.Next() {
var uid string
err := rows.Scan(&uid)
if err != nil {
return nil, errors.Wrap(err, "rows.Scan")
}
m = append(m, uid)
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "rows.Err()")
}
return m, nil
}
func ConvertDocx(db *sql.DB) error {
docs, err := loadDocs(db)
if err != nil {
return errors.Wrap(err, "Fetch docs from mdb")
}
total := len(docs)
log.Debugf("%d docs in MDB", total)
var notEmptyDocs []string
for _, doc := range docs {
if len(doc) <= 0 {
log.Warn("Empty doc, skipping. Should not happen.")
continue
}
notEmptyDocs = append(notEmptyDocs, doc)
}
batches := make(chan []string)
batchSize := prepareDocsBatchSize
go func(notEmptyDocs []string, batches chan []string) {
for start := 0; start < len(notEmptyDocs); start += batchSize {
end := start + batchSize
if end > len(notEmptyDocs) {
end = len(notEmptyDocs)
}
batches <- notEmptyDocs[start:end]
}
close(batches)
}(notEmptyDocs, batches)
parallelism := prepareDocsParallelism
var waitDone sync.WaitGroup
waitDone.Add(parallelism)
batchesDone := 0
prepareMutex := &sync.Mutex{}
prepareErr := error(nil)
successMap := make(map[string]int)
for i := 0; i < parallelism; i++ {
go func(j int, batches chan []string) {
for batch := range batches {
prepareMutex.Lock()
batchesDone += 1
currentBatchDone := batchesDone
prepareMutex.Unlock()
log.Infof("[%d] Prepare %d / %d", j, currentBatchDone, len(notEmptyDocs)/batchSize)
sleep := 0 * time.Second
tryRetry := true
retries := 5
for ; retries > 0 && tryRetry; retries-- {
log.Debugf("Retry[%d]: %d, tryRetry: %t", j, retries, tryRetry)
if sleep > 0 {
log.Debugf("Bakoff[%d], sleep %.2f, retry: %d", j, sleep.Seconds(), 5-retries)
time.Sleep(sleep)
}
var batchSuccessMap map[string]int
// ERR SHOULD BE LAST
err, tryRetry, batchSuccessMap = Prepare(batch)
if tryRetry {
log.Debugf("Try retry[%d]: true", j)
} else {
log.Debugf("Try retry[%d]: false", j)
}
shouldBreak := false
nextBatch := []string{}
prepareMutex.Lock()
for uid, code := range batchSuccessMap {
currentCode, ok := successMap[uid]
if ok {
if currentCode != http.StatusOK {
successMap[uid] = code
} else if code != http.StatusOK {
errStr := fmt.Sprintf("Making things worse, had %d for uid %s not got %d.", currentCode, uid, code)
log.Error(errStr)
if prepareErr == nil {
prepareErr = errors.New(errStr)
}
}
} else {
successMap[uid] = code
}
if currentCode != http.StatusOK {
nextBatch = append(nextBatch, uid)
}
}
reason := ""
if prepareErr != nil {
shouldBreak = true
reason = prepareErr.Error()
}
prepareMutex.Unlock()
if shouldBreak {
log.Errorf("Breaking[%d]... Due to: %s.", j, reason)
break
}
if err != nil {
log.Warnf("Error while Prepare %d / %d. Error: %s", currentBatchDone, len(notEmptyDocs)/batchSize, err)
prepareMutex.Lock()
if prepareErr != nil {
prepareErr = err
}
prepareMutex.Unlock()
break
}
if tryRetry {
log.Debugf("Trying to retry [%d].", j)
if sleep == 0 {
sleep = 10 * time.Second
} else {
sleep += 10 * time.Second
}
} else {
log.Debugf("Trying not to retry [%d]. Retries: %d.", j, retries)
}
// At next retry, we want to try only failed uids.
batch = nextBatch
}
shouldBreak := false
reason := ""
log.Debugf("[%d] Locking...", j)
prepareMutex.Lock()
if prepareErr != nil {
reason = prepareErr.Error()
shouldBreak = true
} else if retries == 0 {
prepareErr = errors.New(fmt.Sprintf("No more retries[%d]. Exiting.", j))
reason = prepareErr.Error()
shouldBreak = true
}
prepareMutex.Unlock()
log.Debugf("[%d] Unlocking...", j)
if shouldBreak {
log.Errorf("Breaking... Due to: %s.", reason)
break
}
}
log.Infof("[%d] Done", j)
waitDone.Done()
}(i, batches)
}
waitDone.Wait()
reverseSuccessMap := make(map[int]int)
for _, code := range successMap {
if _, ok := reverseSuccessMap[code]; ok {
reverseSuccessMap[code]++
} else {
reverseSuccessMap[code] = 1
}
}
for code, count := range reverseSuccessMap {
log.Infof("Code: %d Count: %d.", code, count)
}
return nil
}