From cb79941f1b015a51765b0542c4df0d415f6d5735 Mon Sep 17 00:00:00 2001 From: Amit Amrutiya Date: Tue, 23 Apr 2024 16:55:58 +0530 Subject: [PATCH] add cron job to backend --- backend/cron.js | 20 ++++++++++++++++++++ backend/index.js | 3 +++ backend/package-lock.json | 21 +++++++++++++++++++++ backend/package.json | 3 ++- frontend/.netlify/state.json | 3 +++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 backend/cron.js create mode 100644 frontend/.netlify/state.json diff --git a/backend/cron.js b/backend/cron.js new file mode 100644 index 0000000..f2248f4 --- /dev/null +++ b/backend/cron.js @@ -0,0 +1,20 @@ +import cron from "cron"; +import https from "https"; + +const URL = process.env.CLOUD_BACKEND_URL; + +const job = new cron.CronJob("*/14 * * * *", function () { + https + .get(URL, (res) => { + if (res.statusCode === 200) { + console.log("GET request sent successfully"); + } else { + console.log("GET request failed", res.statusCode); + } + }) + .on("error", (e) => { + console.error("Error while sending request", e); + }); +}); + +export default job; diff --git a/backend/index.js b/backend/index.js index a15db01..d1fa475 100644 --- a/backend/index.js +++ b/backend/index.js @@ -7,14 +7,17 @@ import contactUsRoute from "./routes/contactUs.route.js" import connectDB from "./config/database.js" import cookieParser from "cookie-parser" import cors from "cors" +import job from "./cron.js"; import { cloudinaryConnect } from "./config/cloudinary.js" import fileUpload from "express-fileupload" import dotenv from "dotenv" + dotenv.config({ path: "./env", }) +job.start(); const app = express() // database connect diff --git a/backend/package-lock.json b/backend/package-lock.json index cb8b80a..caa8030 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -13,10 +13,12 @@ "cloudinary": "^1.41.0", "cookie-parser": "^1.4.6", "cors": "^2.8.5", + "cron": "^3.1.7", "crypto-random-string": "^5.0.0", "dotenv": "^16.3.1", "express": "^4.18.2", "express-fileupload": "^1.4.1", + "http": "^0.0.1-security", "jsonwebtoken": "^9.0.2", "mongoose": "^7.6.3", "node-schedule": "^2.1.1", @@ -2907,6 +2909,11 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/luxon": { + "version": "3.4.2", + "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz", + "integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==" + }, "node_modules/@types/node": { "version": "20.11.17", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.17.tgz", @@ -4199,6 +4206,15 @@ "node": ">=8" } }, + "node_modules/cron": { + "version": "3.1.7", + "resolved": "https://registry.npmjs.org/cron/-/cron-3.1.7.tgz", + "integrity": "sha512-tlBg7ARsAMQLzgwqVxy8AZl/qlTc5nibqYwtNGoCrd+cV+ugI+tvZC1oT/8dFH8W455YrywGykx/KMmAqOr7Jw==", + "dependencies": { + "@types/luxon": "~3.4.0", + "luxon": "~3.4.0" + } + }, "node_modules/cron-parser": { "version": "4.9.0", "resolved": "https://registry.npmjs.org/cron-parser/-/cron-parser-4.9.0.tgz", @@ -6069,6 +6085,11 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, + "node_modules/http": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/http/-/http-0.0.1-security.tgz", + "integrity": "sha512-RnDvP10Ty9FxqOtPZuxtebw1j4L/WiqNMDtuc1YMH1XQm5TgDRaR1G9u8upL6KD1bXHSp9eSXo/ED+8Q7FAr+g==" + }, "node_modules/http-errors": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.0.tgz", diff --git a/backend/package.json b/backend/package.json index b0e267b..87d5453 100644 --- a/backend/package.json +++ b/backend/package.json @@ -19,6 +19,7 @@ "cloudinary": "^1.41.0", "cookie-parser": "^1.4.6", "cors": "^2.8.5", + "cron": "^3.1.7", "crypto-random-string": "^5.0.0", "dotenv": "^16.3.1", "express": "^4.18.2", @@ -50,4 +51,4 @@ "jest": "^29.7.0", "nodemon": "^3.0.1" } -} \ No newline at end of file +} diff --git a/frontend/.netlify/state.json b/frontend/.netlify/state.json new file mode 100644 index 0000000..b06da18 --- /dev/null +++ b/frontend/.netlify/state.json @@ -0,0 +1,3 @@ +{ + "siteId": "2f173ab2-a27f-47ab-931e-a07e183b7690" +} \ No newline at end of file