Skip to content

Commit 013cba9

Browse files
authored
Refactor resolvePartition method for clarity
Adjust the order of checks in resolvePartition(). The current PR checks properties before the getPartitionKey() method, which could break existing code where users override getPartitionKey() in their jobs. This way: * onPartition() has highest priority (explicit) * Custom getPartitionKey() in job classes still works * Global resolver finally works as a fallback * Auto-detection from properties is the last resort
1 parent 491d79a commit 013cba9

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/Queue/BalancedRedisQueue.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -263,34 +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()
266267
if (isset($job->partitionKey)) {
267-
return $job->partitionKey;
268+
return (string) $job->partitionKey;
268269
}
269-
270-
if (property_exists($job, 'userId')) {
271-
return (string)$job->userId;
272-
}
273-
274-
if (property_exists($job, 'user_id')) {
275-
return (string)$job->user_id;
276-
}
277-
278-
if (property_exists($job, 'tenantId')) {
279-
return (string)$job->tenantId;
280-
}
281-
282-
if (property_exists($job, 'tenant_id')) {
283-
return (string)$job->tenant_id;
284-
}
285-
270+
271+
// 2. Overridden in job class (now works correctly without trait method!)
286272
if (method_exists($job, 'getPartitionKey')) {
287273
return (string) $job->getPartitionKey();
288274
}
289-
275+
276+
// 3. Global resolver from config
290277
if (isset($this->partitionResolver)) {
291278
return (string) ($this->partitionResolver)($job);
292279
}
293-
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+
294288
return 'default';
295289
}
296290

0 commit comments

Comments
 (0)