From ffda869c33c30627b6eb5c25f096882d885681dc Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 16 Mar 2024 11:15:46 -0500 Subject: [PATCH] Better cancelled execution creation --- src/Worker/Internal/ContextWorker.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Worker/Internal/ContextWorker.php b/src/Worker/Internal/ContextWorker.php index 1aa3bbd..a399cac 100644 --- a/src/Worker/Internal/ContextWorker.php +++ b/src/Worker/Internal/ContextWorker.php @@ -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); @@ -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)); } }