Skip to content

Commit

Permalink
Merge pull request #49 from aloware/fix/optimize-queue-partitions
Browse files Browse the repository at this point in the history
Fix | Optimize Queue Partitions
  • Loading branch information
hamed-aloware authored Apr 13, 2023
2 parents 7291fe7 + b1b2834 commit 5020b68
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 97 deletions.
16 changes: 4 additions & 12 deletions src/FairSignalJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function handle()
/** @var RepositoryInterface $repository */
$repository = app(RepositoryInterface::class);

list($partition, $jobSerialized) = $this->pop($repository, 'pop', 'partitions');
list($partition, $jobSerialized) = $this->pop($repository, 'pop', 'getRandomPartitionName');

// if (is_null($jobSerialized)) {
// list($partition, $jobSerialized) = $this->pop($repository, 'popFailed', 'failedPartitions');
Expand Down Expand Up @@ -136,7 +136,7 @@ public function onPartition($partition)
return $this;
}

private function pop($repository, $popMethod = 'pop', $partitionsMethod = 'partitions')
private function pop($repository, $popMethod = 'pop', $partitionsMethod = 'getRandomPartitionName')
{
$partition = $this->selectPartition($repository, $partitionsMethod);

Expand Down Expand Up @@ -167,17 +167,9 @@ private function pop($repository, $popMethod = 'pop', $partitionsMethod = 'parti
return [$partition, $jobSerialized];
}

private function selectPartition($repository, $partitionsMethod = 'partitions')
private function selectPartition($repository, $partitionsMethod = 'getRandomPartitionName')
{
$partitions = $repository->$partitionsMethod($this->queue);

if (empty($partitions)) {
return null;
}

$partitionIndex = random_int(0, count($partitions) - 1);

return $partitions[$partitionIndex];
return $repository->$partitionsMethod($this->queue);
}

public function updateStats($uuid)
Expand Down
9 changes: 9 additions & 0 deletions src/Repositories/RedisKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ private function queueListPattern()
);
}

private function queuePartitionsListKeyName($queue)
{
return sprintf(
'%s-partitions-%s',
$this->fairQueueKeyPrefix(),
$queue
);
}

private function queuePartitionListPattern($queue)
{
return sprintf(
Expand Down
Loading

0 comments on commit 5020b68

Please sign in to comment.