add bulk ops for inserting monitors, set primary concurrency to 0, handles scheduling only

This commit is contained in:
Alex Holliday committed 2026-06-03 11:00:40 -07:00
1 parent 9e4fd6d708
commit 707b8f903b
2 files changed
+32 -25

No files matched your search

@@ -14,6 +14,9 @@ const SERVICE_NAME = "JobQueue";
// every lockMs/3, so ~5s with the default lockMs), comfortably inside this TTL. // every lockMs/3, so ~5s with the default lockMs), comfortably inside this TTL.
const WORKER_TTL_MS = 30_000; const WORKER_TTL_MS = 30_000;
// Derived from the scheduler
type SchedulerJobInput = Parameters<Scheduler["addJobs"]>[0][number];
// For discriminating job types // For discriminating job types
type MonitorJob = Omit<IJob, "data"> & { data: Monitor }; 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"]);
@@ -138,7 +141,7 @@ export class LessSimpleQueue implements IJobQueue {
url: envSettings.dbConnectionString ?? "mongodb://localhost:27017/uptime_db", url: envSettings.dbConnectionString ?? "mongodb://localhost:27017/uptime_db",
}); });
const scheduler = new Scheduler(store, { const scheduler = new Scheduler(store, {
concurrency: 50, concurrency: envSettings.queueMode === "primary" ? 0 : 50,
}); });
const instance = new LessSimpleQueue(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode); const instance = new LessSimpleQueue(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode);
await instance.init(queueMode); await instance.init(queueMode);
@@ -190,11 +193,9 @@ export class LessSimpleQueue implements IJobQueue {
if (!monitors) { if (!monitors) {
return true; return true;
} }
for (const monitor of monitors) { const jobInputs = monitors.flatMap((monitor) => this.buildJobInputs(monitor));
const randomOffset = Math.floor(Math.random() * 100); if (jobInputs.length > 0) {
setTimeout(() => { await this.scheduler.addJobs(jobInputs);
this.addJob(monitor.id, monitor);
}, randomOffset);
} }
this.scheduler.addJob({ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true, upsert: true }); this.scheduler.addJob({ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true, upsert: true });
@@ -211,32 +212,38 @@ export class LessSimpleQueue implements IJobQueue {
} }
}; };
addJob = async (monitorId: string, monitor: Monitor) => { // Builds the scheduler inputs for a monitor: always a monitor-job, plus a
this.scheduler.addJob({ // geo-check job when the monitor supports and enables geo checks.
id: monitorId, private buildJobInputs = (monitor: Monitor): SchedulerJobInput[] => {
template: "monitor-job", const inputs: SchedulerJobInput[] = [
repeat: monitor.interval, {
active: monitor.isActive, id: monitor.id,
data: monitor, template: "monitor-job",
upsert: true, repeat: monitor.interval,
}); active: monitor.isActive,
data: monitor,
upsert: true,
jitter: true,
},
];
// Return early if we don't need geo checks if (supportsGeoCheck(monitor.type) && monitor.geoCheckEnabled) {
if (!supportsGeoCheck(monitor.type)) { inputs.push({
return; id: `${monitor.id}-geo`,
}
// Add geo check job if enabled for HTTP monitors
if (monitor.geoCheckEnabled) {
this.scheduler.addJob({
id: `${monitorId}-geo`,
template: "geo-check-job", template: "geo-check-job",
repeat: monitor.geoCheckInterval, repeat: monitor.geoCheckInterval,
active: monitor.isActive, active: monitor.isActive,
data: monitor, data: monitor,
upsert: true, upsert: true,
jitter: true,
}); });
} }
return inputs;
};
addJob = async (_monitorId: string, monitor: Monitor) => {
await this.scheduler.addJobs(this.buildJobInputs(monitor));
}; };
deleteJob = async (monitor: Monitor) => { deleteJob = async (monitor: Monitor) => {
@@ -26,7 +26,7 @@ export class BufferService implements IBufferService {
private geoChecksService: IGeoChecksService; private geoChecksService: IGeoChecksService;
constructor(logger: ILogger, checkService: ICheckService, geoChecksService: IGeoChecksService, settingsService: ISettingsService) { constructor(logger: ILogger, checkService: ICheckService, geoChecksService: IGeoChecksService, settingsService: ISettingsService) {
this.BUFFER_TIMEOUT = settingsService.getSettings().nodeEnv === "development" ? 10 : 1000 * 60 * 1; // 1 minute this.BUFFER_TIMEOUT = settingsService.getSettings().nodeEnv === "development" ? 1000 : 1000 * 60 * 1; // 1 minute
this.logger = logger; this.logger = logger;
this.checksService = checkService; this.checksService = checkService;
this.geoChecksService = geoChecksService; this.geoChecksService = geoChecksService;