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
|
/* eslint-disable no-unused-vars */
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
const EventEmitter = require("events");
const uuidv1 = require("uuidv1");
const postgreSQLClient = require("./postgreSQLClient");
const toolbox = require("./toolbox");
class retryProcessor extends EventEmitter {
constructor(logger, requestProcessor) {
super();
this.logger = logger;
this.requestProcessor = requestProcessor;
}
addRetryJob(originalUuid, retryAction, args, delay) {
let _this = this;
const retryUuid = uuidv1();
_this.logger.log(`Setting up ${retryAction}`, "warn", originalUuid);
postgreSQLClient.insert(
"retry_queue", ["uuid", "retryaction", "args"],
[retryUuid, retryAction, toolbox.encodeJSONtoBase64(args)],
function () {
_this.logger.log(
`Retry ${retryAction} registered for ${retryUuid}`,
"verbose", originalUuid
);
// Call retry in 30 seconds or custom delay time.
setTimeout(function () {
_this.emit("processRetry", retryUuid);
}, delay || 30000);
}
);
}
// Process a retry item and call its original callback, which should resume
// the process where it left off.
processRetry(uuid, callback) {
let _this = this;
_this.logger.log(`Processing retry event with uuid ${uuid}`, "debug", "RETRY");
function deleteRetryRecord() {
postgreSQLClient.deleteDBEntry("retry_queue", "uuid", uuid, function (success, data) {});
}
postgreSQLClient.query("retry_queue", undefined, "uuid", uuid, "=", function (success, rows) {
if (success) {
deleteRetryRecord();
let args = toolbox.decodeBase64toJSON(rows[0].args);
_this.logger.log(
`Processing retryRequest "${rows[0].retryaction}" for ${uuid} with args: ${args}`,
"debug"
);
_this.requestProcessor.emit(rows[0].retryaction, ...args);
} else if (callback) {
_this.logger.log(
`Error retrieving retryRequest ${uuid} from the database, ${rows}`,
"error"
);
callback(false, rows);
} else {
// This is a silent failure and may leave orphaned jobs.
// All calls to processRetry should pass a callback for safety.
}
});
}
}
module.exports = retryProcessor;
|