diff --git a/framework/package-lock.json b/framework/package-lock.json index 93ae401..a90f4c5 100644 --- a/framework/package-lock.json +++ b/framework/package-lock.json @@ -17,6 +17,7 @@ "jsonwebtoken": "^9.0.2", "moleculer": "^0.14.26", "moleculer-auto-openapi": "^1.1.6", + "moleculer-cron": "^0.0.4", "moleculer-db": "^0.8.25", "moleculer-db-adapter-mongo": "^0.4.19", "moleculer-web": "^0.10.4", @@ -4971,6 +4972,14 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true }, + "node_modules/cron": { + "version": "1.8.2", + "resolved": "https://registry.npmjs.org/cron/-/cron-1.8.2.tgz", + "integrity": "sha512-Gk2c4y6xKEO8FSAUTklqtfSr7oTq0CiPQeLBG5Fl0qoXpZyMcj1SG59YL+hqq04bu6/IuEA7lMkYDAplQNKkyg==", + "dependencies": { + "moment-timezone": "^0.5.x" + } + }, "node_modules/cross-spawn": { "version": "7.0.3", "dev": true, @@ -9905,6 +9914,20 @@ "resolved": "https://registry.npmjs.org/swagger-ui-dist/-/swagger-ui-dist-4.19.1.tgz", "integrity": "sha512-n/gFn+R7G/BXWwl5UZLw6F1YgWOlf3zkwGlsPhTMhNtAAolBGKg0JS5b2RKt5NI6/hSopVaSrki2wTIMUDDy2w==" }, + "node_modules/moleculer-cron": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/moleculer-cron/-/moleculer-cron-0.0.4.tgz", + "integrity": "sha512-pRUjcwbzJsBf4gqm9yr+apGoZ1AVXUmbl3hk/3xbrwRF1tiTO9K2PxUOrznm8OYjEd3UQu7RFj5Dx1kxuHk3tw==", + "dependencies": { + "cron": "^1.8.2" + }, + "engines": { + "node": ">= 6.x.x" + }, + "peerDependencies": { + "moleculer": "^0.14.32" + } + }, "node_modules/moleculer-db": { "version": "0.8.25", "resolved": "https://registry.npmjs.org/moleculer-db/-/moleculer-db-0.8.25.tgz", @@ -9996,6 +10019,25 @@ "version": "4.0.0", "license": "ISC" }, + "node_modules/moment": { + "version": "2.30.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.30.1.tgz", + "integrity": "sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==", + "engines": { + "node": "*" + } + }, + "node_modules/moment-timezone": { + "version": "0.5.45", + "resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.45.tgz", + "integrity": "sha512-HIWmqA86KcmCAhnMAN0wuDOARV/525R2+lOLotuGFzn4HO+FH+/645z2wx0Dt3iDv6/p61SIvKnDstISainhLQ==", + "dependencies": { + "moment": "^2.29.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/mongodb": { "version": "3.7.4", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.7.4.tgz", diff --git a/framework/package.json b/framework/package.json index 75aaeed..7682166 100644 --- a/framework/package.json +++ b/framework/package.json @@ -74,6 +74,7 @@ "jsonwebtoken": "^9.0.2", "moleculer": "^0.14.26", "moleculer-auto-openapi": "^1.1.6", + "moleculer-cron": "^0.0.4", "moleculer-db": "^0.8.25", "moleculer-db-adapter-mongo": "^0.4.19", "moleculer-web": "^0.10.4", diff --git a/framework/services/httppull.service.js b/framework/services/httppull.service.js index 5c97461..dcca8cd 100644 --- a/framework/services/httppull.service.js +++ b/framework/services/httppull.service.js @@ -9,17 +9,35 @@ */ const ApiGateway = require("moleculer-web"); // Included for Invalid Authentication Errors const axios = require("axios"); - +const Cron = require("moleculer-cron"); /** @type {ServiceSchema} */ module.exports = { name: "httppull", + mixins: [Cron], /** * Dependencies */ dependencies: ["httppull_model"], + crons: [ + { + name: "Scheduled HTTP Pulls", + cronTime: '*/5 * * * *', + manualStart: true, + timeZone: 'Europe/Berlin', + onTick: async function() { + console.log('Scheduled HTTP Pulls triggered'); + }, + runOnInit: function() { + console.log("Scheduled HTTP Pulls is created"); + }, + onComplete: function() { + console.log("Scheduled HTTP Pulls is finished"); + } + } + ], /** * Actions */ @@ -76,6 +94,10 @@ module.exports = { } }); const rule = results[0].processor; + results[0].lastFetch = new Date().getTime(); + results[0].id = results[0]._id; + await ctx.call("httppull_model.update",results[0]); + const Handlebars = require('handlebars'); function convertObject(obj, rulesTemplate) { @@ -92,6 +114,25 @@ module.exports = { } } }, + scheduledFetch: { + rest: { + method: "GET", + path: "/scheduledFetch" + }, + async handler(ctx) { + const results = await ctx.call("httppull_model.find",{ + $or: [ + {lastFetch: { $exists: false }}, + {lastFetch: { $gt: new Date().getTime() - 86400000 }} + ] + }); + let res = []; + for(let i=0;i