52 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-04-08 10:01:19 -07:00
import Fastify from "fastify";
import { config } from "./config.ts";
import { runWorkerOnce } from "./processor.ts";
const app = Fastify({
logger: true
});
let lastRun: { processed: boolean; assetId?: string; error?: string } | null = null;
app.get("/health", async () => ({
status: "ok",
service: "worker",
lastRun
}));
app.post("/run-once", async () => {
lastRun = await runWorkerOnce();
return lastRun;
});
const interval = setInterval(() => {
void runWorkerOnce()
.then((result) => {
lastRun = result;
if (result.processed) {
app.log.info({ assetId: result.assetId }, "Processed queued asset.");
}
})
.catch((error) => {
app.log.error(error);
lastRun = {
processed: false,
error: error instanceof Error ? error.message : "Unknown worker error."
};
});
}, config.pollIntervalMs);
process.on("SIGINT", () => clearInterval(interval));
process.on("SIGTERM", () => clearInterval(interval));
try {
await app.listen({
port: config.port,
host: config.host
});
} catch (error) {
app.log.error(error);
clearInterval(interval);
process.exit(1);
}