replace findAll with findAllForScheduling

This commit is contained in:
Alex Holliday committed 2026-08-04 15:16:03 -07:00
1 parent 4c8342f379
commit 5fa74bd883
7 files changed
+47 -23

No files matched your search

@@ -1,4 +1,4 @@
import type { MonitorType, Monitor, MonitorStatus, MonitorsSummary, CheckSnapshot } from "@/domain/monitors/monitor.type.js"; import type { MonitorType, Monitor, MonitorStatus, MonitorsSummary, CheckSnapshot, MonitorScheduleFields } from "@/domain/monitors/monitor.type.js";
export interface TeamQueryConfig { export interface TeamQueryConfig {
limit?: number; limit?: number;
@@ -26,7 +26,7 @@ export interface IMonitorsRepository {
findByIdLean(monitorId: string): Promise<Monitor | null>; findByIdLean(monitorId: string): Promise<Monitor | null>;
// collection fetch // collection fetch
findAll(): Promise<Monitor[]>; findAllForScheduling(): Promise<MonitorScheduleFields[]>;
findByTeamId(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>; findByTeamId(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>;
findByTeamIdWithStats(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>; findByTeamIdWithStats(teamId: string, config: TeamQueryConfig): Promise<Monitor[]>;
findByIds(monitorIds: string[]): Promise<Monitor[]>; findByIds(monitorIds: string[]): Promise<Monitor[]>;
@@ -1,7 +1,7 @@
import { MonitorModel } from "@/domain/monitors/monitor.model.js"; import { MonitorModel } from "@/domain/monitors/monitor.model.js";
import type { MonitorDocument, CheckSnapshotDocument } from "@/domain/monitors/monitor.model.js"; import type { MonitorDocument, CheckSnapshotDocument } from "@/domain/monitors/monitor.model.js";
import type { CheckSnapshot } from "@/domain/checks/check.type.js"; import type { CheckSnapshot } from "@/domain/checks/check.type.js";
import type { Monitor, MonitorStatus, MonitorsSummary } from "@/domain/monitors/monitor.type.js"; import type { Monitor, MonitorScheduleFields, MonitorStatus, MonitorsSummary } from "@/domain/monitors/monitor.type.js";
import mongoose, { type FilterQuery, type PipelineStage } from "mongoose"; import mongoose, { type FilterQuery, type PipelineStage } from "mongoose";
import { MongoBulkWriteError } from "mongodb"; import { MongoBulkWriteError } from "mongodb";
import { AppError } from "@/utils/AppError.js"; import { AppError } from "@/utils/AppError.js";
@@ -41,13 +41,20 @@ class MongoMonitorsRepository implements IMonitorsRepository {
}; };
findByIdLean = async (monitorId: string): Promise<Monitor | null> => { findByIdLean = async (monitorId: string): Promise<Monitor | null> => {
const monitor = await MonitorModel.findOne({ _id: monitorId }); const monitor = await MonitorModel.findOne({ _id: monitorId }).select({ recentChecks: 0 });
return monitor ? this.toEntity(monitor) : null; return monitor ? this.toEntity(monitor) : null;
}; };
findAll = async (): Promise<Monitor[]> => { findAllForScheduling = async (): Promise<MonitorScheduleFields[]> => {
const monitors = await MonitorModel.find(); const docs = await MonitorModel.find({}, { type: 1, isActive: 1, interval: 1, geoCheckEnabled: 1, geoCheckInterval: 1 }).lean();
return this.mapDocuments(monitors); return docs.map((doc) => ({
id: doc._id.toString(),
type: doc.type,
isActive: doc.isActive,
interval: doc.interval,
geoCheckEnabled: doc.geoCheckEnabled ?? false,
geoCheckInterval: doc.geoCheckInterval ?? 300000,
}));
}; };
private queryBuilder = (config: TeamQueryConfig, teamId: string): FilterQuery<MonitorDocument> => { private queryBuilder = (config: TeamQueryConfig, teamId: string): FilterQuery<MonitorDocument> => {
@@ -182,7 +189,7 @@ class MongoMonitorsRepository implements IMonitorsRepository {
}, },
...(statusPatch && { $set: statusPatch }), ...(statusPatch && { $set: statusPatch }),
}, },
{ returnDocument: "after" } { returnDocument: "after", projection: { recentChecks: 0 } }
); );
if (!updatedMonitor) { if (!updatedMonitor) {
@@ -178,3 +178,5 @@ export interface Game {
} }
export type GamesMap = Record<string, Game>; export type GamesMap = Record<string, Game>;
export type MonitorScheduleFields = Pick<Monitor, "id" | "type" | "isActive" | "interval" | "geoCheckEnabled" | "geoCheckInterval">;
+4 -4
View File
@@ -1,7 +1,7 @@
import { IJobScheduler } from "@/worker/worker.interface.js"; import { IJobScheduler } from "@/worker/worker.interface.js";
import { IJobsRepository } from "@/domain/jobs/job.repository.interface.js"; import { IJobsRepository } from "@/domain/jobs/job.repository.interface.js";
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js"; import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
import { Monitor, supportsGeoCheck } from "@/domain/monitors/monitor.type.js"; import { Monitor, MonitorScheduleFields, supportsGeoCheck } from "@/domain/monitors/monitor.type.js";
import { type Job, type JobSeed, type JobType, jobId } from "@/domain/jobs/job.type.js"; import { type Job, type JobSeed, type JobType, jobId } from "@/domain/jobs/job.type.js";
import { IQueueWorkersRepository } from "@/domain/queue-workers/queue-worker.repository.interface.js"; import { IQueueWorkersRepository } from "@/domain/queue-workers/queue-worker.repository.interface.js";
import { WorkerJobsPagination, WorkerJobSummary, WorkerMetrics } from "@/worker/worker.interface.js"; import { WorkerJobsPagination, WorkerJobSummary, WorkerMetrics } from "@/worker/worker.interface.js";
@@ -43,7 +43,7 @@ export class JobScheduler implements IJobScheduler {
"cleanup-retention": POLL_MS, "cleanup-retention": POLL_MS,
}; };
// Helpers, builds job seeds. immediate=true runs on the next tick; otherwise jitter spreads the herd. // Helpers, builds job seeds. immediate=true runs on the next tick; otherwise jitter spreads the herd.
protected toCheckJob = (monitor: Monitor, now: number, immediate = false): JobSeed => ({ protected toCheckJob = (monitor: MonitorScheduleFields, now: number, immediate = false): JobSeed => ({
id: jobId("check", monitor.id), id: jobId("check", monitor.id),
type: "check", type: "check",
refId: monitor.id, refId: monitor.id,
@@ -52,7 +52,7 @@ export class JobScheduler implements IJobScheduler {
intervalMs: monitor.interval, intervalMs: monitor.interval,
}); });
protected toGeoCheckJob = (monitor: Monitor, now: number, immediate = false): JobSeed => ({ protected toGeoCheckJob = (monitor: MonitorScheduleFields, now: number, immediate = false): JobSeed => ({
...this.toCheckJob(monitor, now, immediate), ...this.toCheckJob(monitor, now, immediate),
id: jobId("geo-check", monitor.id), id: jobId("geo-check", monitor.id),
type: "geo-check", type: "geo-check",
@@ -187,7 +187,7 @@ export class JobScheduler implements IJobScheduler {
// Seed queue // Seed queue
protected reconcile = async () => { protected reconcile = async () => {
const now = Date.now(); const now = Date.now();
const monitors = await this.monitorsRepository.findAll(); const monitors = await this.monitorsRepository.findAllForScheduling();
for (const monitor of monitors) { for (const monitor of monitors) {
await this.jobsRepository.upsertJob(this.toCheckJob(monitor, now)); await this.jobsRepository.upsertJob(this.toCheckJob(monitor, now));
if (supportsGeoCheck(monitor.type) && monitor.geoCheckEnabled) { if (supportsGeoCheck(monitor.type) && monitor.geoCheckEnabled) {
@@ -1,6 +1,6 @@
import type { IMonitorsRepository, TeamQueryConfig, SummaryConfig } from "../../src/domain/monitors/monitor.repository.interface.ts"; import type { IMonitorsRepository, TeamQueryConfig, SummaryConfig } from "../../src/domain/monitors/monitor.repository.interface.ts";
import type { CheckSnapshot } from "../../src/domain/checks/check.type.ts"; import type { CheckSnapshot } from "../../src/domain/checks/check.type.ts";
import type { Monitor, MonitorsSummary } from "../../src/domain/monitors/monitor.type.ts"; import type { Monitor, MonitorScheduleFields, MonitorsSummary } from "../../src/domain/monitors/monitor.type.ts";
export class InMemoryMonitorsRepository implements IMonitorsRepository { export class InMemoryMonitorsRepository implements IMonitorsRepository {
private monitors: Monitor[] = []; private monitors: Monitor[] = [];
@@ -24,8 +24,15 @@ export class InMemoryMonitorsRepository implements IMonitorsRepository {
return { ...monitor }; return { ...monitor };
} }
async findAll(): Promise<Monitor[]> { async findAllForScheduling(): Promise<MonitorScheduleFields[]> {
return this.monitors.map((m) => ({ ...m })); return this.monitors.map((m) => ({
id: m.id,
type: m.type,
isActive: m.isActive,
interval: m.interval,
geoCheckEnabled: m.geoCheckEnabled ?? false,
geoCheckInterval: m.geoCheckInterval ?? 300000,
}));
} }
async findByTeamId(_teamId: string, _config: TeamQueryConfig): Promise<Monitor[]> { async findByTeamId(_teamId: string, _config: TeamQueryConfig): Promise<Monitor[]> {
@@ -84,7 +84,7 @@ const createWorker = (overrides?: { queueMode?: QueueMode; queuePrimaryProcesses
const monitorsRepository = { const monitorsRepository = {
findByIds: jest.fn<any>().mockResolvedValue([makeMonitor()]), findByIds: jest.fn<any>().mockResolvedValue([makeMonitor()]),
findByIdLean: jest.fn<any>().mockResolvedValue(makeMonitor()), findByIdLean: jest.fn<any>().mockResolvedValue(makeMonitor()),
findAll: jest.fn<any>().mockResolvedValue([]), findAllForScheduling: jest.fn<any>().mockResolvedValue([]),
updateById: jest.fn<any>().mockResolvedValue({}), updateById: jest.fn<any>().mockResolvedValue({}),
}; };
const checksRepository = { const checksRepository = {
@@ -183,7 +183,11 @@ describe("DBQueueWorker", () => {
queueMode: "primary", queueMode: "primary",
queuePrimaryProcesses: false, // isolate reconcile from the polling loops queuePrimaryProcesses: false, // isolate reconcile from the polling loops
mocks: { mocks: {
monitorsRepository: { findByIds: jest.fn<any>(), findAll: jest.fn<any>().mockResolvedValue([makeMonitor()]), updateById: jest.fn<any>() }, monitorsRepository: {
findByIds: jest.fn<any>(),
findAllForScheduling: jest.fn<any>().mockResolvedValue([makeMonitor()]),
updateById: jest.fn<any>(),
},
}, },
}); });
@@ -200,7 +204,11 @@ describe("DBQueueWorker", () => {
queueMode: "primary", queueMode: "primary",
queuePrimaryProcesses: false, queuePrimaryProcesses: false,
mocks: { mocks: {
monitorsRepository: { findByIds: jest.fn<any>(), findAll: jest.fn<any>().mockResolvedValue([geoMonitor]), updateById: jest.fn<any>() }, monitorsRepository: {
findByIds: jest.fn<any>(),
findAllForScheduling: jest.fn<any>().mockResolvedValue([geoMonitor]),
updateById: jest.fn<any>(),
},
}, },
}); });
@@ -38,7 +38,7 @@ const createScheduler = (overrides?: { queueMode?: QueueMode; mocks?: Record<str
deleteById: jest.fn<any>().mockResolvedValue(undefined), deleteById: jest.fn<any>().mockResolvedValue(undefined),
}; };
const monitorsRepository = { const monitorsRepository = {
findAll: jest.fn<any>().mockResolvedValue([]), findAllForScheduling: jest.fn<any>().mockResolvedValue([]),
}; };
const logger = createMockLogger(); const logger = createMockLogger();
const mocks = { jobsRepository, queueWorkersRepository, monitorsRepository, logger, ...overrides?.mocks }; const mocks = { jobsRepository, queueWorkersRepository, monitorsRepository, logger, ...overrides?.mocks };
@@ -73,7 +73,7 @@ describe("JobScheduler", () => {
it("primary mode reconciles: seeds a check row per monitor plus the two cleanup rows", async () => { it("primary mode reconciles: seeds a check row per monitor plus the two cleanup rows", async () => {
const { scheduler, mocks } = createScheduler({ const { scheduler, mocks } = createScheduler({
queueMode: "primary", queueMode: "primary",
mocks: { monitorsRepository: { findAll: jest.fn<any>().mockResolvedValue([makeMonitor()]) } }, mocks: { monitorsRepository: { findAllForScheduling: jest.fn<any>().mockResolvedValue([makeMonitor()]) } },
}); });
await scheduler.init(); await scheduler.init();
@@ -87,7 +87,7 @@ describe("JobScheduler", () => {
it("primary mode also seeds a geo-check row for geo-enabled monitors", async () => { it("primary mode also seeds a geo-check row for geo-enabled monitors", async () => {
const { scheduler, mocks } = createScheduler({ const { scheduler, mocks } = createScheduler({
queueMode: "primary", queueMode: "primary",
mocks: { monitorsRepository: { findAll: jest.fn<any>().mockResolvedValue([makeMonitor({ geoCheckEnabled: true })]) } }, mocks: { monitorsRepository: { findAllForScheduling: jest.fn<any>().mockResolvedValue([makeMonitor({ geoCheckEnabled: true })]) } },
}); });
await scheduler.init(); await scheduler.init();
@@ -98,12 +98,12 @@ describe("JobScheduler", () => {
it("worker mode heartbeats but does not reconcile (no monitor reads, no seeds)", async () => { it("worker mode heartbeats but does not reconcile (no monitor reads, no seeds)", async () => {
const { scheduler, mocks } = createScheduler({ const { scheduler, mocks } = createScheduler({
queueMode: "worker", queueMode: "worker",
mocks: { monitorsRepository: { findAll: jest.fn<any>().mockResolvedValue([makeMonitor()]) } }, mocks: { monitorsRepository: { findAllForScheduling: jest.fn<any>().mockResolvedValue([makeMonitor()]) } },
}); });
await scheduler.init(); await scheduler.init();
expect(mocks.queueWorkersRepository.upsert).toHaveBeenCalledWith("worker-1", "worker", false); expect(mocks.queueWorkersRepository.upsert).toHaveBeenCalledWith("worker-1", "worker", false);
expect(mocks.monitorsRepository.findAll).not.toHaveBeenCalled(); expect(mocks.monitorsRepository.findAllForScheduling).not.toHaveBeenCalled();
expect(mocks.jobsRepository.upsertJob).not.toHaveBeenCalled(); expect(mocks.jobsRepository.upsertJob).not.toHaveBeenCalled();
expect(mocks.jobsRepository.upsertCleanupJob).not.toHaveBeenCalled(); expect(mocks.jobsRepository.upsertCleanupJob).not.toHaveBeenCalled();
}); });