Add chat health cron checks
This commit is contained in:
@@ -10,3 +10,9 @@ APOLLO_BACKEND_GRAPHQL_URL=http://apollo-backend:4000/graphql
|
|||||||
HATCHET_CLIENT_TOKEN=
|
HATCHET_CLIENT_TOKEN=
|
||||||
HATCHET_CLIENT_HOST_PORT=fregat-hatchet-engine:7070
|
HATCHET_CLIENT_HOST_PORT=fregat-hatchet-engine:7070
|
||||||
HATCHET_WORKER_NAME=fregat-hatchet-worker
|
HATCHET_WORKER_NAME=fregat-hatchet-worker
|
||||||
|
|
||||||
|
CHAT_CRON_ENABLED=1
|
||||||
|
CHAT_CRON_INTERVAL_SEC=300
|
||||||
|
|
||||||
|
TELEGRAM_BOT_TOKEN=
|
||||||
|
MAX_BOT_WEBHOOK_URL=
|
||||||
|
|||||||
52
src/chat-health-cron.js
Normal file
52
src/chat-health-cron.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
function isEnabled(value) {
|
||||||
|
return ['1', 'true', 'yes', 'on'].includes(String(value || '').toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkBackendHealth() {
|
||||||
|
const url = process.env.APOLLO_BACKEND_GRAPHQL_URL;
|
||||||
|
if (!url) {
|
||||||
|
return 'backend_url_missing';
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json' },
|
||||||
|
body: JSON.stringify({ query: 'query Healthcheck { healthcheck }' }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return `backend_http_${response.status}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = await response.json();
|
||||||
|
if (payload.errors?.length) {
|
||||||
|
return `backend_graphql_error:${payload.errors[0].message}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'backend_ok';
|
||||||
|
} catch (error) {
|
||||||
|
return `backend_unreachable:${error.message}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runHealthTick() {
|
||||||
|
const telegram = process.env.TELEGRAM_BOT_TOKEN ? 'configured' : 'missing';
|
||||||
|
const max = process.env.MAX_BOT_WEBHOOK_URL ? 'configured' : 'missing';
|
||||||
|
const backend = await checkBackendHealth();
|
||||||
|
|
||||||
|
console.log(`[chat-cron] telegram=${telegram} max=${max} backend=${backend}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startChatHealthCron() {
|
||||||
|
if (!isEnabled(process.env.CHAT_CRON_ENABLED ?? '1')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const intervalSec = Number(process.env.CHAT_CRON_INTERVAL_SEC ?? 300);
|
||||||
|
const safeIntervalSec = Number.isFinite(intervalSec) && intervalSec > 0 ? intervalSec : 300;
|
||||||
|
|
||||||
|
runHealthTick();
|
||||||
|
const timer = setInterval(runHealthTick, safeIntervalSec * 1000);
|
||||||
|
timer.unref();
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
|
|
||||||
|
import { startChatHealthCron } from './chat-health-cron.js';
|
||||||
import { hatchet } from './hatchet-client.js';
|
import { hatchet } from './hatchet-client.js';
|
||||||
import { orderStatusNotifier } from './workflows/order-status-notifier.js';
|
import { orderStatusNotifier } from './workflows/order-status-notifier.js';
|
||||||
import { referralBalanceSync } from './workflows/referral-balance-sync.js';
|
import { referralBalanceSync } from './workflows/referral-balance-sync.js';
|
||||||
@@ -7,6 +8,8 @@ import { referralBalanceSync } from './workflows/referral-balance-sync.js';
|
|||||||
const workerName = process.env.HATCHET_WORKER_NAME || 'fregat-hatchet-worker';
|
const workerName = process.env.HATCHET_WORKER_NAME || 'fregat-hatchet-worker';
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
|
startChatHealthCron();
|
||||||
|
|
||||||
const worker = await hatchet.worker(workerName, {
|
const worker = await hatchet.worker(workerName, {
|
||||||
workflows: [orderStatusNotifier, referralBalanceSync],
|
workflows: [orderStatusNotifier, referralBalanceSync],
|
||||||
slots: 10,
|
slots: 10,
|
||||||
|
|||||||
Reference in New Issue
Block a user