Skip to content

Commit

Permalink
add 'acceptMessage' property on service bus
Browse files Browse the repository at this point in the history
  • Loading branch information
achretien committed Sep 24, 2024
1 parent 3a869dd commit 450444e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use yii\base\NotSupportedException;
use yii\di\Instance;
use yii\httpclient\Exception;
use yii\queue\PushEvent;

class Queue extends \yii\queue\cli\Queue
{
Expand All @@ -24,7 +23,7 @@ class Queue extends \yii\queue\cli\Queue
public string $queue = 'default';
/** @var ServiceBus[] */
public array $queues = [
'default' => 'serviceBus'
'default' => 'serviceBus',
];
// endregion Public Properties

Expand All @@ -45,6 +44,26 @@ public function init(): void
// endregion Initialization

// region Public Methods
public function push($job): ?string
{
$defaultQueue = $this->queue;

if (
$job instanceof AzureJobInterface
&& ($dedicatedQueue = $job->getQueue())
&& isset($this->queues[$dedicatedQueue])
&& $this->queues[$dedicatedQueue]->acceptMessage
) {
$this->queue = $dedicatedQueue;
}

$result = parent::push($job);

$this->queue = $defaultQueue;

return $result;
}

/**
* Listens queue and runs each job.
*
Expand All @@ -60,21 +79,6 @@ public function run(bool $repeat, ?string $queue = null, int $timeout = 30): ?in
return $this->runWorker(fn (callable $canContinue) => $this->processWorker($canContinue, $repeat, $queue ?? $this->queue, $timeout));
}

public function push($job): ?string
{
$defaultQueue = $this->queue;

if ($job instanceof AzureJobInterface && ($dedicatedQueue = $job->getQueue()) && isset($this->queues[$dedicatedQueue])) {
$this->queue = $dedicatedQueue;
}

$result = parent::push($job);

$this->queue = $defaultQueue;

return $result;
}

/**
* @param string $id of a job message
*
Expand Down
1 change: 1 addition & 0 deletions src/service/ServiceBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ServiceBus extends Component
// endregion Constants

// region Public Properties
public bool $acceptMessage = true;
public string $environment = 'servicebus.windows.net';
public string $queue;
public int $requestMaxRetries = 10;
Expand Down

0 comments on commit 450444e

Please sign in to comment.