Add event handler
This commit is contained in:
parent
2426b77f6c
commit
5910414e39
40
eventWatcher.js
Normal file
40
eventWatcher.js
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
const db = global.db;
|
||||||
|
const { EventEmitter } = require('events');
|
||||||
|
const log = require("./logger");
|
||||||
|
|
||||||
|
log.info("Starting event watcher...");
|
||||||
|
|
||||||
|
const emitter = new EventEmitter();
|
||||||
|
|
||||||
|
var lastEventIndex = 0;
|
||||||
|
|
||||||
|
async function getLastEventIndex() {
|
||||||
|
const result = await db.query('SELECT EventIndex FROM Events ORDER BY EventIndex DESC LIMIT 1');
|
||||||
|
if (result && result.length > 0) {
|
||||||
|
lastEventIndex = result[0].EventIndex;
|
||||||
|
log.info(`Last event index: ${lastEventIndex}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getLastEventIndex();
|
||||||
|
|
||||||
|
async function fetchEvents() {
|
||||||
|
try {
|
||||||
|
const events = await db.query('SELECT * FROM Events WHERE EventIndex > ? ORDER BY EventIndex ASC', [lastEventIndex]);
|
||||||
|
if (events && events.length > 0) {
|
||||||
|
log.debug(`Fetched ${events.length} new event(s)`);
|
||||||
|
events.forEach(event => {
|
||||||
|
emitter.emit('event', event);
|
||||||
|
lastEventIndex = event.EventIndex;
|
||||||
|
log.debug(`Processed event index: ${lastEventIndex}; ${JSON.stringify(event)}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
log.error(`Error fetching events: ${err}`);
|
||||||
|
}
|
||||||
|
setTimeout(fetchEvents, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchEvents();
|
||||||
|
|
||||||
|
module.exports = emitter;
|
4
index.js
4
index.js
|
@ -107,7 +107,9 @@ app.listen(port, (err) => {
|
||||||
global.log.error(`Cannot create default user: ${err}`);
|
global.log.error(`Cannot create default user: ${err}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
|
global.dbEvent = require("./eventWatcher.js");
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
global.log.error(`Database connection failed: ${err}`);
|
global.log.error(`Database connection failed: ${err}`);
|
||||||
|
|
37
package-lock.json
generated
37
package-lock.json
generated
|
@ -17,6 +17,7 @@
|
||||||
"ejs": "^3.1.10",
|
"ejs": "^3.1.10",
|
||||||
"express": "^5.1.0",
|
"express": "^5.1.0",
|
||||||
"express-session": "^1.18.2",
|
"express-session": "^1.18.2",
|
||||||
|
"express-ws": "^5.0.2",
|
||||||
"mariadb": "^3.4.5",
|
"mariadb": "^3.4.5",
|
||||||
"uhppoted": "^0.8.11"
|
"uhppoted": "^0.8.11"
|
||||||
}
|
}
|
||||||
|
@ -807,6 +808,21 @@
|
||||||
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
|
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/express-ws": {
|
||||||
|
"version": "5.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/express-ws/-/express-ws-5.0.2.tgz",
|
||||||
|
"integrity": "sha512-0uvmuk61O9HXgLhGl3QhNSEtRsQevtmbL94/eILaliEADZBHZOQUAiHFrGPrgsjikohyrmSG5g+sCfASTt0lkQ==",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"ws": "^7.4.6"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.5.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"express": "^4.0.0 || ^5.0.0-alpha.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/file-uri-to-path": {
|
"node_modules/file-uri-to-path": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
|
||||||
|
@ -2490,6 +2506,27 @@
|
||||||
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
|
"node_modules/ws": {
|
||||||
|
"version": "7.5.10",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
|
||||||
|
"integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8.3.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": "^5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/yallist": {
|
"node_modules/yallist": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
"ejs": "^3.1.10",
|
"ejs": "^3.1.10",
|
||||||
"express": "^5.1.0",
|
"express": "^5.1.0",
|
||||||
"express-session": "^1.18.2",
|
"express-session": "^1.18.2",
|
||||||
|
"express-ws": "^5.0.2",
|
||||||
"mariadb": "^3.4.5",
|
"mariadb": "^3.4.5",
|
||||||
"uhppoted": "^0.8.11"
|
"uhppoted": "^0.8.11"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue