Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move variable resolution to job dispatch time #167

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/job-dispatch/src/job-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as schema from "@ctrlplane/db/schema";
import { JobStatus } from "@ctrlplane/validators/jobs";

import { createTriggeredRunbookJob } from "./job-creation.js";
import { createReleaseVariables } from "./job-variables-deployment/job-variables-deployment.js";
import { dispatchJobsQueue } from "./queue.js";

export type DispatchFilterFunc = (
Expand Down Expand Up @@ -67,6 +68,20 @@ class DispatchBuilder {

console.log(`Dispatching ${wfs.length} jobs to the dispatch queue`);

await Promise.all(
wfsWithJobAgent.map((wf) =>
createReleaseVariables(this.db, wf.id).catch(async (error) => {
await this.db
.update(schema.job)
.set({
status: JobStatus.Failure,
message: `Failed to create release variables for job ${wf.id}: ${error.message}`,
})
.where(eq(schema.job.id, wf.id));
}),
),
);

zacharyblasczyk marked this conversation as resolved.
Show resolved Hide resolved
await dispatchJobsQueue.addBulk(
wfsWithJobAgent.map((wf) => ({ name: wf.id, data: { jobId: wf.id } })),
);
Expand Down Expand Up @@ -108,6 +123,17 @@ export const dispatchRunbook = async (
.where(eq(schema.runbook.id, runbookId))
.then(takeFirst);
const job = await createTriggeredRunbookJob(db, runbook, values);
await createReleaseVariables(db, job.id).catch(
async (error) =>
await db
.update(schema.job)
.set({
status: JobStatus.Failure,
message: `Failed to create release variables: ${error.message}`,
})
.where(eq(schema.job.id, job.id)),
);

zacharyblasczyk marked this conversation as resolved.
Show resolved Hide resolved
await dispatchJobsQueue.add(job.id, { jobId: job.id });
return job;
};
6 changes: 0 additions & 6 deletions packages/job-dispatch/src/release-job-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
targetMatchesMetadata,
} from "@ctrlplane/db/schema";

import { createReleaseVariables } from "./job-variables-deployment/job-variables-deployment.js";

type FilterFunc = (
tx: Tx,
insertReleaseJobTriggers: ReleaseJobTriggerInsert[],
Expand Down Expand Up @@ -209,10 +207,6 @@ class ReleaseJobTriggerBuilder {
.values(wtWithJobId)
.returning();

await Promise.all(
jobs.map((job) => createReleaseVariables(this.tx, job.id)),
);

for (const func of this._then) await func(this.tx, releaseJobTriggers);

return releaseJobTriggers;
Expand Down
Loading