Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Introduce OnQueue and OnConnection attributes for jobs #54229

Open
wants to merge 6 commits into
base: 11.x
Choose a base branch
from

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Jan 17, 2025

The problem

PHP is weird about traits. If a class uses a trait, then it cannot re-declare properties from the trait with differing values. (Though if another class uses the trait and then you extend that class, you can 🙃)

This is problematic with setting queue/connections, where it would be great if we could just define the $queue and $connection properties.

An example with Queueable
class SyncAthletesJob implements ShouldQueue
{
    use Queueable;
    use Dispatchable;

    public $connection = "redis_medium";  // ❌ this doesn't work, as it differs from the definition in Queueable
}

Setting queue/connection is annoying

My jobs do not need to be dynamic in where they are dispatched. If it's a SyncAthletesJob, it's always on the import queue (and the import queue is always on the redis_medium connection, but that's not a problem solved here).

The two not-so-pretty ways of specifying this
class SyncAthletesJob implements ShouldQueue
{
    use Queueable;
    use Dispatchable;

    public function __construct(private TeamId $teamId)
    {
        $this->onConnection('redis_medium');
        $this->onQueue('import');
    }
}

// or when I call it
SyncAthletesJob::dispatch($teamId)->onQueue('import')->onConnection('redis_medium');

The solution

Introduce OnQueue and OnConnection attributes.

Now the above job can look clean 🧹 and I never have to worry about specifying queue/connection in calling code or inside of the constructor.

#[OnQueue(QueueEnum::IMPORT)]
#[OnConnection(QueueConnectionEnum::REDIS_MEDIUM)]
class SyncAthletesJob implements ShouldQueue
{
    use Queueable;
    use Dispatchable;

    public function __construct(private TeamId $teamId) { }
}

Bonus: when I call dispatch(new SyncAthletesJob($teamId)) I get the same benefit.

Follow ups

  • Does this need added for queued event listeners as well? If so, we could share these attributes, but leaving them in Foundation/Bus doesn't really make sense.

  • Additional attributes for #[OnDasher], #[OnDancer], #[OnPrancer], #[OnVixen], et cetera

@cosmastech cosmastech marked this pull request as draft January 17, 2025 13:54
@cosmastech cosmastech marked this pull request as ready for review January 17, 2025 14:26
@cosmastech cosmastech changed the title [11.x] Introduce OnQueue and OnConnection attributes [11.x] Introduce OnQueue and OnConnection attributes for jobs Jan 17, 2025
@shaedrich
Copy link
Contributor

An On prefix makes me think, it's an event listener. In would sound more naturally to me.

@cosmastech
Copy link
Contributor Author

An On prefix makes me think, it's an event listener. In would sound more naturally to me.

But then how would I make my super funny joke about #[OnDasher], #[OnDancer]?

@shaedrich
Copy link
Contributor

I see, touché 😂👍🏻

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.

2 participants