Skip to content

Commit

Permalink
Bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorprogger committed Dec 8, 2024
1 parent 3fe4f6a commit f3917f7
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Debug/QueueCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function collectWorkerProcessing(MessageInterface $message, QueueInterfac
if (!$this->isActive()) {
return;
}
$this->processingMessages[$queue->getChannelName()][] = $message;
$this->processingMessages[$queue->getChannelName() ?? 'null'][] = $message;
}

private function reset(): void
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/QueueDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
public function status(string|int $id): JobStatus
{
$result = $this->queue->status($id);
$this->collector->collectStatus($id, $result);
$this->collector->collectStatus((string) $id, $result);

return $result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function dispatch(
FailureHandlingRequest $request,
MessageFailureHandlerInterface $finishHandler
): FailureHandlingRequest {
/** @var string $channelName It is always string in this context */
$channelName = $request->getQueue()->getChannelName();
if (!isset($this->middlewareDefinitions[$channelName]) || $this->middlewareDefinitions[$channelName] === []) {
$channelName = self::DEFAULT_PIPELINE;
Expand Down
2 changes: 1 addition & 1 deletion src/Provider/AdapterFactoryQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private function getOrTryToCreate(string $channel): QueueInterface|null
),
);
}
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter)->withChannelName($channel);
$this->queues[$channel] = $this->baseQueue->withAdapter($adapter->withChannel($channel));
} else {
$this->queues[$channel] = null;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Provider/PrototypeQueueProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Queue\Provider;

use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\QueueInterface;

/**
Expand All @@ -17,12 +18,13 @@ final class PrototypeQueueProvider implements QueueProviderInterface
*/
public function __construct(
private readonly QueueInterface $baseQueue,
private readonly AdapterInterface $baseAdapter,
) {
}

public function get(string $channel): QueueInterface
{
return $this->baseQueue->withChannelName($channel);
return $this->baseQueue->withAdapter($this->baseAdapter->withChannel($channel));
}

public function has(string $channel): bool
Expand Down
11 changes: 9 additions & 2 deletions stubs/StubAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/
final class StubAdapter implements AdapterInterface
{
public function __construct(private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME)
{
}

public function runExisting(callable $handlerCallback): void
{
}
Expand All @@ -34,11 +38,14 @@ public function subscribe(callable $handlerCallback): void

public function withChannel(string $channel): AdapterInterface
{
return clone $this;
$new = clone $this;
$new->channelName = $channel;

return $new;
}

public function getChannelName(): string
{
return QueueInterface::DEFAULT_CHANNEL_NAME;
return $this->channelName;
}
}
20 changes: 4 additions & 16 deletions stubs/StubQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
final class StubQueue implements QueueInterface
{
public function __construct(
private string $channelName = QueueInterface::DEFAULT_CHANNEL_NAME,
private ?AdapterInterface $adapter = null,
) {
public function __construct(private ?AdapterInterface $adapter = null) {
}

public function push(
Expand Down Expand Up @@ -51,21 +48,12 @@ public function withAdapter(AdapterInterface $adapter): QueueInterface
{
$new = clone $this;
$new->adapter = $adapter;
return $new;
}

public function getChannelName(): string
{
return $this->channelName;
return $new;
}

public function withChannelName(string $channel): QueueInterface
public function getChannelName(): ?string
{
$new = clone $this;
$new->channelName = $channel;
if ($new->adapter !== null) {
$new->adapter = $new->adapter->withChannel($channel);
}
return $new;
return $this->adapter?->getChannelName();
}
}
7 changes: 5 additions & 2 deletions tests/Unit/Provider/CompositeQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class CompositeQueueProviderTest extends TestCase
{
public function testBase(): void
{
$queue = new StubQueue('channel');
$queue = new StubQueue(new StubAdapter());
$provider = new CompositeQueueProvider(
new AdapterFactoryQueueProvider(
$queue,
Expand All @@ -38,7 +38,10 @@ public function testBase(): void
public function testNotFound(): void
{
$provider = new CompositeQueueProvider(
new AdapterFactoryQueueProvider(new StubQueue('channel'), ['channel1' => new StubAdapter()]),
new AdapterFactoryQueueProvider(
new StubQueue(new StubAdapter()),
['channel1' => new StubAdapter()]
),
);

$this->expectException(ChannelNotFoundException::class);
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Provider/PrototypeQueueProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Provider\PrototypeQueueProvider;
use Yiisoft\Queue\Stubs\StubAdapter;
use Yiisoft\Queue\Stubs\StubQueue;

final class PrototypeQueueProviderTest extends TestCase
Expand All @@ -14,6 +15,7 @@ public function testBase(): void
{
$provider = new PrototypeQueueProvider(
new StubQueue(),
new StubAdapter(),
);

$queue = $provider->get('test-channel');
Expand Down
13 changes: 1 addition & 12 deletions tests/Unit/Stubs/StubQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testBase(): void
$this->assertSame($message, $queue->push($message));
$this->assertSame(0, $queue->run());
$this->assertTrue($queue->status('test')->isDone());
$this->assertSame(QueueInterface::DEFAULT_CHANNEL_NAME, $queue->getChannelName());
$this->assertNull($queue->getChannelName());
$this->assertNull($queue->getAdapter());
$queue->listen();
}
Expand All @@ -34,15 +34,4 @@ public function testWithAdapter(): void
$this->assertNotSame($queue, $sourceQueue);
$this->assertInstanceOf(StubAdapter::class, $queue->getAdapter());
}

public function testWithChannelName(): void
{
$sourceQueue = new StubQueue();

$queue = $sourceQueue->withChannelName('test');

$this->assertNotSame($queue, $sourceQueue);
$this->assertSame(QueueInterface::DEFAULT_CHANNEL_NAME, $sourceQueue->getChannelName());
$this->assertSame('test', $queue->getChannelName());
}
}

0 comments on commit f3917f7

Please sign in to comment.