-
Notifications
You must be signed in to change notification settings - Fork 0
/
bull.js
118 lines (105 loc) · 3.7 KB
/
bull.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
let express = require("express");
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
const { createBullBoard } = require("@bull-board/api");
const { BullMQAdapter } = require("@bull-board/api/bullMQAdapter");
const { ExpressAdapter } = require("@bull-board/express");
const { ClientCasper } = require('@casperholders/core');
const client = new ClientCasper(process.env.CASPER_RPC_URL);
const { Queue, Worker, QueueScheduler } = require("bullmq");
const cors = require("cors");
const logger = require("morgan");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const models = require("./models");
const parse = require("parse-duration");
let app = express();
app.use(cors({
origin: process.env.ORIGIN.split(","),
optionsSuccessStatus: 200,
}));
app.use(logger("dev"));
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
if(process.env.DISABLE_REDIS !== 'true'){
async function cleanDatabase() {
const parse = require("parse-duration");
const models = require("./models");
const deploys = await models.Deploy.findAll();
const deploysToDelete = [];
for (let i = 0; i < deploys.length; i++) {
const deploy = deploys[i].deploy.deploy;
const ttl = parse(deploy.header.ttl);
const timestamp = Date.parse(deploy.header.timestamp);
const now = Date.now();
if (now >= ttl + timestamp) {
deploysToDelete.push(deploy.hash);
console.log(`Deploy expired ${deploy.hash}`);
} else {
try {
await client.casperRPC.getDeployInfo(deploy.hash);
deploysToDelete.push(deploy.hash);
} catch (e) {
console.log(`Deploy ${deploy.hash} not sent and not expired. Timestamp : ${deploy.header.timestamp}. TTL : ${deploy.header.ttl}`);
}
}
}
console.log(`Those deploys will be destroyed : ${JSON.stringify(deploysToDelete)}`);
await models.Deploy.destroy({
where: {
hash: deploysToDelete,
},
});
}
const myQueueScheduler = new QueueScheduler("DatabaseCleanup", {
connection: {
host: process.env.REDIS_HOST || "localhost",
port: process.env.REDIS_PORT || "6379",
},
});
const queue = new Queue("DatabaseCleanup", {
connection: {
host: process.env.REDIS_HOST || "localhost",
port: process.env.REDIS_PORT || "6379",
},
});
const worker = new Worker("DatabaseCleanup", async job => {
if (job.name === "cleanup") {
await cleanDatabase();
}
}, {
connection: {
host: process.env.REDIS_HOST || "localhost",
port: process.env.REDIS_PORT || "6379",
},
});
queue.add(
"cleanup",
{},
{
repeat: {
cron: "0 0 * * * *",
},
removeOnComplete: {
age: 604800,
count: 200
},
removeOnFail: {
age: 604800,
count: 200
}
},
);
const serverAdapter = new ExpressAdapter();
const { addQueue, removeQueue, setQueues, replaceQueues } = createBullBoard({
queues: [
new BullMQAdapter(queue),
],
serverAdapter: serverAdapter,
});
serverAdapter.setBasePath('/admin/queues')
app.use('/admin/queues', serverAdapter.getRouter());
}
module.exports = app;