diff --git a/apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts b/apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts index 5768603c9..d485cb343 100644 --- a/apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts +++ b/apps/webservice/src/app/api/v1/resources/[resourceId]/route.ts @@ -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); }); diff --git a/packages/job-dispatch/src/resource/dispatch-resource.ts b/packages/job-dispatch/src/resource/dispatch-resource.ts index 19cd50da7..21b26b953 100644 --- a/packages/job-dispatch/src/resource/dispatch-resource.ts +++ b/packages/job-dispatch/src/resource/dispatch-resource.ts @@ -55,6 +55,7 @@ export async function dispatchJobsForAddedResources( resourceIds: string[], envId: string, ): Promise { + if (resourceIds.length === 0) return; log.info("Dispatching jobs for added resources", { resourceIds, envId }); const environment = await getEnvironmentWithReleaseChannels(db, envId); diff --git a/packages/job-dispatch/src/resource/insert-resource-variables.ts b/packages/job-dispatch/src/resource/insert-resource-variables.ts index 9743f246b..8b15b50f9 100644 --- a/packages/job-dispatch/src/resource/insert-resource-variables.ts +++ b/packages/job-dispatch/src/resource/insert-resource-variables.ts @@ -13,7 +13,7 @@ export type ResourceWithVariables = Resource & { export const insertResourceVariables = async ( tx: Tx, resources: ResourceWithVariables[], -) => { +): Promise> => { const resourceIds = resources.map(({ id }) => id); const existingVariables = await tx .select() @@ -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) @@ -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); }; diff --git a/packages/job-dispatch/src/resource/upsert.ts b/packages/job-dispatch/src/resource/upsert.ts index b26de3f63..408362c45 100644 --- a/packages/job-dispatch/src/resource/upsert.ts +++ b/packages/job-dispatch/src/resource/upsert.ts @@ -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), ]); @@ -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 ?? []; @@ -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, ); }