Skip to content

Global Partition Resolver is ignored#7

Merged
YanGusik merged 4 commits intoYanGusik:mainfrom
sebestenyb:sebestenyb-patch-2
Jan 28, 2026
Merged

Global Partition Resolver is ignored#7
YanGusik merged 4 commits intoYanGusik:mainfrom
sebestenyb:sebestenyb-patch-2

Conversation

@sebestenyb
Copy link
Copy Markdown
Contributor

  1. The BalancedDispatchable contains a base method of getPartitionKey()
  2. The BalancedRedisQueue prioritise this method to resolve the partition in its resolvePartition method over the partitionResolver callback
  3. Since every balanced job uses the BalancedDispatchable trait, every job implements the getPartitionKey, the global partition resolver is ignored.

I think the trait should not implement the getPartitionKey method, leaving it to the actual jobs, and the property checks should maybe be moved to the BalancedRedisQueue@resolvePartition(), before the method checks.

Fixes #6

Removed the getPartitionKey method and its documentation.
Add additional checks for partition key resolution based on job properties.
@YanGusik
Copy link
Copy Markdown
Owner

YanGusik commented Jan 27, 2026

Thanks for this PR! You've correctly identified the issue - the global partition_resolver is indeed ignored because getPartitionKey() always exists in the trait.

However, I think the order of checks in resolvePartition() should be adjusted. The current PR checks properties before the getPartitionKey() method, which could break existing code where users override getPartitionKey() in their jobs.

Proposed order:

protected function resolvePartition($job): string
{
    // 1. Explicitly set via onPartition()
    if (isset($job->partitionKey)) {
        return (string) $job->partitionKey;
    }

    // 2. Overridden in job class (now works correctly without trait method!)
    if (method_exists($job, 'getPartitionKey')) {
        return (string) $job->getPartitionKey();
    }

    // 3. Global resolver from config
    if (isset($this->partitionResolver)) {
        return (string) ($this->partitionResolver)($job);
    }

    // 4. Auto-detect from common properties
    foreach (['userId', 'user_id', 'tenantId', 'tenant_id'] as $prop) {
        if (property_exists($job, $prop) && $job->$prop !== null) {
            return (string) $job->$prop;
        }
    }

    return 'default';
}

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

Could you update the PR with this order? Thanks!

sebestenyb and others added 2 commits January 28, 2026 06:55
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
@YanGusik YanGusik merged commit 01d88b3 into YanGusik:main Jan 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Global Partition Resolver is ignored

2 participants