reorg jobqueue to worker

This commit is contained in:
Alex Holliday committed 2026-06-10 09:30:14 -07:00
1 parent c8c4bac2c7
commit 2bf3c08f3c
14 files changed
+77 -77

No files matched your search

@@ -1,4 +1,4 @@
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js"; import { IWorker } from "@/worker/worker.interface.js";
import { getQueueJobsQueryValidation } from "@/api/validation/queueValidation.js"; import { getQueueJobsQueryValidation } from "@/api/validation/queueValidation.js";
import { Request, Response, NextFunction } from "express"; import { Request, Response, NextFunction } from "express";
@@ -13,9 +13,9 @@ export interface IJobQueueController {
class JobQueueController implements IJobQueueController { class JobQueueController implements IJobQueueController {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private jobQueue: IJobQueue; private worker: IWorker;
constructor(jobQueue: IJobQueue) { constructor(worker: IWorker) {
this.jobQueue = jobQueue; this.worker = worker;
} }
get serviceName() { get serviceName() {
@@ -24,7 +24,7 @@ class JobQueueController implements IJobQueueController {
getMetrics = async (req: Request, res: Response, next: NextFunction) => { getMetrics = async (req: Request, res: Response, next: NextFunction) => {
try { try {
const metrics = await this.jobQueue.getMetrics(); const metrics = await this.worker.getMetrics();
res.status(200).json({ res.status(200).json({
success: true, success: true,
msg: "Queue metrics fetched successfully", msg: "Queue metrics fetched successfully",
@@ -38,7 +38,7 @@ class JobQueueController implements IJobQueueController {
getJobs = async (req: Request, res: Response, next: NextFunction) => { getJobs = async (req: Request, res: Response, next: NextFunction) => {
try { try {
const pagination = getQueueJobsQueryValidation.parse(req.query); const pagination = getQueueJobsQueryValidation.parse(req.query);
const { jobs, count } = await this.jobQueue.getJobs(pagination); const { jobs, count } = await this.worker.getJobs(pagination);
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
msg: "Queue jobs fetched successfully", msg: "Queue jobs fetched successfully",
@@ -52,8 +52,8 @@ class JobQueueController implements IJobQueueController {
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => { getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
try { try {
const pagination = getQueueJobsQueryValidation.parse(req.query); const pagination = getQueueJobsQueryValidation.parse(req.query);
const { jobs, count } = await this.jobQueue.getJobs(pagination); const { jobs, count } = await this.worker.getJobs(pagination);
const metrics = await this.jobQueue.getMetrics(); const metrics = await this.worker.getMetrics();
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
msg: "Queue metrics fetched successfully", msg: "Queue metrics fetched successfully",
@@ -66,7 +66,7 @@ class JobQueueController implements IJobQueueController {
flushQueue = async (req: Request, res: Response, next: NextFunction) => { flushQueue = async (req: Request, res: Response, next: NextFunction) => {
try { try {
const result = await this.jobQueue.flushQueues(); const result = await this.worker.flushQueues();
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
msg: "Queue flushed successfully", msg: "Queue flushed successfully",
+1 -1
View File
@@ -39,7 +39,7 @@ export const initializeControllers = (services: InitializedServices): Initialize
geoCheckController: new GeoCheckController(services.geoChecksService), geoCheckController: new GeoCheckController(services.geoChecksService),
inviteController: new InviteController(services.inviteService), inviteController: new InviteController(services.inviteService),
maintenanceWindowController: new MaintenanceWindowController(services.maintenanceWindowService), maintenanceWindowController: new MaintenanceWindowController(services.maintenanceWindowService),
queueController: new QueueController(services.jobQueue), queueController: new QueueController(services.worker),
logController: new LogController(services.logger), logController: new LogController(services.logger),
statusPageController: new StatusPageController(services.statusPageService, services.monitorsRepository, services.settingsService), statusPageController: new StatusPageController(services.statusPageService, services.monitorsRepository, services.settingsService),
notificationController: new NotificationController(services.notificationsService, services.monitorsRepository), notificationController: new NotificationController(services.notificationsService, services.monitorsRepository),
+11 -11
View File
@@ -28,10 +28,10 @@ import { IUserService, UserService } from "@/domain/users/user.service.js";
import { BufferService, IBufferService } from "@/service/bufferService.js"; import { BufferService, IBufferService } from "@/service/bufferService.js";
import { EmailService, IEmailService } from "@/service/emailService.js"; import { EmailService, IEmailService } from "@/service/emailService.js";
import { GlobalPingService } from "@/service/globalPingService.js"; import { GlobalPingService } from "@/service/globalPingService.js";
import { QueueHelper } from "@/service/job-queues/job-queue.helper.js"; import { QueueHelper } from "@/worker/worker.helper.js";
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js"; import { IWorker } from "@/worker/worker.interface.js";
import { LessSimpleQueue } from "@/service/job-queues/job-queue.less-simple.js"; import { LessSimpleWorker } from "@/worker/worker.less-simple.js";
import { SuperSimpleQueue } from "@/service/job-queues/job-queue.sumper-simple.js"; import { SuperSimpleQueue } from "@/worker/worker.super-simple.js";
import { INetworkService, NetworkService } from "@/service/networkService.js"; import { INetworkService, NetworkService } from "@/service/networkService.js";
import { IStatusService, StatusService } from "@/service/statusService.js"; import { IStatusService, StatusService } from "@/service/statusService.js";
@@ -110,7 +110,7 @@ export type InitializedServices = {
emailService: IEmailService; emailService: IEmailService;
bufferService: IBufferService; bufferService: IBufferService;
statusService: IStatusService; statusService: IStatusService;
jobQueue: IJobQueue; worker: IWorker;
userService: IUserService; userService: IUserService;
checkService: ICheckService; checkService: ICheckService;
geoChecksService: IGeoChecksService; geoChecksService: IGeoChecksService;
@@ -337,13 +337,13 @@ export const initializeServices = async ({
}); });
} }
let jobQueue: IJobQueue; let worker: IWorker;
switch (envSettings.queueType) { switch (envSettings.queueType) {
case "lessSimpleQueue": case "lessSimpleQueue":
jobQueue = await LessSimpleQueue.create(logger, queueHelper, monitorsRepository, queueWorkersRepository, envSettings, queueMode); worker = await LessSimpleWorker.create(logger, queueHelper, monitorsRepository, queueWorkersRepository, envSettings, queueMode);
break; break;
default: default:
jobQueue = await SuperSimpleQueue.create(logger, queueHelper, monitorsRepository); worker = await SuperSimpleQueue.create(logger, queueHelper, monitorsRepository);
} }
// Business services // Business services
@@ -353,7 +353,7 @@ export const initializeServices = async ({
settingsService, settingsService,
logger, logger,
jwt, jwt,
jobQueue, worker,
monitorsRepository, monitorsRepository,
usersRepository, usersRepository,
invitesRepository, invitesRepository,
@@ -373,7 +373,7 @@ export const initializeServices = async ({
maintenanceWindowsRepository, maintenanceWindowsRepository,
}); });
const monitorService = new MonitorService({ const monitorService = new MonitorService({
jobQueue, worker,
logger, logger,
games, games,
monitorsRepository, monitorsRepository,
@@ -393,7 +393,7 @@ export const initializeServices = async ({
emailService, emailService,
bufferService, bufferService,
statusService, statusService,
jobQueue, worker,
userService, userService,
checkService, checkService,
geoChecksService, geoChecksService,
@@ -8,7 +8,7 @@ import type { IMonitorsRepository } from "@/domain/monitors/monitor.repository.i
import type { IUsersRepository } from "@/domain/users/user.repository.interface.js"; import type { IUsersRepository } from "@/domain/users/user.repository.interface.js";
import type { Incident, IncidentSummary } from "@/domain/incidents/incident.type.js"; import type { Incident, IncidentSummary } from "@/domain/incidents/incident.type.js";
import type { User } from "@/domain/users/user.type.js"; import type { User } from "@/domain/users/user.type.js";
import type { MonitorActionDecision } from "@/service/job-queues/job-queue.helper.js"; import type { MonitorActionDecision } from "@/worker/worker.helper.js";
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js"; import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
import type { ILogger } from "@/utils/logger.js"; import type { ILogger } from "@/utils/logger.js";
+16 -16
View File
@@ -21,7 +21,7 @@ import type { IStatusPagesRepository } from "@/domain/status-pages/status-page-r
import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" }; import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" };
import { AppError } from "@/utils/AppError.js"; import { AppError } from "@/utils/AppError.js";
import type { ImportedMonitor } from "@/api/validation/monitorValidation.js"; import type { ImportedMonitor } from "@/api/validation/monitorValidation.js";
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js"; import { IWorker } from "@/worker/worker.interface.js";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
const SERVICE_NAME = "MonitorService"; const SERVICE_NAME = "MonitorService";
@@ -94,7 +94,7 @@ export interface IMonitorService {
export class MonitorService implements IMonitorService { export class MonitorService implements IMonitorService {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private jobQueue: IJobQueue; private worker: IWorker;
private logger: ILogger; private logger: ILogger;
private games: GamesMap; private games: GamesMap;
private monitorsRepository: IMonitorsRepository; private monitorsRepository: IMonitorsRepository;
@@ -105,7 +105,7 @@ export class MonitorService implements IMonitorService {
private incidentsRepository: IIncidentsRepository; private incidentsRepository: IIncidentsRepository;
constructor({ constructor({
jobQueue, worker,
logger, logger,
games, games,
monitorsRepository, monitorsRepository,
@@ -115,7 +115,7 @@ export class MonitorService implements IMonitorService {
statusPagesRepository, statusPagesRepository,
incidentsRepository, incidentsRepository,
}: { }: {
jobQueue: IJobQueue; worker: IWorker;
logger: ILogger; logger: ILogger;
games: GamesMap; games: GamesMap;
monitorsRepository: IMonitorsRepository; monitorsRepository: IMonitorsRepository;
@@ -125,7 +125,7 @@ export class MonitorService implements IMonitorService {
statusPagesRepository: IStatusPagesRepository; statusPagesRepository: IStatusPagesRepository;
incidentsRepository: IIncidentsRepository; incidentsRepository: IIncidentsRepository;
}) { }) {
this.jobQueue = jobQueue; this.worker = worker;
this.logger = logger; this.logger = logger;
this.games = games; this.games = games;
this.monitorsRepository = monitorsRepository; this.monitorsRepository = monitorsRepository;
@@ -171,7 +171,7 @@ export class MonitorService implements IMonitorService {
throw new AppError({ message: "Failed to create monitor", status: 500, service: SERVICE_NAME, method: "createMonitor" }); throw new AppError({ message: "Failed to create monitor", status: 500, service: SERVICE_NAME, method: "createMonitor" });
} }
this.jobQueue.addJob(monitor.id, monitor); this.worker.addJob(monitor.id, monitor);
}; };
createMonitors = async (monitors: Array<Monitor>): Promise<Monitor[] | null> => { createMonitors = async (monitors: Array<Monitor>): Promise<Monitor[] | null> => {
@@ -180,7 +180,7 @@ export class MonitorService implements IMonitorService {
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" }); throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" });
} }
await Promise.all(createdMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor))); await Promise.all(createdMonitors.map((monitor) => this.worker.addJob(monitor.id, monitor)));
return createdMonitors; return createdMonitors;
}; };
@@ -196,7 +196,7 @@ export class MonitorService implements IMonitorService {
})); }));
const demoMonitors = await this.monitorsRepository.createMonitors(monitors as unknown as Monitor[]); const demoMonitors = await this.monitorsRepository.createMonitors(monitors as unknown as Monitor[]);
await Promise.all(demoMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor))); await Promise.all(demoMonitors.map((monitor) => this.worker.addJob(monitor.id, monitor)));
return demoMonitors; return demoMonitors;
}; };
@@ -428,7 +428,7 @@ export class MonitorService implements IMonitorService {
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Partial<Monitor> }) => { editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Partial<Monitor> }) => {
const editedMonitor = await this.monitorsRepository.updateById(monitorId, teamId, body); const editedMonitor = await this.monitorsRepository.updateById(monitorId, teamId, body);
await this.jobQueue.updateJob(editedMonitor); await this.worker.updateJob(editedMonitor);
return editedMonitor; return editedMonitor;
}; };
@@ -448,7 +448,7 @@ export class MonitorService implements IMonitorService {
// If notifications were updated, we should update the jobs in the queue // If notifications were updated, we should update the jobs in the queue
if (modifiedCount > 0) { if (modifiedCount > 0) {
const monitors = await this.monitorsRepository.findByIds(monitorIds); const monitors = await this.monitorsRepository.findByIds(monitorIds);
await Promise.all(monitors.map((monitor) => this.jobQueue.updateJob(monitor))); await Promise.all(monitors.map((monitor) => this.worker.updateJob(monitor)));
} }
return modifiedCount; return modifiedCount;
@@ -457,9 +457,9 @@ export class MonitorService implements IMonitorService {
pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => { pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId); const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId);
if (monitor.isActive) { if (monitor.isActive) {
await this.jobQueue.resumeJob(monitor); await this.worker.resumeJob(monitor);
} else { } else {
await this.jobQueue.pauseJob(monitor); await this.worker.pauseJob(monitor);
} }
return monitor; return monitor;
}; };
@@ -478,9 +478,9 @@ export class MonitorService implements IMonitorService {
const results = await Promise.allSettled( const results = await Promise.allSettled(
monitors.map(async (monitor) => { monitors.map(async (monitor) => {
if (monitor.isActive) { if (monitor.isActive) {
await this.jobQueue.resumeJob(monitor); await this.worker.resumeJob(monitor);
} else { } else {
await this.jobQueue.pauseJob(monitor); await this.worker.pauseJob(monitor);
} }
}) })
); );
@@ -541,7 +541,7 @@ export class MonitorService implements IMonitorService {
}); });
}); });
await this.jobQueue.deleteJob(monitor); await this.worker.deleteJob(monitor);
return monitor; return monitor;
}; };
@@ -550,7 +550,7 @@ export class MonitorService implements IMonitorService {
await Promise.all( await Promise.all(
monitors.map(async (monitor) => { monitors.map(async (monitor) => {
try { try {
await this.jobQueue.deleteJob(monitor); await this.worker.deleteJob(monitor);
await this.checksRepository.deleteByMonitorId(monitor.id); await this.checksRepository.deleteByMonitorId(monitor.id);
await this.geoChecksRepository.deleteByMonitorId(monitor.id); await this.geoChecksRepository.deleteByMonitorId(monitor.id);
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id); await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id);
@@ -1,6 +1,6 @@
import type { Monitor } from "@/domain/monitors/monitor.types.js"; import type { Monitor } from "@/domain/monitors/monitor.types.js";
import type { HardwareStatusPayload, MonitorStatusResponse } from "@/types/network.js"; import type { HardwareStatusPayload, MonitorStatusResponse } from "@/types/network.js";
import type { MonitorActionDecision } from "@/service/job-queues/job-queue.helper.js"; import type { MonitorActionDecision } from "@/worker/worker.helper.js";
import type { import type {
NotificationMessage, NotificationMessage,
NotificationType, NotificationType,
@@ -5,7 +5,7 @@ import type { NotificationMessage } from "@/domain/notifications/notification.ty
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js"; import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js"; import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js";
import { INotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js"; import { INotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
import type { MonitorActionDecision } from "@/service/job-queues/job-queue.helper.js"; import type { MonitorActionDecision } from "@/worker/worker.helper.js";
import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js"; import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js"; import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
+6 -6
View File
@@ -8,12 +8,12 @@ import type { User } from "@/domain/users/user.type.js";
import { canManageRole, type UserRole } from "@/domain/users/user.type.js"; import { canManageRole, type UserRole } from "@/domain/users/user.type.js";
import bcrypt from "bcryptjs"; import bcrypt from "bcryptjs";
import { AppError } from "@/utils/AppError.js"; import { AppError } from "@/utils/AppError.js";
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js";
import { IEmailService } from "@/service/emailService.js"; import { IEmailService } from "@/service/emailService.js";
import { EnvConfig, ISettingsService } from "@/domain/app-settings/app-settings.service.js"; import { EnvConfig, ISettingsService } from "@/domain/app-settings/app-settings.service.js";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import crypto from "crypto"; import crypto from "crypto";
import { IWorker } from "@/worker/worker.interface.js";
type CryptoType = typeof crypto; type CryptoType = typeof crypto;
type JwtType = typeof jwt; type JwtType = typeof jwt;
const SERVICE_NAME = "userService"; const SERVICE_NAME = "userService";
@@ -53,7 +53,7 @@ export class UserService implements IUserService {
private settingsService: ISettingsService; private settingsService: ISettingsService;
private logger: ILogger; private logger: ILogger;
private jwt: JwtType; private jwt: JwtType;
private jobQueue: IJobQueue; private worker: IWorker;
private crypto: CryptoType; private crypto: CryptoType;
private monitorsRepository: IMonitorsRepository; private monitorsRepository: IMonitorsRepository;
private usersRepository: IUsersRepository; private usersRepository: IUsersRepository;
@@ -68,7 +68,7 @@ export class UserService implements IUserService {
settingsService, settingsService,
logger, logger,
jwt, jwt,
jobQueue, worker,
monitorsRepository, monitorsRepository,
usersRepository, usersRepository,
invitesRepository, invitesRepository,
@@ -81,7 +81,7 @@ export class UserService implements IUserService {
settingsService: ISettingsService; settingsService: ISettingsService;
logger: ILogger; logger: ILogger;
jwt: JwtType; jwt: JwtType;
jobQueue: IJobQueue; worker: IWorker;
monitorsRepository: IMonitorsRepository; monitorsRepository: IMonitorsRepository;
usersRepository: IUsersRepository; usersRepository: IUsersRepository;
invitesRepository: IInvitesRepository; invitesRepository: IInvitesRepository;
@@ -93,7 +93,7 @@ export class UserService implements IUserService {
this.settingsService = settingsService; this.settingsService = settingsService;
this.logger = logger; this.logger = logger;
this.jwt = jwt; this.jwt = jwt;
this.jobQueue = jobQueue; this.worker = worker;
this.crypto = crypto; this.crypto = crypto;
this.monitorsRepository = monitorsRepository; this.monitorsRepository = monitorsRepository;
this.usersRepository = usersRepository; this.usersRepository = usersRepository;
@@ -330,7 +330,7 @@ export class UserService implements IUserService {
if (roles.includes("superadmin")) { if (roles.includes("superadmin")) {
const monitors = await this.monitorsRepository.findByTeamId(teamId, {}); const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
if (monitors) { if (monitors) {
await Promise.all(monitors.map((monitor) => this.jobQueue.deleteJob(monitor))); await Promise.all(monitors.map((monitor) => this.worker.deleteJob(monitor)));
} }
} }
// 6. Delete the user by id // 6. Delete the user by id
+1 -1
View File
@@ -63,7 +63,7 @@ export class EmailService implements IEmailService {
init = () => { init = () => {
const loadTemplate = (templateName: string) => { const loadTemplate = (templateName: string) => {
try { try {
const templatePath = this.path.join(__dirname, `../../templates/${templateName}.mjml`); const templatePath = this.path.join(__dirname, `../templates/${templateName}.mjml`);
const templateContent = this.fs.readFileSync(templatePath, "utf8"); const templateContent = this.fs.readFileSync(templatePath, "utf8");
return this.compile(templateContent); return this.compile(templateContent);
} catch (error: unknown) { } catch (error: unknown) {
+1 -1
View File
@@ -16,7 +16,7 @@ export const initShutdownListener = (server: Server | null, services: Initialize
try { try {
server?.close(); server?.close();
await services.jobQueue.shutdown(); await services.worker.shutdown();
await services.db.disconnect(); await services.db.disconnect();
logger.info({ message: "Graceful shutdown complete" }); logger.info({ message: "Graceful shutdown complete" });
process.exit(0); process.exit(0);
@@ -22,7 +22,7 @@ import { ITeamsRepository } from "@/domain/teams/team.repository.interface.js";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
import { IBufferService } from "@/service/bufferService.js"; import { IBufferService } from "@/service/bufferService.js";
export interface IQueueHelper { export interface IWorkerHelper {
readonly serviceName: string; readonly serviceName: string;
getHeartbeatJob(): (monitor: Monitor) => Promise<void>; getHeartbeatJob(): (monitor: Monitor) => Promise<void>;
getHeartbeatGeoJob(): (monitor: Monitor) => Promise<void>; getHeartbeatGeoJob(): (monitor: Monitor) => Promise<void>;
@@ -45,7 +45,7 @@ export interface MonitorActionDecision {
}; };
} }
export class QueueHelper implements IQueueHelper { export class QueueHelper implements IWorkerHelper {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private logger: ILogger; private logger: ILogger;
@@ -1,7 +1,7 @@
import { Monitor } from "@/domain/monitors/monitor.types.js"; import { Monitor } from "@/domain/monitors/monitor.types.js";
import { QueueWorker } from "@/domain/queue-workers/queue-worker.type.js"; import { QueueWorker } from "@/domain/queue-workers/queue-worker.type.js";
export type QueueJobFailure = { export type WorkerJobFailure = {
monitorId: string | number; monitorId: string | number;
monitorUrl: string | null; monitorUrl: string | null;
monitorType: string | null; monitorType: string | null;
@@ -10,17 +10,17 @@ export type QueueJobFailure = {
failReason: string | null; failReason: string | null;
}; };
export type QueueMetrics = { export type WorkerMetrics = {
jobs: number; jobs: number;
activeJobs: number; activeJobs: number;
failingJobs: number; failingJobs: number;
jobsWithFailures: QueueJobFailure[]; jobsWithFailures: WorkerJobFailure[];
totalRuns: number; totalRuns: number;
totalFailures: number; totalFailures: number;
workers: QueueWorker[]; workers: QueueWorker[];
}; };
export type QueueJobSummary = { export type WorkerJobSummary = {
monitorId: string | number; monitorId: string | number;
monitorUrl: string | null; monitorUrl: string | null;
monitorType: string | null; monitorType: string | null;
@@ -41,17 +41,17 @@ export type QueueJobSummary = {
repeat: number | null; repeat: number | null;
}; };
export type QueueJobsPagination = { export type WorkerJobsPagination = {
page?: number; page?: number;
rowsPerPage?: number; rowsPerPage?: number;
}; };
export type QueueJobsPage = { export type WorkerJobsPage = {
jobs: QueueJobSummary[]; jobs: WorkerJobSummary[];
count: number; count: number;
}; };
export interface IJobQueue { export interface IWorker {
readonly serviceName: string; readonly serviceName: string;
init(): Promise<boolean>; init(): Promise<boolean>;
addJob(monitorId: string, monitor: Monitor): Promise<void>; addJob(monitorId: string, monitor: Monitor): Promise<void>;
@@ -60,7 +60,7 @@ export interface IJobQueue {
resumeJob(monitor: Monitor): Promise<void>; resumeJob(monitor: Monitor): Promise<void>;
updateJob(monitor: Monitor): Promise<void>; updateJob(monitor: Monitor): Promise<void>;
shutdown(): Promise<void>; shutdown(): Promise<void>;
getMetrics(): Promise<QueueMetrics>; getMetrics(): Promise<WorkerMetrics>;
getJobs(pagination: QueueJobsPagination): Promise<QueueJobsPage>; getJobs(pagination: WorkerJobsPagination): Promise<WorkerJobsPage>;
flushQueues(): Promise<{ success: boolean }>; flushQueues(): Promise<{ success: boolean }>;
} }
@@ -2,9 +2,9 @@ import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interf
import { IQueueWorkersRepository } from "@/domain/queue-workers/queue-worker.repository.interface.js"; import { IQueueWorkersRepository } from "@/domain/queue-workers/queue-worker.repository.interface.js";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
import { MongoStore, Scheduler, IJob, AddJobInput } from "less-simple-scheduler"; import { MongoStore, Scheduler, IJob, AddJobInput } from "less-simple-scheduler";
import { IQueueHelper } from "@/service/job-queues/job-queue.helper.js";
import { Monitor, supportsGeoCheck } from "@/domain/monitors/monitor.types.js"; import { Monitor, supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
import { type QueueMetrics, type QueueJobsPagination, type QueueJobsPage, IJobQueue } from "@/service/job-queues/job-queue.interface.js"; import { IWorkerHelper } from "@/worker/worker.helper.js";
import { type WorkerMetrics, type WorkerJobsPagination, type WorkerJobsPage, IWorker } from "@/worker/worker.interface.js";
import { type QueueMode } from "@/domain/app-settings/app-settings.type.js"; import { type QueueMode } from "@/domain/app-settings/app-settings.type.js";
import { EnvConfig } from "@/domain/app-settings/app-settings.service.js"; import { EnvConfig } from "@/domain/app-settings/app-settings.service.js";
const SERVICE_NAME = "JobQueue"; const SERVICE_NAME = "JobQueue";
@@ -17,11 +17,11 @@ type MonitorJob = Omit<IJob, "data"> & { data: Monitor };
const MONITOR_TEMPLATES = new Set<string>(["monitor-job", "geo-check-job"]); const MONITOR_TEMPLATES = new Set<string>(["monitor-job", "geo-check-job"]);
const isMonitorJob = (job: IJob): job is MonitorJob => MONITOR_TEMPLATES.has(job.template); const isMonitorJob = (job: IJob): job is MonitorJob => MONITOR_TEMPLATES.has(job.template);
export class LessSimpleQueue implements IJobQueue { export class LessSimpleWorker implements IWorker {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private logger: ILogger; private logger: ILogger;
private helper: IQueueHelper; private helper: IWorkerHelper;
private monitorsRepository: IMonitorsRepository; private monitorsRepository: IMonitorsRepository;
private workersRepository: IQueueWorkersRepository; private workersRepository: IQueueWorkersRepository;
private readonly scheduler: Scheduler; private readonly scheduler: Scheduler;
@@ -30,7 +30,7 @@ export class LessSimpleQueue implements IJobQueue {
constructor( constructor(
logger: ILogger, logger: ILogger,
helper: IQueueHelper, helper: IWorkerHelper,
monitorsRepository: IMonitorsRepository, monitorsRepository: IMonitorsRepository,
workersRepository: IQueueWorkersRepository, workersRepository: IQueueWorkersRepository,
scheduler: Scheduler, scheduler: Scheduler,
@@ -46,7 +46,7 @@ export class LessSimpleQueue implements IJobQueue {
} }
get serviceName() { get serviceName() {
return LessSimpleQueue.SERVICE_NAME; return LessSimpleWorker.SERVICE_NAME;
} }
private registerListeners = () => { private registerListeners = () => {
@@ -126,7 +126,7 @@ export class LessSimpleQueue implements IJobQueue {
static async create( static async create(
logger: ILogger, logger: ILogger,
helper: IQueueHelper, helper: IWorkerHelper,
monitorsRepository: IMonitorsRepository, monitorsRepository: IMonitorsRepository,
workersRepository: IQueueWorkersRepository, workersRepository: IQueueWorkersRepository,
envSettings: EnvConfig, envSettings: EnvConfig,
@@ -139,7 +139,7 @@ export class LessSimpleQueue implements IJobQueue {
concurrency: envSettings.queueMode === "primary" ? 0 : 50, concurrency: envSettings.queueMode === "primary" ? 0 : 50,
processEvery: 100, processEvery: 100,
}); });
const instance = new LessSimpleQueue(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode); const instance = new LessSimpleWorker(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode);
await instance.init(queueMode); await instance.init(queueMode);
return instance; return instance;
@@ -350,7 +350,7 @@ export class LessSimpleQueue implements IJobQueue {
this.scheduler.stop(); this.scheduler.stop();
}; };
getMetrics = async (): Promise<QueueMetrics> => { getMetrics = async (): Promise<WorkerMetrics> => {
const stats = await this.scheduler.getStats(); const stats = await this.scheduler.getStats();
const jobsWithFailures = stats.jobsWithFailures.map((failure) => { const jobsWithFailures = stats.jobsWithFailures.map((failure) => {
@@ -379,7 +379,7 @@ export class LessSimpleQueue implements IJobQueue {
}; };
}; };
getJobs = async ({ page = 0, rowsPerPage = 0 }: QueueJobsPagination): Promise<QueueJobsPage> => { getJobs = async ({ page = 0, rowsPerPage = 0 }: WorkerJobsPagination): Promise<WorkerJobsPage> => {
const listOptions = rowsPerPage > 0 ? { skip: Math.max(page, 0) * rowsPerPage, limit: rowsPerPage } : undefined; const listOptions = rowsPerPage > 0 ? { skip: Math.max(page, 0) * rowsPerPage, limit: rowsPerPage } : undefined;
const [jobs, count] = await Promise.all([this.scheduler.getJobs(listOptions), this.scheduler.countJobs()]); const [jobs, count] = await Promise.all([this.scheduler.getJobs(listOptions), this.scheduler.countJobs()]);
const mapped = jobs.map((job) => { const mapped = jobs.map((job) => {
@@ -1,25 +1,25 @@
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js"; import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
import { ILogger } from "@/utils/logger.js"; import { ILogger } from "@/utils/logger.js";
import Scheduler from "super-simple-scheduler"; import Scheduler from "super-simple-scheduler";
import { IQueueHelper } from "@/service/job-queues/job-queue.helper.js";
import { Monitor, MonitorType, supportsGeoCheck } from "@/domain/monitors/monitor.types.js"; import { Monitor, MonitorType, supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
import { hostname } from "node:os"; import { hostname } from "node:os";
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import { QueueMode } from "@/domain/app-settings/app-settings.type.js"; import { QueueMode } from "@/domain/app-settings/app-settings.type.js";
import { IJobQueue, type QueueJobSummary } from "@/service/job-queues/job-queue.interface.js"; import { IWorker, WorkerJobSummary } from "@/worker/worker.interface.js";
import { IWorkerHelper } from "@/worker/worker.helper.js";
const SERVICE_NAME = "JobQueue"; const SERVICE_NAME = "JobQueue";
export class SuperSimpleQueue implements IJobQueue { export class SuperSimpleQueue implements IWorker {
static SERVICE_NAME = SERVICE_NAME; static SERVICE_NAME = SERVICE_NAME;
private logger: ILogger; private logger: ILogger;
private helper: IQueueHelper; private helper: IWorkerHelper;
private workerId: string; private workerId: string;
private monitorsRepository: IMonitorsRepository; private monitorsRepository: IMonitorsRepository;
private readonly scheduler: Scheduler; private readonly scheduler: Scheduler;
constructor(logger: ILogger, helper: IQueueHelper, monitorsRepository: IMonitorsRepository, scheduler: Scheduler) { constructor(logger: ILogger, helper: IWorkerHelper, monitorsRepository: IMonitorsRepository, scheduler: Scheduler) {
this.logger = logger; this.logger = logger;
this.helper = helper; this.helper = helper;
this.monitorsRepository = monitorsRepository; this.monitorsRepository = monitorsRepository;
@@ -100,7 +100,7 @@ export class SuperSimpleQueue implements IJobQueue {
}); });
}; };
static async create(logger: ILogger, helper: IQueueHelper, monitorsRepository: IMonitorsRepository) { static async create(logger: ILogger, helper: IWorkerHelper, monitorsRepository: IMonitorsRepository) {
const scheduler = new Scheduler({ const scheduler = new Scheduler({
concurrency: 50, concurrency: 50,
// storeType: "mongo", // storeType: "mongo",
@@ -341,7 +341,7 @@ export class SuperSimpleQueue implements IJobQueue {
return metrics; return metrics;
}; };
getJobs = async ({ page = 0, rowsPerPage = 0 }: { page?: number; rowsPerPage?: number }): Promise<{ jobs: QueueJobSummary[]; count: number }> => { getJobs = async ({ page = 0, rowsPerPage = 0 }: { page?: number; rowsPerPage?: number }): Promise<{ jobs: WorkerJobSummary[]; count: number }> => {
const jobs = await this.scheduler.getJobs(); const jobs = await this.scheduler.getJobs();
const mapped = jobs.map((job) => { const mapped = jobs.map((job) => {
return { return {