diff --git a/apps/event-worker/src/utils/upsert-release-targets.ts b/apps/event-worker/src/utils/upsert-release-targets.ts index ed276e287..29aa963a0 100644 --- a/apps/event-worker/src/utils/upsert-release-targets.ts +++ b/apps/event-worker/src/utils/upsert-release-targets.ts @@ -55,10 +55,10 @@ export const upsertReleaseTargets = async ( ), ).then((results) => results.flat()); - // if (releaseTargetInserts.length === 0) return []; - // return db - // .insert(SCHEMA.releaseTarget) - // .values(releaseTargetInserts) - // .onConflictDoNothing() - // .returning();`` + if (releaseTargetInserts.length === 0) return []; + return db + .insert(SCHEMA.releaseTarget) + .values(releaseTargetInserts) + .onConflictDoNothing() + .returning(); }; diff --git a/apps/event-worker/src/workers/new-resource.ts b/apps/event-worker/src/workers/new-resource.ts index 49b55c1da..764c0877d 100644 --- a/apps/event-worker/src/workers/new-resource.ts +++ b/apps/event-worker/src/workers/new-resource.ts @@ -7,16 +7,11 @@ const queue = getQueue(Channel.EvaluateReleaseTarget); export const newResourceWorker = createWorker( Channel.NewResource, async ({ data: resource }) => { - console.log("new resource", resource); - return; - // db.transaction(async (tx) => { - // const rts = await upsertReleaseTargets(tx, resource); - // await queue.addBulk( - // rts.map((rt) => ({ - // name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`, - // data: rt, - // })), - // ); - // }), + const rts = await upsertReleaseTargets(db, resource); + const jobs = rts.map((rt) => ({ + name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`, + data: rt, + })); + await queue.addBulk(jobs); }, ); diff --git a/apps/event-worker/src/workers/updated-resources/index.ts b/apps/event-worker/src/workers/updated-resources/index.ts index 27aa9f375..234f830bb 100644 --- a/apps/event-worker/src/workers/updated-resources/index.ts +++ b/apps/event-worker/src/workers/updated-resources/index.ts @@ -16,53 +16,43 @@ export const updatedResourceWorker = createWorker( where: eq(SCHEMA.releaseTarget.resourceId, resource.id), }); - await upsertReleaseTargets(db, resource); - - const endTime = performance.now(); - - logger.info( - `finished processing updated resource ${resource.id} in ${endTime - startTime}ms`, + const upsertedReleaseTargets = await upsertReleaseTargets(db, resource); + const releaseTargetsToDelete = currentReleaseTargets.filter( + (rt) => !upsertedReleaseTargets.some((nrt) => nrt.id === rt.id), ); - return; - - // db.transaction(async (tx) => { - // const currentReleaseTargets = await tx.query.releaseTarget.findMany({ - // where: eq(SCHEMA.releaseTarget.resourceId, resource.id), - // }); + await db.delete(SCHEMA.releaseTarget).where( + inArray( + SCHEMA.releaseTarget.id, + releaseTargetsToDelete.map((rt) => rt.id), + ), + ); - // const newReleaseTargets = await upsertReleaseTargets(tx, resource); - // const releaseTargetsToDelete = currentReleaseTargets.filter( - // (rt) => !newReleaseTargets.some((nrt) => nrt.id === rt.id), - // ); + const dispatchExitHooksPromise = dispatchExitHooks( + db, + resource, + currentReleaseTargets, + upsertedReleaseTargets, + ); - // await tx.delete(SCHEMA.releaseTarget).where( - // inArray( - // SCHEMA.releaseTarget.id, - // releaseTargetsToDelete.map((rt) => rt.id), - // ), - // ); + const addToEvaluateQueuePromise = getQueue( + Channel.EvaluateReleaseTarget, + ).addBulk( + upsertedReleaseTargets.map((rt) => ({ + name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`, + data: rt, + })), + ); - // const dispatchExitHooksPromise = dispatchExitHooks( - // tx, - // resource, - // currentReleaseTargets, - // newReleaseTargets, - // ); + await Promise.allSettled([ + dispatchExitHooksPromise, + addToEvaluateQueuePromise, + ]); - // const addToEvaluateQueuePromise = getQueue( - // Channel.EvaluateReleaseTarget, - // ).addBulk( - // newReleaseTargets.map((rt) => ({ - // name: `${rt.resourceId}-${rt.environmentId}-${rt.deploymentId}`, - // data: rt, - // })), - // ); + const endTime = performance.now(); - // await Promise.allSettled([ - // dispatchExitHooksPromise, - // addToEvaluateQueuePromise, - // ]); - // }); + logger.info( + `finished processing updated resource ${resource.id} in ${endTime - startTime}ms`, + ); }, );