-
Notifications
You must be signed in to change notification settings - Fork 1
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
fix: Move events to job dispatch and use deleteResources utility function everywhere #222
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,11 +17,11 @@ import { | |||||||||||||||||||||||||||||||||||||||||||||||||
takeFirstOrNull, | ||||||||||||||||||||||||||||||||||||||||||||||||||
} from "@ctrlplane/db"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import * as schema from "@ctrlplane/db/schema"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import { getEventsForResourceDeleted, handleEvent } from "@ctrlplane/events"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||||||||||
cancelOldReleaseJobTriggersOnJobDispatch, | ||||||||||||||||||||||||||||||||||||||||||||||||||
createJobApprovals, | ||||||||||||||||||||||||||||||||||||||||||||||||||
createReleaseJobTriggers, | ||||||||||||||||||||||||||||||||||||||||||||||||||
deleteResources, | ||||||||||||||||||||||||||||||||||||||||||||||||||
dispatchReleaseJobTriggers, | ||||||||||||||||||||||||||||||||||||||||||||||||||
isPassingAllPoliciesExceptNewerThanLastActive, | ||||||||||||||||||||||||||||||||||||||||||||||||||
isPassingNoPendingJobsPolicy, | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -577,20 +577,11 @@ export const resourceRouter = createTRPCRouter({ | |||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.input(z.array(z.string().uuid())) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.mutation(async ({ ctx, input }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const resources = await ctx.db.query.resource.findMany({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
where: inArray(schema.resource.id, input), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
const events = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
await Promise.allSettled(resources.map(getEventsForResourceDeleted)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
).flatMap((r) => (r.status === "fulfilled" ? r.value : [])); | ||||||||||||||||||||||||||||||||||||||||||||||||||
await Promise.allSettled(events.map(handleEvent)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return ctx.db | ||||||||||||||||||||||||||||||||||||||||||||||||||
.delete(schema.resource) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.where(inArray(schema.resource.id, input)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.returning(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
}), | ||||||||||||||||||||||||||||||||||||||||||||||||||
.mutation(async ({ ctx, input }) => | ||||||||||||||||||||||||||||||||||||||||||||||||||
ctx.db.query.resource | ||||||||||||||||||||||||||||||||||||||||||||||||||
.findMany({ where: inArray(schema.resource.id, input) }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
.then((resources) => deleteResources(ctx.db, resources)), | ||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+580
to
+584
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider wrapping operations in a transaction and adding error handling While the implementation correctly uses the new
Here's a suggested implementation: .mutation(async ({ ctx, input }) =>
- ctx.db.query.resource
- .findMany({ where: inArray(schema.resource.id, input) })
- .then((resources) => deleteResources(ctx.db, resources)),
+ ctx.db.transaction(async (tx) => {
+ const resources = await tx.query.resource.findMany({
+ where: inArray(schema.resource.id, input),
+ columns: {
+ id: true,
+ kind: true,
+ name: true,
+ workspaceId: true
+ }
+ });
+
+ if (resources.length !== input.length) {
+ throw new Error('Some resources not found');
+ }
+
+ return deleteResources(tx, resources);
+ }), 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
metadataKeys: protectedProcedure | ||||||||||||||||||||||||||||||||||||||||||||||||||
.meta({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./resource-removed.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export * from "./environment-deleted.js"; | ||
export * from "./deployment-deleted.js"; | ||
export * from "./target-deleted.js"; | ||
export * from "./resource-deleted.js"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ export * from "./policies/concurrency-policy.js"; | |
export * from "./policies/release-window.js"; | ||
export * from "./environment-creation.js"; | ||
export * from "./pending-job-checker.js"; | ||
export * from "./events/index.js"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Event module integration is properly structured but has a circular dependency The events module is well-structured with proper exports and imports, but there's a circular dependency that needs attention:
Suggested fix:
🔗 Analysis chainVerify the event module integration. Let's ensure this export is properly integrated: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Verify the events module integration and usage
# Check if the events/index.js file exists
echo "Checking for events/index.js file..."
fd --type f "index.js" packages/job-dispatch/src/events
# Check for potential circular dependencies
echo "Checking for potential circular dependencies..."
rg -l "from \"@ctrlplane/job-dispatch\"" packages/job-dispatch/src/events
# Check usage of the newly exported events
echo "Checking usage of exported events..."
rg --type ts "from \"@ctrlplane/job-dispatch\".*events" -A 3
Length of output: 523 Script: #!/bin/bash
# Let's verify the events module structure and integration more thoroughly
# Check the content of events/index.ts (not .js)
echo "Checking for events/index.ts file..."
fd --type f "index.ts" packages/job-dispatch/src/events
echo "Checking content of events/index.ts if exists..."
fd --type f "index.ts" packages/job-dispatch/src/events --exec cat {}
echo "Checking for all files in events directory..."
fd . packages/job-dispatch/src/events
# Check the specific circular dependency found
echo "Checking content of resource-removed.ts..."
cat packages/job-dispatch/src/events/handlers/resource-removed.ts
# Check if events are imported elsewhere in the project
echo "Checking broader usage of events..."
rg "from.*events" packages/job-dispatch/src
Length of output: 4003 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import { | |
import { logger } from "@ctrlplane/logger"; | ||
import { variablesAES256 } from "@ctrlplane/secrets"; | ||
|
||
import { getEventsForResourceDeleted, handleEvent } from "./events/index.js"; | ||
import { dispatchJobsForNewResources } from "./new-resource.js"; | ||
|
||
const log = logger.child({ label: "upsert-resources" }); | ||
|
@@ -320,10 +321,7 @@ export const upsertResources = async ( | |
); | ||
|
||
if (resourcesToDelete.length > 0) { | ||
await deleteResources( | ||
tx, | ||
resourcesToDelete.map((r) => r.id), | ||
).catch((err) => { | ||
await deleteResources(tx, resourcesToDelete).catch((err) => { | ||
log.error("Error deleting resources", { error: err }); | ||
throw err; | ||
}); | ||
|
@@ -346,6 +344,12 @@ export const upsertResources = async ( | |
* @param tx - The transaction to use. | ||
* @param resourceIds - The ids of the resources to delete. | ||
*/ | ||
export const deleteResources = async (tx: Tx, resourceIds: string[]) => { | ||
export const deleteResources = async (tx: Tx, resources: Resource[]) => { | ||
const eventsPromises = Promise.all( | ||
resources.map(getEventsForResourceDeleted), | ||
); | ||
const events = await eventsPromises.then((res) => res.flat()); | ||
await Promise.all(events.map(handleEvent)); | ||
Comment on lines
+347
to
+352
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling in The Apply this diff to add error handling: export const deleteResources = async (tx: Tx, resources: Resource[]) => {
+ try {
const eventsPromises = Promise.all(
resources.map(getEventsForResourceDeleted),
);
const events = await eventsPromises.then((res) => res.flat());
await Promise.all(events.map(handleEvent));
const resourceIds = resources.map((r) => r.id);
await tx.delete(resource).where(inArray(resource.id, resourceIds));
+ } catch (err) {
+ log.error("Error in deleteResources", { error: err });
+ throw err;
+ }
};
|
||
const resourceIds = resources.map((r) => r.id); | ||
await tx.delete(resource).where(inArray(resource.id, resourceIds)); | ||
}; |
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
Add error handling and improve logging precision
While the new implementation correctly checks for resource existence before deletion, there are a few concerns:
Consider this improved implementation:
📝 Committable suggestion