Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

check for secondary instance

Thiago Araujo da Silvab0dbae240e43
diff --git a/backend/src/services/org/org-access-sweep-queue.test.ts b/backend/src/services/org/org-access-sweep-queue.test.ts
index f04fe0bebb..181055e3f3 100644
--- a/backend/src/services/org/org-access-sweep-queue.test.ts
+++ b/backend/src/services/org/org-access-sweep-queue.test.ts
@@ -4,7 +4,11 @@ import { TEventInput } from "@app/services/event-outbox/event-outbox-types";
import { ORG_ACCESS_SWEEP_LIMIT, orgAccessSweepQueueFactory } from "./org-access-sweep-queue";
import { ORG_MEMBERSHIP_REMOVED_EVENT } from "./org-membership-events";
-const { loggerWarn } = vi.hoisted(() => ({ loggerWarn: vi.fn<(...args: unknown[]) => void>() }));
+const { loggerWarn, appCfg } = vi.hoisted(() => ({
+ loggerWarn: vi.fn<(...args: unknown[]) => void>(),
+ appCfg: { isSecondaryInstance: false }
+}));
+vi.mock("@app/lib/config/env", () => ({ getConfig: () => appCfg }));
vi.mock("@app/lib/logger", () => ({
logger: { info: () => {}, warn: loggerWarn, error: () => {}, debug: () => {} },
initLogger: () => {}
@@ -16,8 +20,9 @@ const ORG_B = uuid(2);
const build = (orphans: { actorUserId: string | null; actorIdentityId: string | null; scopeOrgId: string }[]) => {
const emitted: TEventInput[] = [];
+ const register = vi.fn();
const queue = orgAccessSweepQueueFactory({
- cronJob: { register: vi.fn() } as never,
+ cronJob: { register } as never,
orgDAL: { findOrphanedProjectActors: vi.fn().mockResolvedValue(orphans) },
eventEmitter: {
emit: async (event) => {
@@ -25,7 +30,7 @@ const build = (orphans: { actorUserId: string | null; actorIdentityId: string |
}
}
});
- return { queue, emitted };
+ return { queue, emitted, register };
};
describe("orgAccessSweepQueue", () => {
@@ -57,6 +62,16 @@ describe("orgAccessSweepQueue", () => {
expect(emitted).toHaveLength(0);
});
+ test("runs the sweep only on the primary instance", () => {
+ for (const isSecondaryInstance of [false, true]) {
+ appCfg.isSecondaryInstance = isSecondaryInstance;
+ const { queue, register } = build([]);
+ queue.init();
+ expect(register).toHaveBeenCalledWith(expect.objectContaining({ enabled: !isSecondaryInstance }));
+ }
+ appCfg.isSecondaryInstance = false;
+ });
+
test("warns when a run hits its row limit", async () => {
const { queue } = build(
Array.from({ length: ORG_ACCESS_SWEEP_LIMIT }, (_, i) => ({
diff --git a/backend/src/services/org/org-access-sweep-queue.ts b/backend/src/services/org/org-access-sweep-queue.ts
index c2459faef0..31c9f8c06a 100644
--- a/backend/src/services/org/org-access-sweep-queue.ts
+++ b/backend/src/services/org/org-access-sweep-queue.ts
@@ -1,3 +1,4 @@
+import { getConfig } from "@app/lib/config/env";
import { CronJobName, TCronJobFactory } from "@app/lib/cron/cron-job";
import { logger } from "@app/lib/logger";
import { TEventEmitter } from "@app/services/event-outbox/event-outbox-types";
@@ -41,10 +42,12 @@ export const orgAccessSweepQueueFactory = ({ cronJob, orgDAL, eventEmitter }: TO
};
const init = () => {
+ const appCfg = getConfig();
cronJob.register({
name: CronJobName.OrgAccessReconcileSweep,
pattern: "0 4 * * *",
runHashTtlS: 24 * 60 * 60,
+ enabled: !appCfg.isSecondaryInstance,
handler: async () => {
const { orphans } = await runSweep();
logger.info(`cron[org-access-reconcile-sweep]: queued ${orphans} orphaned project memberships`);