@penkov/tasks_queue
Version:
A lightweight PostgreSQL-backed task queue system with scheduling, retries, backoff strategies, and priority handling. Designed for efficiency and observability in modern Node.js applications.
58 lines (57 loc) • 1.79 kB
JavaScript
import log4js from "log4js";
import { TaskStatus } from "./tasks-model.js";
import { TimeUtils } from "./time-utils.js";
const logger = log4js.getLogger("TasksAuxiliaryWorker");
export class TasksAuxiliaryWorker {
tasksQueueDao;
workerTimer = null;
constructor(tasksQueueDao) {
this.tasksQueueDao = tasksQueueDao;
}
start() {
try {
const run = () => {
this.runAuxiliaryJobs();
};
this.workerTimer = setInterval(() => run(), TimeUtils.second * 30);
run();
}
catch (e) {
logger.warn("Failed to process stalled tasks", e);
}
}
runAuxiliaryJobs() {
try {
this.tasksQueueDao
.failStalled()
.then((res) => {
if (res.nonEmpty) {
logger.info(`Marked stalled as failed: ${res.mkString(", ")}`);
}
})
.catch((e) => {
logger.warn("Failed to process stalled tasks", e);
});
this.tasksQueueDao.resetFailed().catch((e) => {
logger.warn("Failed to reset failed tasks", e);
});
this.tasksQueueDao.clearFinished().catch((e) => {
logger.warn("Failed to clear finished tasks", e);
});
}
catch (e) {
logger.warn("Failed to process stalled tasks", e);
}
}
async stop() {
if (this.workerTimer) {
clearTimeout(this.workerTimer);
}
}
pendingCount(queue) {
return this.tasksQueueDao.statusCount(queue, TaskStatus.pending);
}
inProgressCount(queue) {
return this.tasksQueueDao.statusCount(queue, TaskStatus.in_progress);
}
}