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

[5.7] When scheduling a job, respect the already set queue #25159

Closed
wants to merge 4 commits into from
Closed

[5.7] When scheduling a job, respect the already set queue #25159

wants to merge 4 commits into from

Conversation

petervmeijgaard
Copy link

Problem description

When creating a job, we sometimes want to define in which queue the job should run.
Sometimes that's something that we want to define in the job itself.

When creating a job, we often specify which queue it should be stored at in the constructor:

// MyCustomJob-class

public function __construct()
{
    $this->onQueue('my-custom-queue');
}

But when scheduling this job, we need to specify a queue in which the job should run.

// Will add the job to the 'default'-queue.
$schedule->job(MyCustomJob::class)->everyMinute();

// Will add the job to the 'my-custom-queue'-queue.
$schedule->job(MyCustomJob::class, 'my-custom-queue')->everyMinute();

This results in having to specify the queue multiple times instead of just once.

Proposed solution

It would be a good solution to respect the already defined queue on the custom job class.
Using the example above, it'll result in a solution something like this:

// MyCustomJob-class

public function __construct()
{
    $this->onQueue('my-custom-queue');
}
// Will add the job to the 'my-custom-queue'-queue.
$schedule->job(MyCustomJob::class)->everyMinute();

// Will add the job to the 'my-other-queue'-queue.
$schedule->job(MyCustomJob::class, 'my-other-queue')->everyMinute();

// Will still add the job to the 'default'-queue.
$schedule->job(MyCustomJob::class, 'default')->everyMinute();

When scheduling a job, we need to respect the already set queue in the job itself.
This commit will prevent the queue from getting overwritten.
@sisve
Copy link
Contributor

sisve commented Aug 9, 2018

Could you add a test for this?

Still need to dispatch the queued job, but because it's a queued job, it seems harder to fire it.
@petervmeijgaard
Copy link
Author

petervmeijgaard commented Aug 11, 2018

Hi @sisve,

I tried to, but I am struggling to fire a scheduled job in my unit test.
If you have the time, could you take a look at what I may am missing?
https://github.com/laravel/framework/pull/25159/files#diff-977603a2c6bc453a498ed4a1b18ec275R39

@X-Coder264
Copy link
Contributor

X-Coder264 commented Aug 11, 2018

@petervmeijgaard I didn't try this, but off the top of my head...

$events = $scheduler->events();
foreach ($events as $event) {
    $event->run($container);
}

@taylorotwell
Copy link
Member

The code doesn't even look correct anymore? Your original PR code looked correct. I don't know why you are setting $queue to a booelan now?

…ue is null. If so, use the queue specified on the job. Otherwise use the queue variable.
@petervmeijgaard
Copy link
Author

Oh my apologies, I was tinkering around with trying different approaches by setting the queue different ways. What I intended to do was $queue = $queue !== null ? $queue : $job->queue.

Coming from a JavaScript perspective, I thought that the code above would result in $queue = $queue || $job->queue, but I was mistaken. I've reverted the code change.

@@ -97,6 +97,8 @@ public function job($job, $queue = null, $connection = null)
$job = is_string($job) ? resolve($job) : $job;

if ($job instanceof ShouldQueue) {
$queue = $queue !== null ? $queue : $job->queue;
Copy link
Contributor

@deleugpn deleugpn Aug 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$queue = $queue ?? $job->queue;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an empty string a valid queue name?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I had a brain-fart. I was thinking of the ?: operator. ?? seem to work as expected.

@taylorotwell
Copy link
Member

Tests fail. Please re-submit with working tests.

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.

5 participants