Skip to content

Commit

Permalink
feat: cron update bus routes
Browse files Browse the repository at this point in the history
  • Loading branch information
kirosc committed Jun 3, 2021
1 parent aced2c9 commit 33dbc31
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
33 changes: 32 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tg-hketa",
"version": "2.1.0",
"version": "2.1.1",
"engines": {
"node": "16.x"
},
Expand Down Expand Up @@ -34,6 +34,7 @@
"@types/lodash": "^4.14.170",
"@types/luxon": "^1.26.5",
"axios": "^0.21.1",
"cron": "^1.8.2",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"lodash": "^4.17.21",
Expand All @@ -48,6 +49,7 @@
"universal-analytics": "^0.4.23"
},
"devDependencies": {
"@types/cron": "^1.7.2",
"@types/universal-analytics": "^0.4.4",
"husky": "^6.0.0",
"jest": "^27.0.4",
Expand Down
1 change: 1 addition & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('dotenv').config();
import './config/alias';
import '@root/cronjob';
import express, { json } from 'express';
import { Telegraf, session, Context, Scenes } from 'telegraf';
import { SceneSession } from 'telegraf/typings/scenes';
Expand Down
33 changes: 33 additions & 0 deletions src/cronjob.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import cronjob from 'cron';
import { DateTime } from 'luxon';
import { getBusRoutes } from '@services/routes';

const every12Hours = '0 0 */12 * * *';

const updateJob = new cronjob.CronJob(
every12Hours,
updateBusRoutes,
null,
false,
'Asia/Hong_Kong',
null,
true
);

updateJob.start();

async function updateBusRoutes() {
console.info(
`Start updating bus routes data at ${DateTime.now().toString()}`
);

try {
await getBusRoutes();
} catch (error) {
console.error('Error on updating bus routes', error);
}

console.info(
`Successfully updated bus routes data at ${DateTime.now().toString()}`
);
}
2 changes: 1 addition & 1 deletion src/services/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function getNLBRotues() {
);
}

async function getBusRoutes() {
export async function getBusRoutes() {
const ctbJob = axios.get(`${BRAVO_BUS_ENDPOINT}/route/CTB`);
const nwfbJob = axios.get(`${BRAVO_BUS_ENDPOINT}/route/NWFB`);
const kmbJob = axios.get(`${KMB_ENDPOINT}/route`);
Expand Down

0 comments on commit 33dbc31

Please sign in to comment.