|
2 | 2 |
|
3 | 3 | namespace Amp\Parallel\Worker\Internal;
|
4 | 4 |
|
| 5 | +use Amp\ByteStream\ReadableBuffer; |
| 6 | +use Amp\ByteStream\StreamChannel; |
| 7 | +use Amp\ByteStream\WritableBuffer; |
5 | 8 | use Amp\Cancellation;
|
6 | 9 | use Amp\CancelledException;
|
7 | 10 | use Amp\DeferredFuture;
|
@@ -139,6 +142,10 @@ public function submit(Task $task, ?Cancellation $cancellation = null): Executio
|
139 | 142 | throw new StatusError("The worker has been shut down");
|
140 | 143 | }
|
141 | 144 |
|
| 145 | + if ($cancellation?->isRequested()) { |
| 146 | + return self::createCancelledExecution($task, $cancellation); |
| 147 | + } |
| 148 | + |
142 | 149 | $receive = empty($this->jobQueue);
|
143 | 150 | $submission = new Internal\TaskSubmission($task);
|
144 | 151 | $jobId = $submission->getId();
|
@@ -220,4 +227,17 @@ public function kill(): void
|
220 | 227 | $this->exitStatus ??= Future::error(new WorkerException("The worker was killed"));
|
221 | 228 | $this->exitStatus->ignore();
|
222 | 229 | }
|
| 230 | + |
| 231 | + private static function createCancelledExecution(Task $task, Cancellation $cancellation): Execution |
| 232 | + { |
| 233 | + $channel = new StreamChannel(new ReadableBuffer(), new WritableBuffer()); |
| 234 | + $channel->close(); |
| 235 | + |
| 236 | + try { |
| 237 | + $cancellation->throwIfRequested(); |
| 238 | + throw new \Error('Expected cancellation to have been requested'); |
| 239 | + } catch (CancelledException $exception) { |
| 240 | + return new Execution($task, $channel, Future::error($exception)); |
| 241 | + } |
| 242 | + } |
223 | 243 | }
|
0 commit comments