Skip to content

Commit 60a0b30

Browse files
Fixed: Hyperf Commands Shutdown (#26)
1 parent 7eeaa13 commit 60a0b30

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/Listener/OtelShutdownListener.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hyperf\OpenTelemetry\Listener;
66

7+
use Hyperf\Command\Event\AfterExecute;
78
use Hyperf\Contract\StdoutLoggerInterface;
89
use Hyperf\Coroutine\Coroutine;
910
use Hyperf\Event\Contract\ListenerInterface;
@@ -25,6 +26,7 @@ public function listen(): array
2526
{
2627
return [
2728
OnWorkerExit::class,
29+
AfterExecute::class,
2830
];
2931
}
3032

src/SpanProcessor/ChannelBatchSpanProcessor.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@ public function __construct(
4949
) {
5050
}
5151

52-
private function ensureInitialized(): void
53-
{
54-
if ($this->initialized) {
55-
return;
56-
}
57-
58-
$this->initialized = true;
59-
$this->exportContext = Context::getCurrent();
60-
$this->channel = new Channel($this->channelCapacity);
61-
$this->startConsumer();
62-
$this->startFlushTimer();
63-
}
64-
6552
public function onStart(ReadWriteSpanInterface $span, ContextInterface $parentContext): void
6653
{
6754
}
@@ -116,6 +103,19 @@ public function shutdown(?CancellationInterface $cancellation = null): bool
116103
return $this->exporter->shutdown($cancellation);
117104
}
118105

106+
private function ensureInitialized(): void
107+
{
108+
if ($this->initialized) {
109+
return;
110+
}
111+
112+
$this->initialized = true;
113+
$this->exportContext = Context::getCurrent();
114+
$this->channel = new Channel($this->channelCapacity);
115+
$this->startConsumer();
116+
$this->startFlushTimer();
117+
}
118+
119119
private function pushBatch(): void
120120
{
121121
if ($this->batch === [] || $this->channel === null) {

tests/Unit/Listener/OtelShutdownListenerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Unit\Listener;
66

7+
use Hyperf\Command\Event\AfterExecute;
78
use Hyperf\Contract\StdoutLoggerInterface;
89
use Hyperf\Event\Contract\ListenerInterface;
910
use Hyperf\Framework\Event\OnWorkerExit;
@@ -36,7 +37,7 @@ public function testListensToOnWorkerExit(): void
3637
$this->createMock(TracerProviderInterface::class),
3738
$this->createMock(StdoutLoggerInterface::class)
3839
);
39-
$this->assertSame([OnWorkerExit::class], $listener->listen());
40+
$this->assertSame([OnWorkerExit::class, AfterExecute::class], $listener->listen());
4041
}
4142

4243
public function testProcessCallsShutdownInsideCoroutine(): void

0 commit comments

Comments
 (0)