From dc0513862d81790db06850f0009e7a27f7efb9ef Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Sun, 20 Oct 2024 13:07:39 -0700 Subject: [PATCH 1/2] fix: Optimize job policy query --- apps/jobs/src/policy-checker/index.ts | 30 +++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/apps/jobs/src/policy-checker/index.ts b/apps/jobs/src/policy-checker/index.ts index 0994807b5..e52926d23 100644 --- a/apps/jobs/src/policy-checker/index.ts +++ b/apps/jobs/src/policy-checker/index.ts @@ -1,4 +1,4 @@ -import { eq } from "@ctrlplane/db"; +import { and, eq, notInArray } from "@ctrlplane/db"; import { db } from "@ctrlplane/db/client"; import * as schema from "@ctrlplane/db/schema"; import { @@ -9,11 +9,37 @@ import { import { JobStatus } from "@ctrlplane/validators/jobs"; export const run = async () => { + const isPassingApprovalGate = notInArray( + schema.environmentPolicyApproval.status, + ["pending", "rejected"], + ); + const releaseJobTriggers = await db .select() .from(schema.releaseJobTrigger) .innerJoin(schema.job, eq(schema.releaseJobTrigger.jobId, schema.job.id)) - .where(eq(schema.job.status, JobStatus.Pending)) + .innerJoin( + schema.environment, + eq(schema.releaseJobTrigger.environmentId, schema.environment.id), + ) + .leftJoin( + schema.environmentPolicy, + eq(schema.environment.policyId, schema.environmentPolicy.id), + ) + .leftJoin( + schema.environmentPolicyApproval, + and( + eq( + schema.environmentPolicyApproval.policyId, + schema.environmentPolicy.id, + ), + eq( + schema.environmentPolicyApproval.releaseId, + schema.releaseJobTrigger.releaseId, + ), + ), + ) + .where(and(eq(schema.job.status, JobStatus.Pending), isPassingApprovalGate)) .then((rows) => rows.map((row) => row.release_job_trigger)); if (releaseJobTriggers.length === 0) return; From e1b28ed1ac65fe36508ca205137acfddc87baa97 Mon Sep 17 00:00:00 2001 From: Aditya Choudhari Date: Sun, 20 Oct 2024 18:37:48 -0700 Subject: [PATCH 2/2] make checks more explicit --- apps/jobs/src/policy-checker/index.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/apps/jobs/src/policy-checker/index.ts b/apps/jobs/src/policy-checker/index.ts index e52926d23..5aa67af35 100644 --- a/apps/jobs/src/policy-checker/index.ts +++ b/apps/jobs/src/policy-checker/index.ts @@ -1,4 +1,4 @@ -import { and, eq, notInArray } from "@ctrlplane/db"; +import { and, eq, isNull, or } from "@ctrlplane/db"; import { db } from "@ctrlplane/db/client"; import * as schema from "@ctrlplane/db/schema"; import { @@ -9,9 +9,10 @@ import { import { JobStatus } from "@ctrlplane/validators/jobs"; export const run = async () => { - const isPassingApprovalGate = notInArray( - schema.environmentPolicyApproval.status, - ["pending", "rejected"], + const isPassingApprovalGate = or( + isNull(schema.environment.policyId), + eq(schema.environmentPolicy.approvalRequirement, "automatic"), + eq(schema.environmentPolicyApproval.status, "approved"), ); const releaseJobTriggers = await db