Skip to content

Commit 01d88b3

Browse files
authored
Merge pull request #7 from sebestenyb/sebestenyb-patch-2
Global Partition Resolver is ignored
2 parents 1dc6fb0 + f23c116 commit 01d88b3

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

src/Jobs/BalancedDispatchable.php

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ trait BalancedDispatchable
3939
/**
4040
* The partition key for this job.
4141
*/
42-
protected ?string $partitionKey = null;
42+
public ?string $partitionKey = null;
4343

4444
/**
4545
* Set the partition key for this job.
@@ -50,37 +50,4 @@ public function onPartition(string|int $partition): static
5050

5151
return $this;
5252
}
53-
54-
/**
55-
* Get the partition key for this job.
56-
*
57-
* Override this method to provide custom partition logic,
58-
* or use onPartition() when dispatching.
59-
*/
60-
public function getPartitionKey(): string
61-
{
62-
// If explicitly set via onPartition()
63-
if ($this->partitionKey !== null) {
64-
return $this->partitionKey;
65-
}
66-
67-
// Try common property names
68-
if (property_exists($this, 'userId')) {
69-
return (string) $this->userId;
70-
}
71-
72-
if (property_exists($this, 'user_id')) {
73-
return (string) $this->user_id;
74-
}
75-
76-
if (property_exists($this, 'tenantId')) {
77-
return (string) $this->tenantId;
78-
}
79-
80-
if (property_exists($this, 'tenant_id')) {
81-
return (string) $this->tenant_id;
82-
}
83-
84-
return 'default';
85-
}
8653
}

src/Queue/BalancedRedisQueue.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,28 @@ public function deletePartitionJob(string $queue, string $partition, string $job
263263
*/
264264
protected function resolvePartition($job): string
265265
{
266+
// 1. Explicitly set via onPartition()
267+
if (isset($job->partitionKey)) {
268+
return (string) $job->partitionKey;
269+
}
270+
271+
// 2. Overridden in job class (now works correctly without trait method!)
266272
if (method_exists($job, 'getPartitionKey')) {
267273
return (string) $job->getPartitionKey();
268274
}
269-
275+
276+
// 3. Global resolver from config
270277
if (isset($this->partitionResolver)) {
271278
return (string) ($this->partitionResolver)($job);
272279
}
273-
280+
281+
// 4. Auto-detect from common properties
282+
foreach (['userId', 'user_id', 'tenantId', 'tenant_id'] as $prop) {
283+
if (property_exists($job, $prop) && $job->$prop !== null) {
284+
return (string) $job->$prop;
285+
}
286+
}
287+
274288
return 'default';
275289
}
276290

tests/Feature/BalancedDispatchableTest.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace YanGusik\BalancedQueue\Tests\Feature;
66

77
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use ReflectionClass;
9+
use ReflectionException;
810
use YanGusik\BalancedQueue\Jobs\BalancedDispatchable;
11+
use YanGusik\BalancedQueue\Queue\BalancedRedisQueue;
912
use YanGusik\BalancedQueue\Tests\TestCase;
1013

1114
class TestJobWithUserId implements ShouldQueue
@@ -54,36 +57,51 @@ public function handle(): void
5457

5558
class BalancedDispatchableTest extends TestCase
5659
{
60+
/**
61+
* @throws ReflectionException
62+
*/
5763
public function test_partition_key_from_user_id_property(): void
5864
{
5965
$job = new TestJobWithUserId(userId: 42);
6066

61-
$this->assertEquals('42', $job->getPartitionKey());
67+
$this->assertEquals('42', $this->resolvePartition($job));
6268
}
6369

70+
/**
71+
* @throws ReflectionException
72+
*/
6473
public function test_partition_key_from_custom_method(): void
6574
{
6675
$job = new TestJobWithCustomPartition(customKey: 'abc123');
6776

68-
$this->assertEquals('custom:abc123', $job->getPartitionKey());
77+
$this->assertEquals('custom:abc123', $this->resolvePartition($job));
6978
}
7079

80+
/**
81+
* @throws ReflectionException
82+
*/
7183
public function test_partition_key_default_when_no_property(): void
7284
{
7385
$job = new TestJobWithoutPartition(data: 'test');
7486

75-
$this->assertEquals('default', $job->getPartitionKey());
87+
$this->assertEquals('default', $this->resolvePartition($job));
7688
}
7789

90+
/**
91+
* @throws ReflectionException
92+
*/
7893
public function test_on_partition_sets_explicit_key(): void
7994
{
8095
$job = new TestJobWithUserId(userId: 42);
8196
$job->onPartition('explicit-key');
8297

8398
// Explicit key takes precedence over userId property
84-
$this->assertEquals('explicit-key', $job->getPartitionKey());
99+
$this->assertEquals('explicit-key', $this->resolvePartition($job));
85100
}
86101

102+
/**
103+
* @throws ReflectionException
104+
*/
87105
public function test_on_partition_returns_self(): void
88106
{
89107
$job = new TestJobWithUserId(userId: 1);
@@ -92,11 +110,25 @@ public function test_on_partition_returns_self(): void
92110
$this->assertSame($job, $result);
93111
}
94112

113+
/**
114+
* @throws ReflectionException
115+
*/
95116
public function test_on_partition_accepts_integer(): void
96117
{
97118
$job = new TestJobWithUserId(userId: 1);
98119
$job->onPartition(999);
99120

100-
$this->assertEquals('999', $job->getPartitionKey());
121+
$this->assertEquals('999', $this->resolvePartition($job));
122+
}
123+
124+
/**
125+
* @throws ReflectionException
126+
*/
127+
private function resolvePartition($job): string
128+
{
129+
$queue = resolve(BalancedRedisQueue::class);
130+
$class = new ReflectionClass($queue);
131+
$method = $class->getMethod('resolvePartition');
132+
return $method->invokeArgs($queue, [$job]);
101133
}
102134
}

0 commit comments

Comments
 (0)