Last update

This commit is contained in:
Vítor Vieira 2025-11-28 20:33:15 +00:00
parent 08afa6452f
commit 565c2ffe26
4 changed files with 6 additions and 11 deletions

View file

@ -1,4 +1,3 @@
import fs from "node:fs";
import { parseArgs } from "node:util"; import { parseArgs } from "node:util";
import { prefetchRates } from "./src/api.js"; import { prefetchRates } from "./src/api.js";
import { Bot } from "./src/bot.js"; import { Bot } from "./src/bot.js";
@ -46,7 +45,7 @@ const interval = parseInt(values.interval, 10);
const threshold = parseFloat(values.threshold); const threshold = parseFloat(values.threshold);
if (isNaN(interval) || interval < 100) { if (isNaN(interval) || interval < 100) {
logger.error("Interval must be a number larger than 100ms"); logger.error("Interval must be a number larger than 100");
process.exit(1); process.exit(1);
} }
if (pairs.length === 0 || pairs.some((p) => !p.match(/^[A-Z]+-?[A-Z]+$/))) { if (pairs.length === 0 || pairs.some((p) => !p.match(/^[A-Z]+-?[A-Z]+$/))) {
@ -70,12 +69,10 @@ async function main() {
try { try {
await prefetchRates(pairs); await prefetchRates(pairs);
} catch (err) { } catch (err) {
logger.error(err, "Critical failure during cache warming"); logger.error(err, "Critical failure during cache population");
process.exit(1); process.exit(1);
} }
fs.promises.writeFile("/tmp/healthy", "ok");
const handleAlert = async (alertData) => { const handleAlert = async (alertData) => {
await insertIntoDB(alertData); await insertIntoDB(alertData);
}; };

View file

@ -50,11 +50,10 @@ export async function fetchRate(pair) {
} }
export async function prefetchRates(pairs) { export async function prefetchRates(pairs) {
logger.info({ pairs }, "Verifying pairs...");
const results = await Promise.allSettled(pairs.map((p) => fetchRate(p))); const results = await Promise.allSettled(pairs.map((p) => fetchRate(p)));
const failed = results.filter((r) => r.status === "rejected"); const failed = results.filter((r) => r.status === "rejected");
if (failed.length > 0) { if (failed.length > 0) {
logger.warn(`[API] ${failed.length} pairs failed initial check.`); logger.warn(`[API] ${failed.length} pairs failed initial check`);
} }
} }

View file

@ -24,7 +24,7 @@ export function getPool() {
}); });
pool.on("error", (err) => { pool.on("error", (err) => {
logger.error(err, "[DB] Unexpected error on idle client", err); logger.error(err, "[DB] Unexpected error on client", err);
}); });
return pool; return pool;
@ -59,7 +59,7 @@ export async function initDB() {
try { try {
client = await currentPool.connect(); client = await currentPool.connect();
await client.query(CREATE_TABLE_QUERY); await client.query(CREATE_TABLE_QUERY);
logger.info("[DB] Database initialized and connected."); logger.info("[DB] Database initialized and connected");
return; return;
} catch (err) { } catch (err) {
if (i === maxRetries - 1) { if (i === maxRetries - 1) {
@ -106,7 +106,7 @@ export async function insertIntoDB(data) {
await getPool().query(query, values); await getPool().query(query, values);
logger.info(`[DB] Event saved for ${data.pair}`); logger.info(`[DB] Event saved for ${data.pair}`);
} catch (err) { } catch (err) {
logger.error(`[DB] Failed to save alert: ${err.message}`); logger.error(`[DB] Failed to save alert ${err.message}`);
throw err; throw err;
} }
} }

View file

@ -128,7 +128,6 @@ describe("API", () => {
vi.resetModules(); vi.resetModules();
nock.cleanAll(); nock.cleanAll();
// Import the actual module for these tests
vi.doMock("../src/api.js", async () => { vi.doMock("../src/api.js", async () => {
return await vi.importActual("../src/api.js"); return await vi.importActual("../src/api.js");
}); });