From 2e0b8a471289f2e04286494a0a6333976b95e01a Mon Sep 17 00:00:00 2001 From: Bobby Date: Thu, 23 Jun 2022 15:07:44 +0530 Subject: added cron job for connecting to the database --- package-lock.json | 30 +++++++++++++++++++++++++++++- package.json | 4 +++- public/fonts/.DS_Store | Bin 0 -> 6148 bytes server.js | 37 +++++++++++++++++++++++++++++-------- views/account.ejs | 2 +- views/partials/header.ejs | 2 +- 6 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 public/fonts/.DS_Store diff --git a/package-lock.json b/package-lock.json index ea8d92f4..17e83ffa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,7 +22,9 @@ "jsonwebtoken": "^8.5.1", "md5": "^2.3.0", "mysql2": "^2.3.3", - "node-fetch": "^3.2.5" + "node-cron": "^3.0.1", + "node-fetch": "^3.2.5", + "uuid": "^8.3.2" }, "devDependencies": { "nodemon": "^2.0.16" @@ -1543,6 +1545,14 @@ "node": ">= 0.6" } }, + "node_modules/node-cron": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.1.tgz", + "integrity": "sha512-RAWZTNn2M5KDIUV/389UX0EXsqvdFAwc9QwHQceh0Ga56dygqSRthqIjwpgZsoDspHGt2rkHdk9Z4RgfPMdALw==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -2320,6 +2330,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", @@ -3559,6 +3577,11 @@ "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", "integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==" }, + "node-cron": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.1.tgz", + "integrity": "sha512-RAWZTNn2M5KDIUV/389UX0EXsqvdFAwc9QwHQceh0Ga56dygqSRthqIjwpgZsoDspHGt2rkHdk9Z4RgfPMdALw==" + }, "node-domexception": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", @@ -4127,6 +4150,11 @@ "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" }, + "uuid": { + "version": "8.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", + "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" + }, "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 2ac6944c..d20de2f3 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,9 @@ "jsonwebtoken": "^8.5.1", "md5": "^2.3.0", "mysql2": "^2.3.3", - "node-fetch": "^3.2.5" + "node-cron": "^3.0.1", + "node-fetch": "^3.2.5", + "uuid": "^8.3.2" }, "devDependencies": { "nodemon": "^2.0.16" diff --git a/public/fonts/.DS_Store b/public/fonts/.DS_Store new file mode 100644 index 00000000..48331f8d Binary files /dev/null and b/public/fonts/.DS_Store differ diff --git a/server.js b/server.js index 5946aba5..dfc25a91 100644 --- a/server.js +++ b/server.js @@ -1,11 +1,15 @@ const express = require("express"); const cors = require("cors"); -const bodyParser = require("body-parser"); -const cookieParser = require('cookie-parser'); -const flash = require('connect-flash'); -const expressSession = require('express-session'); +const cookieParser = require("cookie-parser"); +const flash = require("connect-flash"); +const expressSession = require("express-session"); +const mysql = require("mysql2"); const app = express(); const port = process.env.PORT || 3000; +const connectionURL = process.env.DATABASE_URL; +const validationString = process.env.AUTHORIZATION_STRING; +const cron = require("node-cron"); + require("dotenv").config(); // Middleware @@ -13,10 +17,12 @@ app.use(cors()); app.use(express.json()); app.use(cookieParser()); app.use(express.urlencoded({ extended: true })); -app.use(expressSession({ - cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, - secret: process.env.AUTHORIZATION_STRING, -})); +app.use( + expressSession({ + cookie: { maxAge: 30 * 24 * 60 * 60 * 1000 }, + secret: process.env.AUTHORIZATION_STRING, + }) +); app.use(flash()); // Set Template Engine @@ -31,6 +37,21 @@ app.set("views", __dirname + "/views"); // Routes app.use("/", require("./routes")); +// Run a cron job every 6 days to connect to the database - so that the database does not sleep +cron.schedule("0 0 */6 * *", () => { + console.log("Cron job running"); + const connection = mysql.createConnection(connectionURL); + connection.connect(); + connection.query("SELECT 1", (err, results, fields) => { + if (err) { + console.log(err); + } else { + console.log("Database connected"); + } + }); + connection.end(); +}); + // Start server app.listen(port, () => { console.log(`Server started on port ${port}`); diff --git a/views/account.ejs b/views/account.ejs index 8d9be33f..d1b805b0 100644 --- a/views/account.ejs +++ b/views/account.ejs @@ -15,7 +15,7 @@
Avatar - <%= user.username %>'s avatar + <%= user.username %>'s avatar
diff --git a/views/partials/header.ejs b/views/partials/header.ejs index 96b0ac5f..74d9e117 100644 --- a/views/partials/header.ejs +++ b/views/partials/header.ejs @@ -6,7 +6,7 @@ That Computer Scientist - <%= title %> - + \ No newline at end of file -- cgit v1.2.3