Skip to content

Commit

Permalink
fix: Always dispatch resources with changed vars (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Nov 23, 2024
1 parent 6e1fe9e commit c1535f0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ export const PATCH = request()
{ status: 404 },
);

const { updated } = await upsertResources(db, [_.merge(resource, body)]);
const res = updated.at(0);
const { all } = await upsertResources(db, [_.merge(resource, body)]);
const res = all.at(0);
if (res == null) throw new Error("Failed to update resource");
return NextResponse.json(res);
});
Expand Down
1 change: 1 addition & 0 deletions packages/job-dispatch/src/resource/dispatch-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export async function dispatchJobsForAddedResources(
resourceIds: string[],
envId: string,
): Promise<void> {
if (resourceIds.length === 0) return;
log.info("Dispatching jobs for added resources", { resourceIds, envId });

const environment = await getEnvironmentWithReleaseChannels(db, envId);
Expand Down
12 changes: 9 additions & 3 deletions packages/job-dispatch/src/resource/insert-resource-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ResourceWithVariables = Resource & {
export const insertResourceVariables = async (
tx: Tx,
resources: ResourceWithVariables[],
) => {
): Promise<Set<string>> => {
const resourceIds = resources.map(({ id }) => id);
const existingVariables = await tx
.select()
Expand All @@ -31,7 +31,7 @@ export const insertResourceVariables = async (
})),
);

if (resourceVariablesValues.length === 0) return;
if (resourceVariablesValues.length === 0) return new Set();

const updatedVariables = await tx
.insert(schema.resourceVariable)
Expand Down Expand Up @@ -66,5 +66,11 @@ export const insertResourceVariables = async (
(a.value !== b.value || a.sensitive !== b.sensitive),
);

return { created, deleted, updated };
const updatedResourceIds = [
...created.map((r) => r.resourceId),
...deleted.map((r) => r.resourceId),
...updated.map((r) => r.resourceId),
];

return new Set(updatedResourceIds);
};
16 changes: 14 additions & 2 deletions packages/job-dispatch/src/resource/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const upsertResources = async (
}));

log.debug("Inserting resource metadata and variables");
await Promise.all([
const [, updatedVariableResourceIds] = await Promise.all([
insertResourceMetadata(tx, resourcesWithId),
insertResourceVariables(tx, resourcesWithId),
]);
Expand All @@ -80,6 +80,16 @@ export const upsertResources = async (
resources: e.resources.map((r) => r.identifier),
})),
});
const envVariableChangePromises = envsAfterInsert.map((env) =>
dispatchJobsForAddedResources(
tx,
env.resources
.filter((r) => updatedVariableResourceIds.has(r.id))
.map((r) => r.id),
env.id,
),
);
await Promise.all(envVariableChangePromises);
const changedEnvs = envsAfterInsert.map((env) => {
const beforeEnv = envsBeforeInsert.find((e) => e.id === env.id);
const beforeResources = beforeEnv?.resources ?? [];
Expand Down Expand Up @@ -110,7 +120,9 @@ export const upsertResources = async (
});
await dispatchJobsForAddedResources(
tx,
env.addedResources.map((r) => r.id),
env.addedResources
.map((r) => r.id)
.filter((r) => !updatedVariableResourceIds.has(r)),
env.id,
);
}
Expand Down

0 comments on commit c1535f0

Please sign in to comment.