Skip to content

Commit 3c8e2bd

Browse files
authored
Merge pull request #12 from MammatusPHP/cancel-all-promises-when-shutting-down
Cancel all promises when shutting down
2 parents d7c8246 + 613980c commit 3c8e2bd

File tree

4 files changed

+50
-121
lines changed

4 files changed

+50
-121
lines changed

composer.lock

+34-112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Mammatus\Queue\App\Queue;
1212
use Mammatus\Queue\Generated\AbstractList;
1313
use Psr\Log\LoggerInterface;
14+
use React\Promise\PromiseInterface;
1415
use Throwable;
1516
use WyriHaximus\Broadcast\Contracts\Listener;
1617

@@ -21,6 +22,9 @@
2122
/** @implements Bootable<Queue> */
2223
final class App extends AbstractList implements Bootable, Listener
2324
{
25+
/** @var array<PromiseInterface<mixed>> */
26+
private array $promises = [];
27+
2428
public function __construct(
2529
private readonly Consumer $consumer,
2630
private readonly LoggerInterface $logger,
@@ -29,22 +33,25 @@ public function __construct(
2933

3034
public function stop(Shutdown $event): void
3135
{
36+
foreach ($this->promises as $promise) {
37+
$promise->cancel();
38+
}
39+
3240
$this->consumer->close();
3341
}
3442

3543
public function boot(Argv $argv): ExitCode
3644
{
3745
try {
38-
$promises = [];
3946
foreach ($this->workers() as $worker) {
4047
if ($worker->class !== $argv->className) {
4148
continue;
4249
}
4350

44-
$promises[] = $this->consumer->setupConsumer($worker);
51+
$this->promises[] = $this->consumer->setupConsumer($worker);
4552
}
4653

47-
await(all($promises));
54+
await(all($this->promises));
4855

4956
$exitCode = ExitCode::Success;
5057
} catch (Throwable $throwable) { /** @phpstan-ignore-line */

src/Manager.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Mammatus\Queue;
66

7-
use Mammatus\LifeCycleEvents\Initialize;
7+
use Mammatus\LifeCycleEvents\Boot;
88
use Mammatus\LifeCycleEvents\Shutdown;
99
use Mammatus\Queue\Generated\AbstractList;
1010
use Psr\EventDispatcher\EventDispatcherInterface;
@@ -21,7 +21,7 @@ public function __construct(
2121
) {
2222
}
2323

24-
public function start(Initialize $event): void
24+
public function start(Boot $event): void
2525
{
2626
$this->logger->debug('Starting queue manager');
2727
$this->boot();

tests/ManagerTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Mammatus\Tests\Queue;
66

7-
use Mammatus\LifeCycleEvents\Initialize;
7+
use Mammatus\LifeCycleEvents\Boot;
88
use Mammatus\LifeCycleEvents\Shutdown;
99
use Mammatus\Queue\BuildIn\Noop;
1010
use Mammatus\Queue\Contracts\Worker as WorkerContract;
@@ -47,7 +47,7 @@ public function runHappy(): void
4747
$eventDispatcher,
4848
$logger,
4949
);
50-
$manager->start(new Initialize());
50+
$manager->start(new Boot());
5151
await(sleep(9));
5252
$manager->stop(new Shutdown());
5353
}
@@ -76,7 +76,7 @@ public function runAngry(): void
7676
$eventDispatcher,
7777
$logger,
7878
);
79-
$manager->start(new Initialize());
79+
$manager->start(new Boot());
8080
await(sleep(9));
8181
$manager->stop(new Shutdown());
8282
}
@@ -111,6 +111,6 @@ public function notAnWorker(): void
111111
$eventDispatcher,
112112
$logger,
113113
);
114-
$manager->start(new Initialize());
114+
$manager->start(new Boot());
115115
}
116116
}

0 commit comments

Comments
 (0)