Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Jan 24, 2025
1 parent 086b29a commit 2d7bc87
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
4 changes: 4 additions & 0 deletions packages/db/src/schema/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export const environmentPolicyApproval = pgTable(
.references(() => release.id, { onDelete: "cascade" }),
status: approvalStatusType("status").notNull().default("pending"),
userId: uuid("user_id").references(() => user.id, { onDelete: "set null" }),
approvedAt: timestamp("approved_at", {
withTimezone: true,
precision: 0,
}).default(sql`NULL`),
},
(t) => ({ uniq: uniqueIndex().on(t.policyId, t.releaseId) }),
);
Expand Down
28 changes: 0 additions & 28 deletions packages/job-dispatch/src/gradual-rollout.ts

This file was deleted.

60 changes: 56 additions & 4 deletions packages/job-dispatch/src/policies/gradual-rollout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import _ from "lodash";
import murmurhash from "murmurhash";
import { isPresent } from "ts-is-present";

import { eq, inArray } from "@ctrlplane/db";
import { and, eq, inArray } from "@ctrlplane/db";
import * as schema from "@ctrlplane/db/schema";

import type { ReleaseIdPolicyChecker } from "./utils.js";
import { isReleaseJobTriggerInRolloutWindow } from "../gradual-rollout.js";

const timeWindowPercent = (startDate: Date, duration: number) => {
if (duration === 0) return 100;
const now = Date.now();
const start = startDate.getTime();
const end = start + duration;

if (now < start) return 0;
if (now > end) return 100;

return ((now - start) / duration) * 100;
};

export const isReleaseJobTriggerInRolloutWindow = (
session: string,
startDate: Date,
duration: number,
) => murmurhash.v3(session, 11) % 100 < timeWindowPercent(startDate, duration);

export const getRolloutDateForReleaseJobTrigger = (
session: string,
startDate: Date,
duration: number,
) =>
new Date(
startDate.getTime() + ((duration * murmurhash.v3(session, 11)) % 100) / 100,
);

/**
*
Expand Down Expand Up @@ -37,6 +63,16 @@ export const isPassingJobRolloutPolicy: ReleaseIdPolicyChecker = async (
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.release.id),
),
)
.where(
inArray(
schema.releaseJobTrigger.id,
Expand All @@ -47,11 +83,27 @@ export const isPassingJobRolloutPolicy: ReleaseIdPolicyChecker = async (
return policies
.filter((p) => {
if (p.environment_policy == null) return true;
if (p.environment_policy.approvalRequirement === "automatic")
return isReleaseJobTriggerInRolloutWindow(
[
p.release.id,
p.environment.id,
p.release_job_trigger.resourceId,
].join(":"),
p.release.createdAt,
p.environment_policy.rolloutDuration,
);

const approval = p.environment_policy_approval;
const { status, approvedAt } = approval ?? {};
const isApproved = status === "approved" && approvedAt != null;
if (!isApproved) return false;

return isReleaseJobTriggerInRolloutWindow(
[p.release.id, p.environment.id, p.release_job_trigger.resourceId].join(
":",
),
p.release.createdAt,
approvedAt,
p.environment_policy.rolloutDuration,
);
})
Expand Down

0 comments on commit 2d7bc87

Please sign in to comment.