Skip to content

Commit

Permalink
feat: cron job to make get request
Browse files Browse the repository at this point in the history
  • Loading branch information
shivam-sharma7 committed Oct 20, 2024
1 parent bff1410 commit 48daab4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"description": "This is api project for world clockify",
"dependencies": {
"cron": "^3.1.7",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"express-rate-limit": "^7.4.1",
Expand Down
18 changes: 18 additions & 0 deletions api/src/cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cron from 'cron';
import https from 'https';

const URL: string = 'https://world-clockify-api.onrender.com/';

export const job = new cron.CronJob('*/10 * * * * *', function () {
https
.get(URL, (res) => {
if (res.statusCode === 200) {
console.log('Get request sent successfully');
} else {
console.log('Get request failed');
}
})
.on('error', (e) => {
console.error('Error while sending req', e);
});
});
3 changes: 3 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import express, { Request, Response } from 'express';
import dotenv from 'dotenv';

import timezoneRoutes from './routes/timezone.routes';
import { job } from './cron';

dotenv.config();

job.start();

const PORT = process.env.PORT || 5000;

const app = express();
Expand Down

0 comments on commit 48daab4

Please sign in to comment.