From 07c080f249ad630204a6ff2953c9c975c3efc126 Mon Sep 17 00:00:00 2001 From: Ralfs Date: Sat, 10 Apr 2021 01:26:00 +0300 Subject: [PATCH] Put cron in a function --- src/index.ts | 64 ++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index 239b9c3..3ec8fd5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -79,34 +79,38 @@ app.listen(config.port, () => { sendLogsToDiscord(readyMessage, null); }); -cron.schedule("1 * * * *", async () => { - try { - const mongoConnection = await MongoClient.connect(config.dbConnectionString, { useUnifiedTopology: true }); - const db = mongoConnection.db(config.db); - const alerts = await db.collection('alerts').find({open: true}).toArray(); - const uniqueMangoGroupPks: string[] = [...new Set(alerts.map(alert => alert.mangoGroupPk))]; - const mangoGroups:any = await reduceMangoGroups(client, connection, uniqueMangoGroupPks); - alerts.forEach(async (alert) => { - const marginAccountPk = new PublicKey(alert.marginAccountPk); - const marginAccount = await client.getMarginAccount(connection, marginAccountPk, dexProgramId); - const collateralRatio = marginAccount.getCollateralRatio(mangoGroups[alert.mangoGroupPk]['mangoGroup'], mangoGroups[alert.mangoGroupPk]['prices']); - if (collateralRatio <= alert.collateralRatioThresh) { - let message = MESSAGE.replace('@ratio@', alert.collateralRatioThresh); - message += marginAccount.toPrettyString( - mangoGroups[alert.mangoGroupPk]['mangoGroup'], - mangoGroups[alert.mangoGroupPk]['prices'] - ); - message += '\nVisit https://trade.mango.markets/' - const alertSent = sendAlert(alert, message); - if (alertSent) { - db.collection('alerts').updateOne({ _id: new ObjectId(alert._id) }, { '$set': { open: false } }); +const runCron = async () => { + const mongoConnection = await MongoClient.connect(config.dbConnectionString, { useUnifiedTopology: true }); + const db = mongoConnection.db(config.db); + cron.schedule("1 * * * *", async () => { + try { + const alerts = await db.collection('alerts').find({open: true}).toArray(); + const uniqueMangoGroupPks: string[] = [...new Set(alerts.map(alert => alert.mangoGroupPk))]; + const mangoGroups:any = await reduceMangoGroups(client, connection, uniqueMangoGroupPks); + alerts.forEach(async (alert) => { + const marginAccountPk = new PublicKey(alert.marginAccountPk); + const marginAccount = await client.getMarginAccount(connection, marginAccountPk, dexProgramId); + const collateralRatio = marginAccount.getCollateralRatio(mangoGroups[alert.mangoGroupPk]['mangoGroup'], mangoGroups[alert.mangoGroupPk]['prices']); + if (collateralRatio <= alert.collateralRatioThresh) { + let message = MESSAGE.replace('@ratio@', alert.collateralRatioThresh); + message += marginAccount.toPrettyString( + mangoGroups[alert.mangoGroupPk]['mangoGroup'], + mangoGroups[alert.mangoGroupPk]['prices'] + ); + message += '\nVisit https://trade.mango.markets/' + const alertSent = sendAlert(alert, message); + if (alertSent) { + db.collection('alerts').updateOne({ _id: new ObjectId(alert._id) }, { '$set': { open: false } }); + } } - } - }); - const expiryTime = new Date( Date.now() - (1000 * 60 * 1) ); // 15 Minutes - console.log(expiryTime); - db.collection('alerts').deleteMany({ tgChatId: { $exists: false }, timestamp: { '$lt': expiryTime } }); - } catch (e) { - sendLogsToDiscord(null, e); - } -}); + }); + const expiryTime = new Date( Date.now() - (1000 * 60 * 1) ); // 15 Minutes + console.log(expiryTime); + db.collection('alerts').deleteMany({ tgChatId: { $exists: false }, timestamp: { '$lt': expiryTime } }); + } catch (e) { + sendLogsToDiscord(null, e); + } + }); +} + +runCron();