Skip to content

Commit f698890

Browse files
committed
channels
1 parent 6b4e846 commit f698890

4 files changed

Lines changed: 27 additions & 13 deletions

File tree

packages/queue/src/AbstractRunner.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public function loop(string|array $channel): void
8787
{
8888
gc_enable();
8989

90+
$isAllChannels = in_array('*', (array) $channel, true);
91+
9092
// Last Restart
9193
$this->lastRestart = (int) new DateTimeImmutable('now')->format('U');
9294

@@ -113,8 +115,18 @@ public function loop(string|array $channel): void
113115
$this->registerSignals();
114116

115117
if (($this->options->force ?? null) || $this->canLoop()) {
118+
$channels = $channel;
119+
120+
if ($isAllChannels) {
121+
$channels = iterator_to_array($this->queue->getChannels());
122+
}
123+
124+
if ($this->options->shuffleChannels && is_array($channels)) {
125+
shuffle($channels);
126+
}
127+
116128
try {
117-
$this->next($channel);
129+
$this->next($channels);
118130
} catch (UnrecoverableException $e) {
119131
$this->stop('[STOP] Unrecoverable error: ' . $e->getMessage(), 1, true);
120132
} catch (Exception $exception) {

packages/queue/src/Queue.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use JsonException;
99
use Windwalker\DI\Definition\DefinitionInterface;
10+
use Windwalker\Queue\Driver\ChannelAwareDriverInterface;
1011
use Windwalker\Queue\Driver\QueueDriverInterface;
1112
use Windwalker\Queue\Job\ClosureJob;
1213
use Windwalker\Utilities\Classes\ObjectBuilderAwareTrait;
@@ -157,6 +158,18 @@ public function defer(mixed $message, int $delay = 0): void
157158
$this->driver->defer($message);
158159
}
159160

161+
/**
162+
* @return iterable<string>
163+
*/
164+
public function getChannels(): iterable
165+
{
166+
if (!$this->driver instanceof ChannelAwareDriverInterface) {
167+
throw new \DomainException('Driver does not support get channels.');
168+
}
169+
170+
return $this->driver->getChannels();
171+
}
172+
160173
/**
161174
* getMessage
162175
*

packages/queue/src/RunnerOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct(
1616
public int $timeout = 60,
1717
public int $maxRuns = 0,
1818
public int $lifetime = 0,
19+
public bool $shuffleChannels = false,
1920
public bool $stopWhenEmpty = false,
2021
public ?string $restartSignal = null,
2122
public ?\Closure $controllerFactory = null,

packages/queue/src/Worker.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@
44

55
namespace Windwalker\Queue;
66

7-
use DateTimeImmutable;
8-
use Exception;
9-
use Psr\Log\AbstractLogger;
10-
use Psr\Log\LoggerInterface;
11-
use Psr\Log\NullLogger;
127
use Throwable;
13-
use Windwalker\Event\EventAwareInterface;
14-
use Windwalker\Event\EventAwareTrait;
158
use Windwalker\Queue\Attributes\JobBackoff;
169
use Windwalker\Queue\Attributes\JobFailed;
1710
use Windwalker\Queue\Event\AfterJobRunEvent;
1811
use Windwalker\Queue\Event\BeforeJobRunEvent;
19-
use Windwalker\Queue\Event\DebugOutputEvent;
2012
use Windwalker\Queue\Event\JobFailureEvent;
21-
use Windwalker\Queue\Event\LoopEndEvent;
22-
use Windwalker\Queue\Event\LoopFailureEvent;
23-
use Windwalker\Queue\Event\LoopStartEvent;
24-
use Windwalker\Queue\Event\StopEvent;
2513
use Windwalker\Queue\Exception\MaxAttemptsExceededException;
2614
use Windwalker\Queue\Job\JobController;
2715

0 commit comments

Comments
 (0)