Skip to content

Commit

Permalink
Better cancelled execution creation
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Mar 16, 2024
1 parent a00eb2d commit ffda869
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/Worker/Internal/ContextWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ public function submit(Task $task, ?Cancellation $cancellation = null): Executio
throw new StatusError("The worker has been shut down");
}

if ($cancellation?->isRequested()) {
return self::createCancelledExecution($task, $cancellation);
try {
$cancellation?->throwIfRequested();
} catch (CancelledException $exception) {
return self::createCancelledExecution($task, $exception);
}

$receive = empty($this->jobQueue);
Expand Down Expand Up @@ -229,16 +231,11 @@ public function kill(): void
$this->exitStatus->ignore();
}

private static function createCancelledExecution(Task $task, Cancellation $cancellation): Execution
private static function createCancelledExecution(Task $task, CancelledException $exception): Execution
{
$channel = new StreamChannel(new ReadableBuffer(), new WritableBuffer());
$channel->close();

try {
$cancellation->throwIfRequested();
throw new \Error('Expected cancellation to have been requested');
} catch (CancelledException $exception) {
return new Execution($task, $channel, Future::error($exception));
}
return new Execution($task, $channel, Future::error($exception));
}
}

0 comments on commit ffda869

Please sign in to comment.