-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (123 loc) · 3.42 KB
/
app.js
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
// eslint-disable-next-line import/no-unresolved
import { app, errorHandler } from "mu";
import { CronJob } from "cron";
// eslint-disable-next-line import/no-unresolved
import bodyParser from "body-parser";
import {
getUnprocessedPublishedResources,
updateStatus,
PENDING_STATUS,
SUCCESS_STATUS,
FAILED_STATUS,
} from "./support/queries";
import { startPipeline } from "./support/pipeline";
const PENDING_TIMEOUT = process.env.PENDING_TIMEOUT_HOURS || 3;
const CRON_FREQUENCY = process.env.CACHING_CRON_PATTERN || "0 */5 * * * *";
const MAX_ATTEMPTS = parseInt(process.env.MAX_ATTEMPTS || 10, 10);
const SEARCH_GRAPH =
process.env.SEARCH_GRAPH || "http://mu.semte.ch/graphs/public";
console.info(`besluit-publicatie-publish-service starting at ${new Date()}`);
console.debug({
PENDING_TIMEOUT,
CRON_FREQUENCY,
MAX_ATTEMPTS,
SEARCH_GRAPH,
});
async function startPublishing(origin = "http call") {
console.log(`Service triggered by ${origin} at ${new Date().toISOString()}`);
const unprocessedResources = await getUnprocessedPublishedResources(
SEARCH_GRAPH,
PENDING_TIMEOUT,
MAX_ATTEMPTS,
);
console.log(`Found ${unprocessedResources.length} to process`);
// lock resources (yes yes should be batch operation)
for (const item of unprocessedResources) {
console.log(`-- Locking resources: ${item.resource}`);
await updateStatus(item, PENDING_STATUS, item.numberOfRetries);
item.numberOfRetries = parseInt(item.numberOfRetries || 0, 10) + 1;
}
for (const item of unprocessedResources) {
console.log(`Start processing: ${item.resource}`);
try {
await startPipeline(item);
await updateStatus(item, SUCCESS_STATUS, item.numberOfRetries);
} catch (e) {
console.log(`Error processing: ${item.resource}`);
console.log(e);
await updateStatus(item, FAILED_STATUS, item.numberOfRetries);
}
}
}
class PublishingQueue {
constructor() {
this.queue = [];
this.run();
}
async run() {
if (this.queue.length > 0) {
console.log("executing oldest task on queue");
try {
await startPublishing(this.queue.shift());
} catch (e) {
const errorMessage = e.message ? e.message : e;
console.error(`publishing failed: ${errorMessage}`);
console.info(e);
// eslint-disable-next-line no-use-before-define
queue.addJob("queue failure");
} finally {
setTimeout(() => {
this.run();
}, 500);
}
} else {
setTimeout(() => {
this.run();
}, 3000);
}
}
addJob(origin) {
this.queue.push(origin);
}
}
const queue = new PublishingQueue();
// eslint-disable-next-line no-new
new CronJob(
CRON_FREQUENCY,
async () => {
try {
queue.addJob("cron job");
} catch (err) {
console.log("We had a bonobo");
console.log(err);
}
},
null,
true,
);
// Also parse application/json as json
app.use(
bodyParser.json({
type(req) {
return /^application\/json/.test(req.get("content-type"));
},
limit: "500mb",
}),
);
/**
* Starts extracting the published resources.
*/
app.post("/publish-tasks", async (req, res) => {
try {
queue.addJob("http call");
res.send({ success: true });
} catch (err) {
console.log("We had a bonobo");
console.log(err);
res.status(500).send({
message: `An error occurred while publishing`,
err: JSON.stringify(err),
});
}
});
app.use(errorHandler);