diff --git a/package-lock.json b/package-lock.json index fb829aa..76f37fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "body-parser": "^1.20.2", "express": "^4.18.2", + "node-cron": "^3.0.2", "pino": "^8.15.0", "pino-http": "^8.4.0", "promised-sqlite3": "^2.1.0", @@ -1412,6 +1413,17 @@ "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-4.3.0.tgz", "integrity": "sha512-73sE9+3UaLYYFmDsFZnqCInzPyh3MqIwZO9cw58yIqAZhONrrabrYyYe3TuIqtIiOuTXVhsGau8hcrhhwSsDIQ==" }, + "node_modules/node-cron": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.2.tgz", + "integrity": "sha512-iP8l0yGlNpE0e6q1o185yOApANRe47UPbLf4YxfbiNHt/RU5eBcGB/e0oudruheSf+LQeDMezqC5BVAb5wwRcQ==", + "dependencies": { + "uuid": "8.3.2" + }, + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/node-fetch": { "version": "2.6.12", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.12.tgz", @@ -2285,6 +2297,14 @@ "node": ">= 0.4.0" } }, + "node_modules/uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index 97b1abc..3597af6 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "body-parser": "^1.20.2", "express": "^4.18.2", + "node-cron": "^3.0.2", "pino": "^8.15.0", "pino-http": "^8.4.0", "promised-sqlite3": "^2.1.0", diff --git a/src/index.mjs b/src/index.mjs index 6446a23..4ac4283 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -2,6 +2,7 @@ import express from "express"; import bodyParser from "body-parser"; import logger from "./logger.mjs"; import pinoHTTP from 'pino-http' +import start from "./start.mjs"; const app = express() const port = 4000 app.use(bodyParser.json()) @@ -11,20 +12,19 @@ app.use( }) ) app.post('/api', (req,res) => { - console.log("webhook received:") - console.dir(req.body.body.note.text) + logger.info("webhook received:",req.body.body.note.text) res.sendStatus(200) }) app.listen(port, () => { - console.log(`listening on port ${port}`) + logger.trace(`listening on port ${port}`) }) process.on('uncaughtException', (err) => { // log the exception logger.fatal(err, 'uncaught exception detected'); // shutdown the server gracefully - server.close(() => { + app.close(() => { process.exit(1); // then exit }); @@ -34,4 +34,6 @@ process.on('uncaughtException', (err) => { process.abort(); // exit immediately and generate a core dump file }, 1000).unref() process.exit(1); - }); \ No newline at end of file + }); + +start() \ No newline at end of file diff --git a/src/start.mjs b/src/start.mjs new file mode 100644 index 0000000..57285d5 --- /dev/null +++ b/src/start.mjs @@ -0,0 +1,8 @@ +import cron from 'node-cron' +import logger from './logger.mjs'; + +export default function start(){ + cron.schedule('* * * * *', () => { + logger.trace('running a task every minute'); + }); +} \ No newline at end of file