Skip to content

fix: efficient task trigger queue updates #1489

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 19 additions & 21 deletions apps/webapp/app/v3/services/completeAttempt.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,28 @@ export class CompleteAttemptService extends BaseService {
taskRunAttempt: NonNullable<FoundAttempt>,
env?: AuthenticatedEnvironment
): Promise<"COMPLETED"> {
await $transaction(this._prisma, async (tx) => {
await tx.taskRunAttempt.update({
where: { id: taskRunAttempt.id },
data: {
status: "COMPLETED",
completedAt: new Date(),
output: completion.output,
outputType: completion.outputType,
usageDurationMs: completion.usage?.durationMs,
taskRun: {
update: {
output: completion.output,
outputType: completion.outputType,
},
await this._prisma.taskRunAttempt.update({
where: { id: taskRunAttempt.id },
data: {
status: "COMPLETED",
completedAt: new Date(),
output: completion.output,
outputType: completion.outputType,
usageDurationMs: completion.usage?.durationMs,
taskRun: {
update: {
output: completion.output,
outputType: completion.outputType,
},
},
});
},
});

const finalizeService = new FinalizeTaskRunService(tx);
await finalizeService.call({
id: taskRunAttempt.taskRunId,
status: "COMPLETED_SUCCESSFULLY",
completedAt: new Date(),
});
const finalizeService = new FinalizeTaskRunService();
await finalizeService.call({
id: taskRunAttempt.taskRunId,
status: "COMPLETED_SUCCESSFULLY",
completedAt: new Date(),
});

// Now we need to "complete" the task run event/span
Expand Down
58 changes: 38 additions & 20 deletions apps/webapp/app/v3/services/triggerTask.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,40 @@ export class TriggerTaskService extends BaseService {
},
});

const existingConcurrencyLimit =
typeof taskQueue?.concurrencyLimit === "number"
? taskQueue.concurrencyLimit
: undefined;

if (taskQueue) {
taskQueue = await tx.taskQueue.update({
where: {
id: taskQueue.id,
},
data: {
concurrencyLimit,
rateLimit: body.options.queue.rateLimit,
},
});
if (existingConcurrencyLimit !== concurrencyLimit) {
taskQueue = await tx.taskQueue.update({
where: {
id: taskQueue.id,
},
data: {
concurrencyLimit:
typeof concurrencyLimit === "number" ? concurrencyLimit : null,
rateLimit: body.options.queue.rateLimit,
},
});

if (typeof taskQueue.concurrencyLimit === "number") {
await marqs?.updateQueueConcurrencyLimits(
environment,
taskQueue.name,
taskQueue.concurrencyLimit
);
} else {
await marqs?.removeQueueConcurrencyLimits(environment, taskQueue.name);
}
}
} else {
const queueId = generateFriendlyId("queue");

taskQueue = await tx.taskQueue.create({
data: {
friendlyId: generateFriendlyId("queue"),
friendlyId: queueId,
name: queueName,
concurrencyLimit,
runtimeEnvironmentId: environment.id,
Expand All @@ -436,16 +456,14 @@ export class TriggerTaskService extends BaseService {
type: "NAMED",
},
});
}

if (typeof taskQueue.concurrencyLimit === "number") {
await marqs?.updateQueueConcurrencyLimits(
environment,
taskQueue.name,
taskQueue.concurrencyLimit
);
} else {
await marqs?.removeQueueConcurrencyLimits(environment, taskQueue.name);
if (typeof taskQueue.concurrencyLimit === "number") {
await marqs?.updateQueueConcurrencyLimits(
environment,
taskQueue.name,
taskQueue.concurrencyLimit
);
}
}
}

Expand Down Expand Up @@ -599,7 +617,7 @@ export class TriggerTaskService extends BaseService {

const filename = `${pathPrefix}/payload.json`;

await uploadToObjectStore(filename, packet.data, packet.dataType, environment);
await uploadPacketToObjectStore(filename, packet.data, packet.dataType, environment);
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Inconsistency found in method renaming

The method is still named uploadToObjectStore in both files, but it's being called as uploadPacketToObjectStore in the code snippet. This indicates a mismatch that needs to be corrected.

  • apps/webapp/app/v3/r2.server.ts: Method is defined as uploadToObjectStore
  • apps/webapp/app/v3/services/triggerTask.server.ts: Imports and attempts to use uploadPacketToObjectStore but imports uploadToObjectStore
🔗 Analysis chain

Verify all call sites for renamed upload method

The rename from uploadToObjectStore to uploadPacketToObjectStore improves clarity, but we should verify all call sites have been updated.

Let's check for any remaining references to the old method name:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any remaining references to the old method name
rg "uploadToObjectStore"

# Search for any imports of the old method name
ast-grep --pattern 'import { $$$, uploadToObjectStore, $$$ } from'

Length of output: 376


Script:

#!/bin/bash
# Let's check the implementation and usage in both files
echo "=== Content of r2.server.ts ==="
cat apps/webapp/app/v3/r2.server.ts

echo -e "\n=== Content of triggerTask.server.ts ==="
cat apps/webapp/app/v3/services/triggerTask.server.ts

Length of output: 26530


return {
data: filename,
Expand Down
2 changes: 1 addition & 1 deletion references/v3-catalog/src/trigger/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const batchChildTask = task({
retry: {
maxAttempts: 2,
},
run: async (payload: string, { ctx }) => {
run: async (payload: any, { ctx }) => {
logger.info("Processing child task", { payload });

await wait.for({ seconds: 1 });
Expand Down
Loading