stack/apps/backend/scripts/run-email-queue.ts
BilalG1 609579abab
Some checks failed
all-good: Did all the other checks pass? / all-good (push) Has been cancelled
Ensure Prisma migrations are in sync with the schema / check_prisma_migrations (22.x) (push) Has been cancelled
DB migration compat / Check if migrations changed (push) Has been cancelled
Docker Server Build and Push / Docker Build and Push Server (push) Has been cancelled
Docker Server Build and Run / docker (push) Has been cancelled
Runs E2E API Tests (Local Emulator) / E2E Tests (Local Emulator, Node ${{ matrix.node-version }}) (22.x) (push) Has been cancelled
Runs E2E API Tests / E2E Tests (Node ${{ matrix.node-version }}, Freestyle ${{ matrix.freestyle-mode }}) (mock, 22.x) (push) Has been cancelled
Runs E2E API Tests / E2E Tests (Node ${{ matrix.node-version }}, Freestyle ${{ matrix.freestyle-mode }}) (prod, 22.x) (push) Has been cancelled
Runs E2E API Tests with custom port prefix / build (22.x) (push) Has been cancelled
Runs E2E Fallback Tests / E2E Fallback Tests (Node ${{ matrix.node-version }}) (22.x) (push) Has been cancelled
Lint & build / lint_and_build (24) (push) Has been cancelled
TOC Generator / TOC Generator (push) Has been cancelled
DB migration compat / Back-compat — Current branch migrations with ${{ needs.check-migrations-changed.outputs.base_branch }} branch code (push) Has been cancelled
DB migration compat / Forward-compat — Current branch code with ${{ needs.check-migrations-changed.outputs.base_branch }} branch migrations (push) Has been cancelled
DB migration compat / No migration changes (skipped) (push) Has been cancelled
feat(hexclave): PR 3 — native @hexclave/* source rename + delete dual-publish wiring (#1482)
2026-05-29 15:21:59 -07:00

48 lines
1.8 KiB
TypeScript

import { getEnvVariable } from "@hexclave/shared/dist/utils/env";
import { HexclaveAssertionError } from "@hexclave/shared/dist/utils/errors";
import { runAsynchronously, wait } from "@hexclave/shared/dist/utils/promises";
async function main() {
console.log("Starting email queue processor...");
const cronSecret = getEnvVariable('CRON_SECRET');
const baseUrl = `http://localhost:${getEnvVariable('NEXT_PUBLIC_HEXCLAVE_PORT_PREFIX', '81')}02`;
// Wait a few seconds to make sure the server is fully started
await wait(5_000);
const run = () => runAsynchronously(async () => {
// If a the server is restarted, then the existing email queue step may be cancelled prematurely. That's why we
// have an extra loop here to detect and restart the email queue step if it completes too quickly.
const startTime = performance.now();
while (true) {
console.log("Running email queue step...");
const res = await fetch(`${baseUrl}/api/latest/internal/email-queue-step`, {
method: "GET",
headers: { 'Authorization': `Bearer ${cronSecret}` },
});
if (!res.ok) throw new HexclaveAssertionError(`Failed to call email queue step: ${res.status} ${res.statusText}\n${await res.text()}`, { res });
console.log("Email queue step completed.");
const endTime = performance.now();
if (endTime - startTime < 58_000) {
console.log(`Detected a server restart before email queue step completed (after ${endTime - startTime}ms). Restarting email queue step now...`);
await wait(1_000);
} else {
break;
}
}
});
setInterval(() => {
run();
}, 60000);
run();
}
// eslint-disable-next-line no-restricted-syntax
main().catch((err) => {
console.error(err);
process.exit(1);
});