-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2436 from airqo-platform/staging
move to production
- Loading branch information
Showing
14 changed files
with
105 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const cron = require("node-cron"); | ||
const SimModel = require("@models/Sim"); | ||
const constants = require("@config/constants"); | ||
const log4js = require("log4js"); | ||
const logger = log4js.getLogger( | ||
`${constants.ENVIRONMENT} -- bin/cronJob script` | ||
); | ||
const checkStatus = require("@utils/create-sim").checkStatus; | ||
const secondsDelayBetweenRequests = 20000; | ||
const internetDataBalanceThreshold = 5; | ||
|
||
// Everyday at midnight | ||
cron.schedule("0 0 * * *", async () => { | ||
try { | ||
const batchSize = 100; // Process 100 SIM cards at a time | ||
let skip = 0; | ||
|
||
const simCards = await SimModel("airqo").find({}).select("_id").lean(); | ||
|
||
while (true) { | ||
const simBatch = simCards.slice(skip, skip + batchSize); | ||
|
||
if (simBatch.length === 0) { | ||
break; | ||
} | ||
|
||
await processSimCardsWithDelay(simBatch); | ||
|
||
skip += batchSize; | ||
} | ||
} catch (error) { | ||
logger.error( | ||
`An error occurred in the cron job --- ${JSON.stringify(error)}` | ||
); | ||
} | ||
}); | ||
|
||
async function processSimCardsWithDelay(simBatch) { | ||
for (const sim of simBatch) { | ||
const responseFromCheckStatus = await checkStatus({ | ||
query: { tenant: "airqo" }, | ||
params: { sim_id: sim._id }, | ||
}); | ||
|
||
// Check if data.balance is less than the declared threshold and log it | ||
if ( | ||
responseFromCheckStatus.success && | ||
responseFromCheckStatus.data.balance < internetDataBalanceThreshold | ||
) { | ||
logger.info( | ||
`SIM card ${sim._id} has a balance less than ${internetDataBalanceThreshold}` | ||
); | ||
} | ||
await new Promise((resolve) => | ||
setTimeout(resolve, secondsDelayBetweenRequests) | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters