Skip to content

Only start an attempt if not finished. Send message to worker if pending executing #2377

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

Merged
merged 3 commits into from
Aug 11, 2025
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 @@ -41,7 +41,12 @@ import { runStatusFromError, ServiceValidationError } from "../errors.js";
import { sendNotificationToWorker } from "../eventBus.js";
import { getMachinePreset, machinePresetFromName } from "../machinePresets.js";
import { retryOutcomeFromCompletion } from "../retrying.js";
import { isExecuting, isInitialState } from "../statuses.js";
import {
isExecuting,
isFinishedOrPendingFinished,
isInitialState,
isPendingExecuting,
} from "../statuses.js";
import { RunEngineOptions } from "../types.js";
import { BatchSystem } from "./batchSystem.js";
import { DelayedRunSystem } from "./delayedRunSystem.js";
Expand Down Expand Up @@ -345,8 +350,8 @@ export class RunAttemptSystem {
span.setAttribute("taskRunId", taskRun.id);
span.setAttribute("taskRunFriendlyId", taskRun.friendlyId);

if (taskRun.status === "CANCELED") {
throw new ServiceValidationError("Task run is cancelled", 400);
if (isFinishedOrPendingFinished(latestSnapshot.executionStatus)) {
throw new ServiceValidationError("Task run is already finished", 400);
}

if (!taskRun.lockedById) {
Expand Down Expand Up @@ -1321,7 +1326,10 @@ export class RunAttemptSystem {
});

//if executing, we need to message the worker to cancel the run and put it into `PENDING_CANCEL` status
if (isExecuting(latestSnapshot.executionStatus)) {
if (
isExecuting(latestSnapshot.executionStatus) ||
isPendingExecuting(latestSnapshot.executionStatus)
) {
const newSnapshot = await this.executionSnapshotSystem.createExecutionSnapshot(prisma, {
run,
snapshot: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe("RunEngine cancelling", () => {
cancelledEventData.push(result);
});

//todo call completeAttempt (this will happen from the worker)
// call completeAttempt manually (this will happen from the worker)
const completeResult = await engine.completeRunAttempt({
runId: parentRun.id,
snapshotId: executionData!.snapshot.id,
Expand Down Expand Up @@ -289,13 +289,6 @@ describe("RunEngine cancelling", () => {
prisma
);

//dequeue the run
await setTimeout(500);
const dequeued = await engine.dequeueFromWorkerQueue({
consumerId: "test_12345",
workerQueue: "main",
});

let cancelledEventData: EventBusEventArgs<"runCancelled">[0][] = [];
engine.eventBus.on("runCancelled", (result) => {
cancelledEventData.push(result);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-v3/src/entryPoints/managed/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class RunExecution {
return;
}

await this.exitTaskRunProcessWithoutFailingRun({ flush: true, reason: "re-queued" });
await this.exitTaskRunProcessWithoutFailingRun({ flush: true, reason: "already-finished" });
return;
}
case "QUEUED_EXECUTING":
Expand Down
Loading