Skip to content

Commit

Permalink
improve scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Proladge committed Apr 20, 2020
1 parent 23f0df8 commit f896c06
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 23 deletions.
4 changes: 4 additions & 0 deletions server/src/bots/telegram/services/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
getAllUsers,
getUser,
addUser,
getNotificationMessage,
} from '../../../services/domain/storage';
import * as firebase from 'firebase';

Expand All @@ -32,6 +33,9 @@ export const getTelegramActiveUserSubscriptions: Function = getActiveUserSubscri
export const getTelegramUser = getUser(TELEGRAM_PREFIX);
export const getTelegramAllUsers = getAllUsers(TELEGRAM_PREFIX);
export const addTelegramUser = addUser(TELEGRAM_PREFIX);
export const getTelegramNotificationMessage = getNotificationMessage(
TELEGRAM_PREFIX
);
export const setTelegramSubscription: Function = setSubscription(
TELEGRAM_PREFIX
);
Expand Down
30 changes: 21 additions & 9 deletions server/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import * as dotenv from 'dotenv';
const environmentName = process.env.ENVIRONMENT_NAME ?? 'development';

if (environmentName === 'development') {
dotenv.config({path: `${__dirname}/.env`});
dotenv.config({ path: `${__dirname}/.env` });
} else {
dotenv.config({path: `${__dirname}/.env.prod`});
dotenv.config({ path: `${__dirname}/.env.prod` });
}

const tags = process.env.LOGGLY_TAGS && Array.isArray(process.env.LOGGLY_TAGS)
? process.env.LOGGLY_TAGS
: ['covid19liveupd'];
const tags =
process.env.LOGGLY_TAGS && Array.isArray(process.env.LOGGLY_TAGS)
? process.env.LOGGLY_TAGS
: ['covid19liveupd'];

tags.push('containerV' + process.env.CONTAINER_VERSION, 'pkgV' + process.env.npm_package_version, environmentName);
tags.push(
'containerV' + process.env.CONTAINER_VERSION,
'pkgV' + process.env.npm_package_version,
environmentName
);

let envConfig = {
ENV: environmentName,
Expand All @@ -31,7 +36,8 @@ let envConfig = {
FIREBASE_DATABASE_URL: process.env.FIREBASE_DATABASE_URL ?? '',
FIREBASE_PROJECT_ID: process.env.FIREBASE_PROJECT_ID ?? '',
FIREBASE_STORAGE_BUCKET: process.env.FIREBASE_STORAGE_BUCKET ?? '',
FIREBASE_MESSAGING_SENDER_ID: process.env.FIREBASE_MESSAGING_SENDER_ID ?? '',
FIREBASE_MESSAGING_SENDER_ID:
process.env.FIREBASE_MESSAGING_SENDER_ID ?? '',
FIREBASE_APP_ID: process.env.FIREBASE_APP_ID ?? '',
CHARTSAPI_URL: 'https://quickchart.io/chart',
IsProduction() {
Expand All @@ -42,13 +48,19 @@ let envConfig = {
},
IsNgRokMode() {
return this.IsDevelopment();
}
},
features: {
ScheduledNotification: {
enabled: process.env.EnableScheduledNotification || false,
cronExpr: process.env.ScheduledNotificationCronExpr,
},
},
};

if (!envConfig.IsProduction()) {
envConfig = {
...envConfig,
LOGGLY_TAGS: tags
LOGGLY_TAGS: tags,
};
}

Expand Down
10 changes: 10 additions & 0 deletions server/src/services/domain/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,13 @@ export const addUser = (messengerPrefix: string) => async (
.ref(`${messengerPrefix}/users/${user.chatId}`)
.set(user);
};

export const getNotificationMessage = (
messengerPrefix: string
) => async (): Promise<string> => {
const snapshot = await firebase
.database()
.ref(`${messengerPrefix}/notificationMessage`)
.once('value');
return snapshot.val() ?? {};
};
63 changes: 49 additions & 14 deletions server/src/services/infrastructure/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import * as schedule from 'node-schedule';
import { tryToUpdateCovid19Cache } from '../domain/covid19';
import { logger } from '../../utils/logger';
import { LogCategory } from '../../models/constants';
import { getTelegramAllUsers } from '../../bots/telegram/services/storage';
import {
getTelegramAllUsers,
getTelegramNotificationMessage,
} from '../../bots/telegram/services/storage';
import { catchAsyncError } from '../../utils/catchError';
import TelegramBot = require('node-telegram-bot-api');
import environments from '../../environments/environment';
import { getUserName } from '../../messages/userMessage';

export const checkCovid19Updates = () => {
// Check covid19 info every hour (at hh:30 mins, e.g. 1:30, 2:30 ...)
Expand All @@ -19,20 +24,50 @@ export const checkCovid19Updates = () => {
};

export const sendReleaseNotificationToUsers = async (bot: TelegramBot) => {
// At 08:00 PM, only on Monday
schedule.scheduleJob('0 0 20 * * 1', async () => {
const [err, users] = await catchAsyncError(getTelegramAllUsers());
if (err) {
logger.error(
'sendReleaseNotificationToUsers failed when accessing users Db',
err,
LogCategory.Scheduler
// At 08:00 PM, every day
schedule.scheduleJob(
environments.features.ScheduledNotification.cronExpr || '0 0 20 * * *',
async () => {
if (!environments.features.ScheduledNotification.enabled) {
logger.log(
'info',
'SchedulerNotification was launched, but currently disabled',
LogCategory.Scheduler
);
return;
}
const [err, users] = await catchAsyncError(getTelegramAllUsers());
if (err) {
logger.error(
'sendReleaseNotificationToUsers failed when accessing users Db',
err,
LogCategory.Scheduler
);
return;
}
const [err1, message] = await catchAsyncError(
getTelegramNotificationMessage()
);
return;
}
if (err) {
logger.error(
'sendReleaseNotificationToUsers failed when accessing getTelegramNotificationMessage()',
err,
LogCategory.Scheduler
);
return;
}
if (!message) {
logger.error(
'getTelegramNotificationMessage() returns empty message',
err,
LogCategory.Scheduler
);
}

for (const usr of users) {
bot.sendMessage(usr.chatId, 'Hello DUDE, What\'s UP? ');
for (const usr of users) {
message.replace('#UserName#', getUserName(usr as any));
bot.sendMessage(usr.chatId, 'Hello DUDE, What\'s UP? ');
}
}
});
);
};

0 comments on commit f896c06

Please sign in to comment.