mirror of
https://github.com/outline/outline.git
synced 2026-06-13 03:14:59 +03:00
bf45e97641
* Update types * fix circular dep * type imports * lint type imports and --fix
48 lines
1.2 KiB
TypeScript
48 lines
1.2 KiB
TypeScript
import type { Queue } from "bull";
|
|
import { Second } from "@shared/utils/time";
|
|
import Logger from "@server/logging/Logger";
|
|
|
|
/* oxlint-disable @typescript-eslint/no-misused-promises */
|
|
export default class HealthMonitor {
|
|
/**
|
|
* Starts a health monitor for the given queue. If the queue stops processing jobs then the
|
|
* process is exit.
|
|
*
|
|
* @param queue The queue to monitor
|
|
*/
|
|
public static start(queue: Queue) {
|
|
let lastActivityTime = Date.now();
|
|
|
|
queue.on("active", () => {
|
|
lastActivityTime = Date.now();
|
|
});
|
|
queue.on("completed", () => {
|
|
lastActivityTime = Date.now();
|
|
});
|
|
queue.on("failed", () => {
|
|
lastActivityTime = Date.now();
|
|
});
|
|
|
|
setInterval(async () => {
|
|
const timeSinceActivity = Date.now() - lastActivityTime;
|
|
|
|
// If there's been recent activity, the queue is healthy
|
|
if (timeSinceActivity < 30 * Second.ms) {
|
|
return;
|
|
}
|
|
|
|
const waiting = await queue.getWaitingCount();
|
|
if (waiting > 50) {
|
|
Logger.fatal(
|
|
"Queue has stopped processing jobs",
|
|
new Error(`Jobs are waiting in the ${queue.name} queue`),
|
|
{
|
|
queue: queue.name,
|
|
waiting,
|
|
}
|
|
);
|
|
}
|
|
}, 30 * Second.ms);
|
|
}
|
|
}
|