Fix: Use Check IDs instead of timestamp to process checks
This commit is contained in:
1 parent
9102335d3e
commit
fa0c0abcac
13 files changed
+491
-56
No files matched your search
@@ -3,7 +3,7 @@ import mongoose from "mongoose";
|
||||
import { MongoMemoryServer } from "mongodb-memory-server";
|
||||
import MongoJobsRepository from "../../src/domain/jobs/job.repository.mongo.ts";
|
||||
import JobModel, { type JobDocument } from "../../src/domain/jobs/job.model.ts";
|
||||
import { BACKOFF_MS, LOCK_MS, PARKED, type Job, type JobType } from "../../src/domain/jobs/job.type.ts";
|
||||
import { BACKOFF_MS, LOCK_MS, PARKED, type Job, type JobType, type PendingCheck } from "../../src/domain/jobs/job.type.ts";
|
||||
|
||||
// ── Real-Mongo harness ─────────────────────────────────────────────────────────
|
||||
// The headline guarantee of the queue — "exactly one claimer wins" — IS MongoDB's
|
||||
@@ -58,6 +58,9 @@ const ownedBy = (r: MongoJobsRepository) => (r as unknown as { workerId: string
|
||||
|
||||
const WORKER_ID = "worker-under-test";
|
||||
|
||||
// Pending entries as ingest pushes them: one per inserted check, stamped with the check's createdAt.
|
||||
const pending = (...checkIds: string[]): PendingCheck[] => checkIds.map((checkId, i) => ({ checkId, createdAt: NOW + i }));
|
||||
|
||||
describe("MongoJobsRepository", () => {
|
||||
let repo: MongoJobsRepository;
|
||||
|
||||
@@ -239,6 +242,23 @@ describe("MongoJobsRepository", () => {
|
||||
expect(row?.failCount).toBe(2);
|
||||
expect(row?.lastFailReason).toBe("timeout");
|
||||
});
|
||||
|
||||
it("re-arms instead of parking when checks were pushed while the run held the lease", async () => {
|
||||
await seedJob({
|
||||
_id: "evaluate:mon-1",
|
||||
type: "evaluate",
|
||||
intervalMs: null,
|
||||
lockedBy: ownedBy(repo),
|
||||
lockedUntil: NOW + LOCK_MS,
|
||||
pendingChecks: pending("c-late"), // arrived mid-run, not yet pulled
|
||||
});
|
||||
|
||||
expect(await repo.recordOneShot("evaluate:mon-1", NOW + 1_000)).toBe(true);
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.nextScheduledAt).toBe(NOW + 1_000); // due now, not PARKED
|
||||
expect(row?.lockedBy).toBeNull();
|
||||
expect(row?.runCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
// ── renewLocks (lease renewal) ───────────────────────────────────────────────
|
||||
@@ -325,10 +345,10 @@ describe("MongoJobsRepository", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── upsertEvaluate (handoff + $min coalescing) ───────────────────────────────
|
||||
// ── upsertEvaluate (handoff: $push ids + $min due time) ──────────────────────
|
||||
describe("upsertEvaluate", () => {
|
||||
it("creates a single evaluate row keyed by monitor", async () => {
|
||||
await repo.upsertEvaluate("mon-1", NOW);
|
||||
it("creates a single evaluate row keyed by monitor carrying the pushed ids", async () => {
|
||||
await repo.upsertEvaluate("mon-1", pending("c1", "c2"), NOW);
|
||||
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.type).toBe("evaluate");
|
||||
@@ -336,11 +356,12 @@ describe("MongoJobsRepository", () => {
|
||||
expect(row?.nextScheduledAt).toBe(NOW);
|
||||
expect(row?.intervalMs).toBeNull();
|
||||
expect(row?.isActive).toBe(true);
|
||||
expect(row?.pendingChecks).toEqual(pending("c1", "c2"));
|
||||
});
|
||||
|
||||
it("coalesces rapid checks: a later call never pushes runAt out ($min)", async () => {
|
||||
await repo.upsertEvaluate("mon-1", NOW);
|
||||
await repo.upsertEvaluate("mon-1", NOW + 9_000);
|
||||
await repo.upsertEvaluate("mon-1", pending("c1"), NOW);
|
||||
await repo.upsertEvaluate("mon-1", pending("c2"), NOW + 9_000);
|
||||
|
||||
const rows = await JobModel.find({ type: "evaluate", refId: "mon-1" }).lean<JobDocument[]>();
|
||||
expect(rows).toHaveLength(1);
|
||||
@@ -348,19 +369,96 @@ describe("MongoJobsRepository", () => {
|
||||
});
|
||||
|
||||
it("lowers runAt when an earlier evaluation arrives ($min)", async () => {
|
||||
await repo.upsertEvaluate("mon-1", NOW + 9_000);
|
||||
await repo.upsertEvaluate("mon-1", NOW);
|
||||
await repo.upsertEvaluate("mon-1", pending("c1"), NOW + 9_000);
|
||||
await repo.upsertEvaluate("mon-1", pending("c2"), NOW);
|
||||
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.nextScheduledAt).toBe(NOW);
|
||||
});
|
||||
|
||||
it("concurrent upserts for one monitor produce exactly one row", async () => {
|
||||
const workers = Array.from({ length: 20 }, (_, i) => new MongoJobsRepository(`worker-${i}`));
|
||||
await Promise.all(workers.map((w) => w.upsertEvaluate("mon-1", NOW)));
|
||||
it("accumulates ids across flushes so a late batch is never lost", async () => {
|
||||
await repo.upsertEvaluate("mon-1", pending("c2"), NOW); // later check, flushed first
|
||||
await repo.upsertEvaluate("mon-1", pending("c1"), NOW + 15_000); // earlier check, flushed later
|
||||
|
||||
const count = await JobModel.countDocuments({ type: "evaluate", refId: "mon-1" });
|
||||
expect(count).toBe(1);
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.pendingChecks.map((entry) => entry.checkId)).toEqual(["c2", "c1"]);
|
||||
});
|
||||
|
||||
it("keeps pushing onto a row that is currently leased by a worker", async () => {
|
||||
await seedJob({
|
||||
_id: "evaluate:mon-1",
|
||||
type: "evaluate",
|
||||
intervalMs: null,
|
||||
lockedBy: "other-worker",
|
||||
lockedUntil: NOW + LOCK_MS,
|
||||
pendingChecks: pending("c1"),
|
||||
});
|
||||
|
||||
await repo.upsertEvaluate("mon-1", pending("c2"), NOW);
|
||||
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.lockedBy).toBe("other-worker"); // lease untouched
|
||||
expect(row?.pendingChecks.map((entry) => entry.checkId)).toEqual(["c1", "c2"]);
|
||||
});
|
||||
|
||||
it("concurrent upserts for one monitor produce exactly one row holding every id", async () => {
|
||||
const workers = Array.from({ length: 20 }, (_, i) => new MongoJobsRepository(`worker-${i}`));
|
||||
await Promise.all(workers.map((w, i) => w.upsertEvaluate("mon-1", pending(`c${i}`), NOW)));
|
||||
|
||||
const rows = await JobModel.find({ type: "evaluate", refId: "mon-1" }).lean<JobDocument[]>();
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].pendingChecks).toHaveLength(20);
|
||||
});
|
||||
});
|
||||
|
||||
// ── pullEvaluated (evaluator removes applied ids) ───────────────────────────
|
||||
describe("pullEvaluated", () => {
|
||||
it("removes only the listed ids and leaves the rest", async () => {
|
||||
await seedJob({
|
||||
_id: "evaluate:mon-1",
|
||||
type: "evaluate",
|
||||
intervalMs: null,
|
||||
lockedBy: ownedBy(repo),
|
||||
lockedUntil: NOW + LOCK_MS,
|
||||
pendingChecks: pending("c1", "c2", "c3"),
|
||||
});
|
||||
|
||||
expect(await repo.pullEvaluated("evaluate:mon-1", ["c1", "c3"])).toBe(true);
|
||||
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.pendingChecks.map((entry) => entry.checkId)).toEqual(["c2"]);
|
||||
});
|
||||
|
||||
it("is fenced on the lease: another worker's row is untouched", async () => {
|
||||
await seedJob({
|
||||
_id: "evaluate:mon-1",
|
||||
type: "evaluate",
|
||||
intervalMs: null,
|
||||
lockedBy: "other-worker",
|
||||
lockedUntil: NOW + LOCK_MS,
|
||||
pendingChecks: pending("c1"),
|
||||
});
|
||||
|
||||
expect(await repo.pullEvaluated("evaluate:mon-1", ["c1"])).toBe(false);
|
||||
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.pendingChecks.map((entry) => entry.checkId)).toEqual(["c1"]);
|
||||
});
|
||||
|
||||
it("is a no-op for an empty id list", async () => {
|
||||
await seedJob({
|
||||
_id: "evaluate:mon-1",
|
||||
type: "evaluate",
|
||||
intervalMs: null,
|
||||
lockedBy: ownedBy(repo),
|
||||
lockedUntil: NOW + LOCK_MS,
|
||||
pendingChecks: pending("c1"),
|
||||
});
|
||||
|
||||
expect(await repo.pullEvaluated("evaluate:mon-1", [])).toBe(true);
|
||||
|
||||
const row = await readRow("evaluate:mon-1");
|
||||
expect(row?.pendingChecks).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -487,7 +585,7 @@ describe("MongoJobsRepository", () => {
|
||||
it("deleteById drops every row for the monitor", async () => {
|
||||
await seedJob({ _id: "check:mon-1", type: "check" });
|
||||
await seedJob({ _id: "geo:mon-1", type: "geo-check" });
|
||||
await repo.upsertEvaluate("mon-1", NOW);
|
||||
await repo.upsertEvaluate("mon-1", pending("c1"), NOW);
|
||||
|
||||
await repo.deleteById("mon-1");
|
||||
|
||||
|
||||
Reference in new issue
Block a user