0

how do you switch between ShouldQueue and Sync in a class?

We have got an endpoint which accepts an argument whether a job should be fired immediately or later.

In both cases the same logic should be executed but in one scenario I expect and answer back and the other one should be handled async.

I am aware of implementing "ShouldQueue" and use the "InteractsWithQueue"-Trait but how can we use this in one situation and not use it in the other?

Can you programatically set whether a request should be queued or not or are there better ways to do this? Thanks

5
  • Haven't tested this yet myself, but can you instantiate the job class and if it must be run immediately, or "online", then just call the handle() method in your controller? If you want to run it in the queue, or "offline", you can use the dispatcher to queue it like normal. Commented Aug 15, 2016 at 22:36
  • @xjstratedgebx yes, this works but how do I pass in a variable in both cases? In the dispatch case it is expected in the constructor, in the handle case the handle method. Commented Aug 16, 2016 at 18:17
  • If you resolve the job class through the service container (i.e. $job = app(\App\Jobs\MyJob::class);), any classes you type hint in the constructor will automatically be passed for you. If you call handle() manually and it requires you to pass parameters, then just pass the parameters when you call it. No need to worry about those parameters when you queue it - I believe the service container will automatically inject the parameters when its run from the queue. Commented Aug 16, 2016 at 19:50
  • @xjstratedgebx - Thank you for your effort. Joseph Silber down here had a better solution. But that one should work also. Commented Aug 31, 2016 at 20:22
  • Not a problem, his solution is good for me to know as well! Commented Aug 31, 2016 at 20:24

1 Answer 1

2

In your controller:

$this->dispatch($job) // queued if implements ShouldQueue

$this->dispatchNow($job) // never queues

If stuck on Laravel 5.1, create a new instance of the Dispatcher yourself:

use Illuminate\Contracts\Bus\Dispatcher;

// Later ...

app(Dispatcher::class)->dispatchNow($job);
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Joseph, this was to good to be true. dispatchNow is not a function. Do I need anything else for this to work?
@hogan which version of Laravel are you using? It's in 5.2 at least (see here). All it does is app(\Illuminate\Contracts\Bus\Dispatcher::class)->dispatchNow($job);
@hogan - if you've upgraded from an earlier version of Laravel, be sure to use the Illuminate/Foundation/Bus/DispatchesJobs trait in your controller.
Oh, should have mentioned it's Laravel 5.1
@JosephSilber - Thank you for your answer and effort! It works as described.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.