This repository was archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreatePostProcessor.js
More file actions
74 lines (66 loc) · 1.86 KB
/
createPostProcessor.js
File metadata and controls
74 lines (66 loc) · 1.86 KB
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
const R = require("ramda");
const { runFor } = require("./utils");
const { eventsDb, publicDb } = require("./config");
const createPostProcessor = (postProcessorName, updateEvent) => {
const getEventsWaitingForPostProcessing = async () => {
const result = await eventsDb.find({
selector: {
type: "EVENT",
status: "postProcessing",
postProcessor: postProcessorName
},
sort: [
{
createdAt: "asc"
}
]
});
return result.docs;
};
const markActionAsError = (event, err) => {
return eventsDb.put(
R.merge(event, {
status: "error",
error: err
})
);
};
const handleAction = async event => {
const { action } = event;
try {
const currentPostProcessor = event.postProcessors.find(
p => p.name === postProcessorName
);
if (currentPostProcessor.status === "done") {
return;
}
const updatedEvent = (await updateEvent(event)) || event;
const currentPostProcessorIndex = event.postProcessors.indexOf(
currentPostProcessor
);
const updatedPostProcessor = R.merge(currentPostProcessor, {
status: "done"
});
const postProcessors = R.update(
currentPostProcessorIndex,
updatedPostProcessor,
event.postProcessors
);
await eventsDb.put(R.merge(updatedEvent, { postProcessors }));
} catch (e) {
console.log(e);
await markActionAsError(event, e);
}
};
const handleEvents = async () => {
const events = await getEventsWaitingForPostProcessing();
console.log(
"Events waiting for " + postProcessorName + " " + events.length
);
for (event of events) {
await handleAction(event);
}
};
return () => runFor(handleEvents, 1000 * 60 * 15, postProcessorName);
};
module.exports = createPostProcessor;