Skip to content

Commit e0d49eb

Browse files
committed
Add support for tasks
1 parent cc8153d commit e0d49eb

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const users = require('./lib/users');
1818
boot.executeInParallel([
1919
boot.forwardHttp,
2020
boot.ensureHttpsCertificates,
21-
boot.ensureDockerTlsCertificates
21+
boot.ensureDockerTlsCertificates,
22+
boot.loadTasks,
2223
], () => {
2324
// You can customize these values in './db.json'.
2425
const hostname = db.get('hostname', 'localhost');

join.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ boot.executeInParallel([
2727
boot.forwardHttp,
2828
boot.ensureHttpsCertificates,
2929
boot.ensureDockerTlsCertificates,
30-
boot.verifyJanitorOAuth2Access
30+
boot.verifyJanitorOAuth2Access,
31+
boot.loadTasks,
3132
], () => {
3233
boot.registerDockerClient(() => {
3334
log('[ok] joined cluster as [hostname = ' + hostname + ']');

lib/boot.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const certificates = require('./certificates');
88
const db = require('./db');
99
const log = require('./log');
1010
const oauth2 = require('./oauth2');
11+
const tasks = require('./tasks');
1112

1213
const hostname = db.get('hostname', 'localhost');
1314

@@ -345,3 +346,10 @@ exports.registerDockerClient = function (next) {
345346
next();
346347
});
347348
};
349+
350+
exports.loadTasks = function (callback) {
351+
setInterval(() => {
352+
tasks.check();
353+
}, 60000);
354+
callback();
355+
};

lib/tasks.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const db = require('./db');
2+
const log = require('./log');
3+
let taskCounter = 0; // Used to generate unique task IDs
4+
5+
exports.tasks = db.get('tasks');
6+
exports.taskTypes = new Map();
7+
8+
exports.check = function () {
9+
const now = Date.now();
10+
for (const task of Object.values(exports.tasks)) {
11+
if (now > Number(task.date)) {
12+
exports.execute(task);
13+
}
14+
}
15+
};
16+
17+
exports.addType = function (type, task) {
18+
if (exports.taskTypes.has(type)) {
19+
throw new Error('[fail] task', task, 'already exists');
20+
}
21+
22+
exports.taskTypes.set(type, task);
23+
};
24+
25+
exports.add = function (date, type, data) {
26+
taskCounter++;
27+
28+
const msSince1970 = date.getTime();
29+
const taskId = `${type}-${msSince1970}-${taskCounter}`;
30+
31+
const task = {
32+
id: taskId,
33+
date: msSince1970,
34+
type,
35+
data,
36+
};
37+
38+
exports.tasks[taskId] = task;
39+
db.save();
40+
return task;
41+
};
42+
43+
exports.remove = function (id) {
44+
delete exports.tasks[id];
45+
db.save();
46+
};
47+
48+
exports.execute = function ({ type, data, id }) {
49+
const task = exports.taskTypes.get(type);
50+
log('[ok] Task', id, 'executed', data);
51+
task(data);
52+
exports.remove(id);
53+
};

0 commit comments

Comments
 (0)