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

fix: Always dispatch resources with changed vars #227

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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);
Comment on lines +83 to +92
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure proper handling when no resources have updated variables

The code dispatches jobs for resources with updated variables:

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);

If updatedVariableResourceIds is empty, dispatchJobsForAddedResources may be called with an empty array of resource IDs. Confirm that dispatchJobsForAddedResources correctly handles an empty array to avoid unnecessary processing or potential errors.

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
Loading