File tree Expand file tree Collapse file tree 7 files changed +80
-33
lines changed Expand file tree Collapse file tree 7 files changed +80
-33
lines changed Original file line number Diff line number Diff line change 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" : {
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" ,
Original file line number Diff line number Diff line change 11import mongoose from 'mongoose' ;
2+ import { logger } from '../logger' ;
23
34export 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
Original file line number Diff line number Diff line change @@ -4,11 +4,7 @@ import { ProblemExtractor } from '../packages/problem-extractor';
44import { logger } from '../logger' ;
55
66export 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}
Original file line number Diff line number Diff line change 11import { app } from "./app" ;
22import { Database } from "./db/db.config" ;
3- import { PopulateProblems } from "./db/problems" ;
43import { logger } from "./logger" ;
4+ import { CronPopulate } from "./packages/cron-populate" ;
55
66const 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 , ( ) => {
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -4,28 +4,18 @@ import { axiosClient } from './axios-client';
44import { LeetcodeProblem } from './interfaces' ;
55
66export 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}
You can’t perform that action at this time.
0 commit comments