38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const node_cron_1 = __importDefault(require("node-cron"));
|
|
const providers_1 = require("./services/providers");
|
|
const db_1 = require("./services/db");
|
|
const ntfy_1 = require("./services/ntfy");
|
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
dotenv_1.default.config();
|
|
const ALERT_THRESHOLD = parseInt(process.env.ALERT_THRESHOLD_PERCENT || '80');
|
|
node_cron_1.default.schedule('*/30 * * * *', async () => {
|
|
console.log('Running quota check cron job...');
|
|
try {
|
|
const quotas = await (0, providers_1.fetchAllQuotas)();
|
|
for (const quota of quotas) {
|
|
// Save to DB
|
|
await (0, db_1.insertQuota)(quota);
|
|
// Send alert if threshold exceeded
|
|
if (quota.provider.toLowerCase() === 'poe') {
|
|
if (quota.percentage <= (100 - ALERT_THRESHOLD)) {
|
|
await (0, ntfy_1.sendNtfyAlert)(quota.provider, quota.percentage);
|
|
}
|
|
}
|
|
else {
|
|
if (quota.percentage >= ALERT_THRESHOLD) {
|
|
await (0, ntfy_1.sendNtfyAlert)(quota.provider, quota.percentage);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error('Error in cron job:', error);
|
|
}
|
|
});
|
|
console.log('Cron jobs scheduled');
|
|
//# sourceMappingURL=cron.js.map
|