Skip to content

Commit 39bc405

Browse files
authored
Merge pull request #6 from leetcode-roulette/feature/setup-cron-job
Feature/setup cron job
2 parents 760ef6d + ae940bb commit 39bc405

File tree

7 files changed

+80
-33
lines changed

7 files changed

+80
-33
lines changed

package-lock.json

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"dotenv": "^16.0.2",
3535
"express": "^4.18.1",
3636
"mongoose": "^6.6.1",
37+
"node-cron": "^3.0.2",
3738
"winston": "^3.8.2"
3839
},
3940
"devDependencies": {
@@ -43,6 +44,7 @@
4344
"@types/express": "^4.17.14",
4445
"@types/jest": "^29.0.3",
4546
"@types/node": "^18.7.18",
47+
"@types/node-cron": "^3.0.4",
4648
"babel-jest": "^29.0.3",
4749
"jest": "^29.0.3",
4850
"jest-rest-preset": "^0.0.4",

src/db/db.config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import mongoose from 'mongoose';
2+
import { logger } from '../logger';
23

34
export class Database {
45
private static url : string | undefined;
@@ -7,11 +8,11 @@ export class Database {
78
mongoose.connect(this.connectionString);
89

910
mongoose.connection.once("open", async () : Promise<void> => {
10-
console.log("Connected to database");
11+
logger.info("Connected to database");
1112
});
1213

1314
mongoose.connection.on("error", async (e) : Promise<void> => {
14-
console.log("Error connectiong to database", e);
15+
logger.error("Error connectiong to database", e);
1516
});
1617
}
1718

@@ -23,7 +24,7 @@ export class Database {
2324
mongoose.disconnect();
2425

2526
mongoose.connection.close(async () : Promise<void> => {
26-
console.log("Disconnected from database");
27+
logger.info("Disconnected from database");
2728
})
2829
}
2930

src/db/problems.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { ProblemExtractor } from '../packages/problem-extractor';
44
import { logger } from '../logger';
55

66
export class PopulateProblems {
7-
private static data : Promise<LeetcodeProblem[] | null> | null = null;
8-
9-
public static async populate() : Promise<void> {
10-
const problems = await this.problems;
11-
7+
public static async populate(problems : LeetcodeProblem[] | null) : Promise<void> {
128
if (problems === null) {
139
return;
1410
}
@@ -62,12 +58,4 @@ export class PopulateProblems {
6258

6359
await p.save();
6460
}
65-
66-
public static get problems() : Promise<LeetcodeProblem[] | null> | null {
67-
if (this.data === null) {
68-
this.data = ProblemExtractor.problems;
69-
}
70-
71-
return this.data;
72-
}
7361
}

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { app } from "./app";
22
import { Database } from "./db/db.config";
3-
import { PopulateProblems } from "./db/problems";
43
import { logger } from "./logger";
4+
import { CronPopulate } from "./packages/cron-populate";
55

66
const serve = async () : Promise<void> => {
77
const PORT : string | number = process.env.PORT || 3000;
88

99
try {
1010
await Database.connect();
11-
await PopulateProblems.populate();
11+
CronPopulate.schedule(process.env.CRON_EXPRESSION || "0 * * * *");
1212
} catch(e) {
13-
logger.error("Exception caught populating database: " + e);
13+
logger.error("Exception caught scheduling cron job: " + e);
1414
}
1515

1616
app.listen(PORT, () => {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import cron, { ScheduledTask } from "node-cron";
2+
import { ProblemExtractor } from "../problem-extractor";
3+
import { PopulateProblems } from "../../db/problems";
4+
import { logger } from "../../logger";
5+
6+
export class CronPopulate {
7+
public static async schedule(cronExpression: string) : Promise<ScheduledTask> {
8+
return cron.schedule(cronExpression, this.run);
9+
}
10+
11+
private static async run() : Promise<void> {
12+
try {
13+
const problems = await ProblemExtractor.problems;
14+
await PopulateProblems.populate(problems);
15+
logger.info("Successfully updated database through cron job");
16+
} catch(e) {
17+
logger.error("Exception caught running cron job: " + e);
18+
}
19+
}
20+
}

src/packages/problem-extractor/index.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,18 @@ import { axiosClient } from './axios-client';
44
import { LeetcodeProblem } from './interfaces';
55

66
export class ProblemExtractor {
7-
private static parsedData : Promise<LeetcodeProblem[] | null> | null = null;
8-
97
public static get problems() : Promise<LeetcodeProblem[] | null> | null {
10-
if (this.parsedData === null) {
11-
this.parsedData = this.makeAPICallAndGetData();
12-
}
13-
14-
return this.parsedData;
8+
return this.makeAPICallAndGetData();
159
}
1610

1711
private static async makeAPICallAndGetData() : Promise<LeetcodeProblem[] | null> {
18-
let data : AxiosResponse;
19-
2012
try {
21-
data = await axiosClient.get("/api/problems/all");
13+
const data : AxiosResponse = await axiosClient.get("/api/problems/all");
14+
const parsedData : LeetcodeProblem[] = data.data.stat_status_pairs;
15+
return parsedData;
2216
} catch(e) {
2317
logger.error("Exception caught getting data from leetcode API " + e);
2418
return null;
2519
}
26-
27-
const parsedData : LeetcodeProblem[] = data.data.stat_status_pairs;
28-
29-
return parsedData;
3020
}
3121
}

0 commit comments

Comments
 (0)