-
Notifications
You must be signed in to change notification settings - Fork 11
fix: Add updated resource and inserted resource workers #461
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7e79a94
fix: Process upserted release worker
adityachoudhari26 7605737
cleanup
adityachoudhari26 180c374
stjff
adityachoudhari26 9320db5
Merge branch 'main' of github.com:sizzldev/ctrlplane into process-ups…
adityachoudhari26 01a921c
cleanup
adityachoudhari26 5ea31c8
nit
adityachoudhari26 ae27732
fix
adityachoudhari26 8c72756
rabbit
adityachoudhari26 0067da5
reanme
adityachoudhari26 4117f76
updates
adityachoudhari26 f30b80b
d[es
adityachoudhari26 9109623
more
adityachoudhari26 ed9f8c2
cleanup
adityachoudhari26 356fdc1
deps
adityachoudhari26 692aa3f
wrap workers in tx
adityachoudhari26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { Tx } from "@ctrlplane/db"; | ||
| import type { ReleaseTargetIdentifier } from "@ctrlplane/rule-engine"; | ||
| import { isPresent } from "ts-is-present"; | ||
|
|
||
| import { and, eq } from "@ctrlplane/db"; | ||
| import * as SCHEMA from "@ctrlplane/db/schema"; | ||
|
|
||
| const getReleaseTargetInsertsForSystem = async ( | ||
| db: Tx, | ||
| resourceId: string, | ||
| system: SCHEMA.System & { | ||
| environments: SCHEMA.Environment[]; | ||
| deployments: SCHEMA.Deployment[]; | ||
| }, | ||
| ): Promise<ReleaseTargetIdentifier[]> => { | ||
| const envs = system.environments.filter((e) => isPresent(e.resourceSelector)); | ||
| const { deployments } = system; | ||
|
|
||
| const maybeTargetsPromises = envs.flatMap((env) => | ||
| deployments.map(async (dep) => { | ||
| const resource = await db.query.resource.findFirst({ | ||
| where: and( | ||
| eq(SCHEMA.resource.id, resourceId), | ||
| SCHEMA.resourceMatchesMetadata(db, env.resourceSelector), | ||
| SCHEMA.resourceMatchesMetadata(db, dep.resourceSelector), | ||
| ), | ||
| }); | ||
|
|
||
| if (resource == null) return null; | ||
| return { environmentId: env.id, deploymentId: dep.id }; | ||
| }), | ||
| ); | ||
|
|
||
| const targets = await Promise.all(maybeTargetsPromises).then((results) => | ||
| results.filter(isPresent), | ||
| ); | ||
|
|
||
| return targets.map((t) => ({ ...t, resourceId })); | ||
| }; | ||
|
|
||
| export const upsertReleaseTargets = async ( | ||
| db: Tx, | ||
| resource: SCHEMA.Resource, | ||
| ) => { | ||
| const workspace = await db.query.workspace.findFirst({ | ||
| where: eq(SCHEMA.workspace.id, resource.workspaceId), | ||
| with: { systems: { with: { environments: true, deployments: true } } }, | ||
| }); | ||
| if (workspace == null) throw new Error("Workspace not found"); | ||
|
|
||
| const releaseTargetInserts = await Promise.all( | ||
| workspace.systems.map((system) => | ||
| getReleaseTargetInsertsForSystem(db, resource.id, system), | ||
| ), | ||
| ).then((results) => results.flat()); | ||
|
|
||
| if (releaseTargetInserts.length === 0) return []; | ||
| return db | ||
| .insert(SCHEMA.releaseTarget) | ||
| .values(releaseTargetInserts) | ||
| .onConflictDoNothing() | ||
| .returning(); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { db } from "@ctrlplane/db/client"; | ||
| import { Channel, createWorker, getQueue } from "@ctrlplane/events"; | ||
|
|
||
| import { upsertReleaseTargets } from "../utils/upsert-release-targets.js"; | ||
|
|
||
| const queue = getQueue(Channel.EvaluateReleaseTarget); | ||
| export const newResourceWorker = createWorker( | ||
| Channel.NewResource, | ||
| ({ data: resource }) => | ||
| db.transaction(async (tx) => { | ||
| const rts = await upsertReleaseTargets(tx, resource); | ||
| await queue.addBulk( | ||
| rts.map((rt) => ({ | ||
| name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`, | ||
| data: rt, | ||
| })), | ||
| ); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
apps/event-worker/src/workers/updated-resources/dispatch-exit-hooks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import type { Tx } from "@ctrlplane/db"; | ||
| import type { ReleaseTargetIdentifier } from "@ctrlplane/rule-engine"; | ||
|
|
||
| import { and, eq, inArray, isNotNull, notInArray, or } from "@ctrlplane/db"; | ||
| import * as SCHEMA from "@ctrlplane/db/schema"; | ||
| import { handleEvent } from "@ctrlplane/job-dispatch"; | ||
| import { HookAction } from "@ctrlplane/validators/events"; | ||
|
|
||
| const getSystemsForUnmatchedEnvs = async ( | ||
| db: Tx, | ||
| previousReleaseTargets: ReleaseTargetIdentifier[], | ||
| newReleaseTargets: ReleaseTargetIdentifier[], | ||
| ) => { | ||
| const previousEnvIds = new Set<string>( | ||
| previousReleaseTargets.map((rt) => rt.environmentId), | ||
| ); | ||
| const newEnvIds = new Set<string>( | ||
| newReleaseTargets.map((rt) => rt.environmentId), | ||
| ); | ||
| const unmatchedEnvs = Array.from(previousEnvIds).filter( | ||
| (envId) => !newEnvIds.has(envId), | ||
| ); | ||
|
|
||
| const envs = await db.query.environment.findMany({ | ||
| where: inArray(SCHEMA.environment.id, unmatchedEnvs), | ||
| }); | ||
|
|
||
| return db.query.system.findMany({ | ||
| where: inArray( | ||
| SCHEMA.system.id, | ||
| envs.map((e) => e.systemId), | ||
| ), | ||
| with: { | ||
| deployments: true, | ||
| environments: { | ||
| where: and( | ||
| isNotNull(SCHEMA.environment.resourceSelector), | ||
| notInArray(SCHEMA.environment.id, unmatchedEnvs), | ||
| ), | ||
| }, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const dispatchExitHooksIfExitedSystem = async ( | ||
| db: Tx, | ||
| resource: SCHEMA.Resource, | ||
| system: { | ||
| deployments: SCHEMA.Deployment[]; | ||
| environments: SCHEMA.Environment[]; | ||
| }, | ||
| ) => { | ||
| const { deployments, environments } = system; | ||
| const matchedResource = await db.query.resource.findFirst({ | ||
| where: and( | ||
| eq(SCHEMA.resource.id, resource.id), | ||
| or( | ||
| ...environments.map((e) => | ||
| SCHEMA.resourceMatchesMetadata(db, e.resourceSelector), | ||
| ), | ||
| ), | ||
| ), | ||
| }); | ||
| if (matchedResource == null) return; | ||
|
|
||
| const events = deployments.map((deployment) => ({ | ||
| action: HookAction.DeploymentResourceRemoved, | ||
| payload: { deployment, resource }, | ||
| })); | ||
|
|
||
| const handleEventPromises = events.map(handleEvent); | ||
| await Promise.allSettled(handleEventPromises); | ||
| }; | ||
|
|
||
| export const dispatchExitHooks = async ( | ||
| db: Tx, | ||
| resource: SCHEMA.Resource, | ||
| currentReleaseTargets: ReleaseTargetIdentifier[], | ||
| newReleaseTargets: ReleaseTargetIdentifier[], | ||
| ) => { | ||
| const systems = await getSystemsForUnmatchedEnvs( | ||
| db, | ||
| currentReleaseTargets, | ||
| newReleaseTargets, | ||
| ); | ||
| const dispatchExitHooksPromises = systems.map((system) => | ||
| dispatchExitHooksIfExitedSystem(db, resource, system), | ||
| ); | ||
| await Promise.allSettled(dispatchExitHooksPromises); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { eq, inArray } from "@ctrlplane/db"; | ||
| import { db } from "@ctrlplane/db/client"; | ||
| import * as SCHEMA from "@ctrlplane/db/schema"; | ||
| import { Channel, createWorker, getQueue } from "@ctrlplane/events"; | ||
|
|
||
| import { upsertReleaseTargets } from "../../utils/upsert-release-targets.js"; | ||
| import { dispatchExitHooks } from "./dispatch-exit-hooks.js"; | ||
|
|
||
| export const updatedResourceWorker = createWorker( | ||
| Channel.UpdatedResource, | ||
| async ({ data: resource }) => | ||
| db.transaction(async (tx) => { | ||
| const currentReleaseTargets = await tx.query.releaseTarget.findMany({ | ||
| where: eq(SCHEMA.releaseTarget.resourceId, resource.id), | ||
| }); | ||
|
|
||
| const newReleaseTargets = await upsertReleaseTargets(tx, resource); | ||
| const releaseTargetsToDelete = currentReleaseTargets.filter( | ||
| (rt) => !newReleaseTargets.some((nrt) => nrt.id === rt.id), | ||
| ); | ||
|
|
||
| await tx.delete(SCHEMA.releaseTarget).where( | ||
| inArray( | ||
| SCHEMA.releaseTarget.id, | ||
| releaseTargetsToDelete.map((rt) => rt.id), | ||
| ), | ||
| ); | ||
|
|
||
| const dispatchExitHooksPromise = dispatchExitHooks( | ||
| tx, | ||
| resource, | ||
| currentReleaseTargets, | ||
| newReleaseTargets, | ||
| ); | ||
|
|
||
| const addToEvaluateQueuePromise = getQueue( | ||
| Channel.EvaluateReleaseTarget, | ||
| ).addBulk( | ||
| newReleaseTargets.map((rt) => ({ | ||
| name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`, | ||
| data: rt, | ||
| })), | ||
| ); | ||
|
|
||
| await Promise.allSettled([ | ||
| dispatchExitHooksPromise, | ||
| addToEvaluateQueuePromise, | ||
| ]); | ||
| }), | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider wrapping upsert in a transaction if partial updates are a concern.
Currently, the call uses
dbdirectly. If a failure happens later in the upsert flow, partial updates may remain in the database. You may want to ensure an all-or-nothing update by using a transaction, if atomicity is needed.