only return last v1 deployment in the shared queue consumer

This commit is contained in:
nicktrn
2025-04-14 12:18:47 +01:00
parent faf5d01b2c
commit 3238514124
5 changed files with 46 additions and 18 deletions
@@ -56,7 +56,7 @@ export class TestPresenter extends BasePresenter {
JOIN ${sqlDatabaseSchema}."BackgroundWorkerTask" bwt ON bwt."workerId" = latest_workers.id
ORDER BY slug ASC;`;
} else {
const currentDeployment = await findCurrentWorkerDeployment(envId);
const currentDeployment = await findCurrentWorkerDeployment({ environmentId: envId });
return currentDeployment?.worker?.tasks ?? [];
}
}
@@ -88,7 +88,7 @@ export class TestTaskPresenter {
}: TestTaskOptions): Promise<TestTaskResult> {
let task: BackgroundWorkerTaskSlim | null = null;
if (environment.type !== "DEVELOPMENT") {
const deployment = await findCurrentWorkerDeployment(environment.id);
const deployment = await findCurrentWorkerDeployment({ environmentId: environment.id });
if (deployment) {
task = deployment.worker?.tasks.find((t) => t.slug === taskIdentifier) ?? null;
}
@@ -612,7 +612,10 @@ export class SharedQueueConsumer {
? await getWorkerDeploymentFromWorkerTask(existingTaskRun.lockedById)
: existingTaskRun.lockedToVersionId
? await getWorkerDeploymentFromWorker(existingTaskRun.lockedToVersionId)
: await findCurrentWorkerDeployment(existingTaskRun.runtimeEnvironmentId);
: await findCurrentWorkerDeployment({
environmentId: existingTaskRun.runtimeEnvironmentId,
type: "V1",
});
});
const worker = deployment?.worker;
@@ -1,5 +1,5 @@
import type { Prettify } from "@trigger.dev/core";
import { BackgroundWorker, RunEngineVersion, WorkerDeployment } from "@trigger.dev/database";
import { BackgroundWorker, RunEngineVersion, WorkerDeploymentType } from "@trigger.dev/database";
import {
CURRENT_DEPLOYMENT_LABEL,
CURRENT_UNMANAGED_DEPLOYMENT_LABEL,
@@ -56,10 +56,23 @@ type WorkerDeploymentWithWorkerTasks = Prisma.WorkerDeploymentGetPayload<{
};
}>;
export async function findCurrentWorkerDeployment(
environmentId: string,
label = CURRENT_DEPLOYMENT_LABEL
): Promise<WorkerDeploymentWithWorkerTasks | undefined> {
/**
* Finds the current worker deployment for a given environment.
*
* @param environmentId - The ID of the environment to find the current worker deployment for.
* @param label - The label of the current worker deployment to find.
* @param type - The type of worker deployment to find. If the current deployment is NOT of this type,
* we will return the latest deployment of the given type.
*/
export async function findCurrentWorkerDeployment({
environmentId,
label = CURRENT_DEPLOYMENT_LABEL,
type,
}: {
environmentId: string;
label?: string;
type?: WorkerDeploymentType;
}): Promise<WorkerDeploymentWithWorkerTasks | undefined> {
const promotion = await prisma.workerDeploymentPromotion.findFirst({
where: {
environmentId,
@@ -93,16 +106,19 @@ export async function findCurrentWorkerDeployment(
return undefined;
}
if (promotion.deployment.type === "V1") {
// This is a run engine v1 deployment, so return it
if (!type) {
return promotion.deployment;
}
// We need to get the latest run engine v1 deployment
const latestV1Deployment = await prisma.workerDeployment.findFirst({
if (promotion.deployment.type === type) {
return promotion.deployment;
}
// We need to get the latest deployment of the given type
const latestDeployment = await prisma.workerDeployment.findFirst({
where: {
environmentId,
type: "V1",
type,
},
orderBy: {
id: "desc",
@@ -127,11 +143,11 @@ export async function findCurrentWorkerDeployment(
},
});
if (!latestV1Deployment) {
if (!latestDeployment) {
return undefined;
}
return latestV1Deployment;
return latestDeployment;
}
export async function getCurrentWorkerDeploymentEngineVersion(
@@ -162,7 +178,11 @@ export async function getCurrentWorkerDeploymentEngineVersion(
export async function findCurrentUnmanagedWorkerDeployment(
environmentId: string
): Promise<WorkerDeploymentWithWorkerTasks | undefined> {
return await findCurrentWorkerDeployment(environmentId, CURRENT_UNMANAGED_DEPLOYMENT_LABEL);
return await findCurrentWorkerDeployment({
environmentId,
label: CURRENT_UNMANAGED_DEPLOYMENT_LABEL,
type: "UNMANAGED",
});
}
export async function findCurrentWorkerFromEnvironment(
@@ -183,7 +203,10 @@ export async function findCurrentWorkerFromEnvironment(
});
return latestDevWorker;
} else {
const deployment = await findCurrentWorkerDeployment(environment.id, label);
const deployment = await findCurrentWorkerDeployment({
environmentId: environment.id,
label,
});
return deployment?.worker ?? null;
}
}
@@ -73,7 +73,9 @@ export class TriggerScheduledTaskService extends BaseService {
if (instance.environment.type !== "DEVELOPMENT") {
// Get the current backgroundWorker for this environment
const currentWorkerDeployment = await findCurrentWorkerDeployment(instance.environment.id);
const currentWorkerDeployment = await findCurrentWorkerDeployment({
environmentId: instance.environment.id,
});
if (!currentWorkerDeployment) {
logger.debug("No current worker deployment found, skipping task trigger", {