Skip to content

Commit

Permalink
fix: Move events to job dispatch and use deleteResources utility func…
Browse files Browse the repository at this point in the history
…tion everywhere (#222)
  • Loading branch information
adityachoudhari26 authored Nov 20, 2024
1 parent 6542ac6 commit eeae5c9
Show file tree
Hide file tree
Showing 26 changed files with 60 additions and 180 deletions.
1 change: 0 additions & 1 deletion apps/jobs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ COPY packages/validators/package.json ./packages/validators/package.json
COPY packages/logger/package.json ./packages/logger/package.json
COPY packages/job-dispatch/package.json ./packages/job-dispatch/package.json
COPY packages/secrets/package.json ./packages/secrets/package.json
COPY packages/events/package.json ./packages/events/package.json

COPY apps/jobs/package.json ./apps/jobs/package.json

Expand Down
1 change: 0 additions & 1 deletion apps/jobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
},
"dependencies": {
"@ctrlplane/db": "workspace:*",
"@ctrlplane/events": "workspace:*",
"@ctrlplane/job-dispatch": "workspace:*",
"@ctrlplane/logger": "workspace:*",
"@ctrlplane/validators": "workspace:*",
Expand Down
5 changes: 4 additions & 1 deletion apps/jobs/src/expired-env-checker/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { inArray, lte } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import * as SCHEMA from "@ctrlplane/db/schema";
import { getEventsForEnvironmentDeleted, handleEvent } from "@ctrlplane/events";
import {
getEventsForEnvironmentDeleted,
handleEvent,
} from "@ctrlplane/job-dispatch";

export const run = async () => {
const expiredEnvironments = await db
Expand Down
9 changes: 6 additions & 3 deletions apps/pty-proxy/src/controller/sockets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ms from "ms";

import { eq } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import * as SCHEMA from "@ctrlplane/db/schema";
import { deleteResources } from "@ctrlplane/job-dispatch";
import { logger } from "@ctrlplane/logger";

Expand All @@ -26,9 +28,10 @@ setInterval(() => {

agent.socket.close(1000, "Agent connection timed out");
agents.delete(agentId);
deleteResources(db, [agentId]).then(() => {
logger.info("Deleted stale agent resource", { agentId });
});
db.query.resource
.findFirst({ where: eq(SCHEMA.resource.id, agentId) })
.then((resource) => resource && deleteResources(db, [resource]))
.then(() => logger.info("Deleted stale agent resource", { agentId }));
}
}
}, ms("1m"));
1 change: 0 additions & 1 deletion apps/webservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ COPY packages/job-dispatch/package.json ./packages/job-dispatch/package.json
COPY packages/ui/package.json ./packages/ui/package.json
COPY packages/logger/package.json ./packages/logger/package.json
COPY packages/secrets/package.json ./packages/secrets/package.json
COPY packages/events/package.json ./packages/events/package.json

COPY apps/webservice/package.json ./apps/webservice/package.json

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const EditHookDialog: React.FC<EditHookDialogProps> = ({

const defaultValues = {
...hook,
action: hook.action as HookAction,
jobAgentId: hook.runhook?.runbook.jobAgentId ?? null,
jobAgentConfig: hook.runhook?.runbook.jobAgentConfig ?? null,
};
Expand Down Expand Up @@ -144,7 +145,7 @@ export const EditHookDialog: React.FC<EditHookDialogProps> = ({
className="items-center justify-start gap-2 px-2"
>
<IconSelector className="h-4 w-4" />
{value === "" ? "Select action..." : value}
{value}
</Button>
</PopoverTrigger>
<PopoverContent align="start" className="w-[462px] p-0">
Expand Down
1 change: 0 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"dependencies": {
"@ctrlplane/auth": "workspace:*",
"@ctrlplane/db": "workspace:*",
"@ctrlplane/events": "workspace:*",
"@ctrlplane/job-dispatch": "workspace:*",
"@ctrlplane/logger": "workspace:*",
"@ctrlplane/secrets": "workspace:*",
Expand Down
5 changes: 4 additions & 1 deletion packages/api/src/router/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import {
updateReleaseChannel,
workspace,
} from "@ctrlplane/db/schema";
import { getEventsForDeploymentDeleted, handleEvent } from "@ctrlplane/events";
import {
getEventsForDeploymentDeleted,
handleEvent,
} from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";
import { JobStatus } from "@ctrlplane/validators/jobs";

Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/router/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import {
system,
updateEnvironment,
} from "@ctrlplane/db/schema";
import { getEventsForEnvironmentDeleted, handleEvent } from "@ctrlplane/events";
import { dispatchJobsForNewResources } from "@ctrlplane/job-dispatch";
import {
dispatchJobsForNewResources,
getEventsForEnvironmentDeleted,
handleEvent,
} from "@ctrlplane/job-dispatch";
import { Permission } from "@ctrlplane/validators/auth";
import {
ComparisonOperator,
Expand Down
21 changes: 6 additions & 15 deletions packages/api/src/router/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)),
),

metadataKeys: protectedProcedure
.meta({
Expand Down
35 changes: 4 additions & 31 deletions packages/api/src/router/runbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,16 @@ export const runbookRouter = createTRPCRouter({
.on({ type: "system", id: input }),
})
.input(z.string().uuid())
.query(({ ctx, input }) => {
return ctx.db.query.runbook.findMany({
.query(({ ctx, input }) =>
ctx.db.query.runbook.findMany({
where: eq(SCHEMA.runbook.systemId, input),
with: {
runhooks: { with: { hook: true } },
jobAgent: true,
variables: true,
},
});
// return ctx.db
// .select()
// .from(SCHEMA.runbook)
// .leftJoin(
// SCHEMA.runbookVariable,
// eq(SCHEMA.runbookVariable.runbookId, SCHEMA.runbook.id),
// )
// .leftJoin(
// SCHEMA.jobAgent,
// eq(SCHEMA.runbook.jobAgentId, SCHEMA.jobAgent.id),
// )
// .leftJoin(
// SCHEMA.runbookJobTrigger,
// eq(SCHEMA.runbook.id, SCHEMA.runbookJobTrigger.runbookId),
// )
// .leftJoin(SCHEMA.job, eq(SCHEMA.runbookJobTrigger.jobId, SCHEMA.job.id))
// .where(eq(SCHEMA.runbook.systemId, input))
// .then((rbs) =>
// _.chain(rbs)
// .groupBy((rb) => rb.runbook.id)
// .map((rb) => ({
// ...rb[0]!.runbook,
// variables: rb.map((v) => v.runbook_variable).filter(isPresent),
// jobAgent: rb[0]!.job_agent,
// }))
// .value(),
// );
}),
}),
),

create: protectedProcedure
.meta({
Expand Down
4 changes: 1 addition & 3 deletions packages/db/src/schema/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ export const runhook = pgTable(
.notNull()
.references(() => runbook.id, { onDelete: "cascade" }),
},
(t) => ({
uniq: uniqueIndex().on(t.hookId, t.runbookId),
}),
(t) => ({ uniq: uniqueIndex().on(t.hookId, t.runbookId) }),
);

export const runhookRelations = relations(runhook, ({ one }) => ({
Expand Down
10 changes: 0 additions & 10 deletions packages/events/eslint.config.js

This file was deleted.

38 changes: 0 additions & 38 deletions packages/events/package.json

This file was deleted.

1 change: 0 additions & 1 deletion packages/events/src/handlers/index.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/events/tsconfig.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/job-dispatch/src/events/handlers/index.ts
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
Expand Up @@ -3,7 +3,8 @@ import type { ResourceRemoved } from "@ctrlplane/validators/events";
import { and, eq } from "@ctrlplane/db";
import { db } from "@ctrlplane/db/client";
import * as SCHEMA from "@ctrlplane/db/schema";
import { dispatchRunbook } from "@ctrlplane/job-dispatch";

import { dispatchRunbook } from "../../job-dispatch.js";

export const handleResourceRemoved = async (event: ResourceRemoved) => {
const { resource, deployment } = event.payload;
Expand Down
File renamed without changes.
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";
1 change: 1 addition & 0 deletions packages/job-dispatch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
14 changes: 9 additions & 5 deletions packages/job-dispatch/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down Expand Up @@ -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;
});
Expand All @@ -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));
const resourceIds = resources.map((r) => r.id);
await tx.delete(resource).where(inArray(resource.id, resourceIds));
};
Loading

0 comments on commit eeae5c9

Please sign in to comment.