11

I have a Job to perform send SMS to user. I want to run this job on the specify queue-name. For example, this job added to "SMS" queue. So I found a way to do this but it's exists some errors.

Create job instance and use onQueue() function to do this:

    $resetPasswordJob = new SendGeneratedPasswordResetCode(app()->make(ICodeNotifier::class), [
        'number' => $user->getMobileNumber(),
        'operationCode' => $operationCode
    ]);

    $resetPasswordJob->onQueue('SMS');

    $this->dispatch($resetPasswordJob);

My Job class like this:

class SendGeneratedPasswordResetCode implements ShouldQueue
{
   use InteractsWithQueue, Queueable;

/**
 * The code notifier implementation.
 *
 * @var ICodeNotifier
 */
protected $codeNotifier;

/**
 * Create the event listener.
 *
 * @param ICodeNotifier $codeNotifier
 * @return self
 */
public function __construct(ICodeNotifier $codeNotifier)
{
    $this->codeNotifier = $codeNotifier;
}

/**
 * Handle the event.
 *
 * @return void
 */
public function handle()
{
    echo "bla blaa bla";
    #$this->codeNotifier->notify($event->contact->getMobileNumber(), $event->code);
}

public function failed()
{
    var_dump("failll");
}
}

So I type this command to console:

php artisan queue:listen --queue=SMS --tries=1

But this error message I get when executes this job:

[InvalidArgumentException]

No handler registered for command [App\Services\Auth\User\Password\SendGeneratedPasswordResetCode]

Note: Other way is add event to EventServiceProvider's listen property and fire the event. But it's does not work with specify queue-name.

Community
  • 1
  • 1
Mesuti
  • 878
  • 1
  • 13
  • 29

3 Answers3

21

You can also specify the queue you want your job to be placed onto by setting the Job objects queue property on construct:

class SendGeneratedPasswordResetCode implements ShouldQueue
{
    // Rest of your class before the construct

    public function __construct(ICodeNotifier $codeNotifier)
    {
        $this->queue = 'SMS'; // This states which queue this job will be placed on.
        $this->codeNotifier = $codeNotifier;
    }

    // Rest of your class after construct

You then do not need to provide the ->onQueue() method on each implementation/usage of this job, as the Job class itself will do it for you.

I have tested this in Laravel 5.6

Dan Streeter
  • 2,259
  • 2
  • 16
  • 16
1

you are calling onQueue in the wrong place
should be like below

dispatch(new YourJob())->onQueue('my_queue');

to make it work

php artisan queue:work --queue=my_queue
dılo sürücü
  • 3,821
  • 1
  • 26
  • 28
1

This is now covered in the documentation: https://laravel.com/docs/9.x/queues#dispatching-to-a-particular-queue

The preferred method is to explicitly call onQueue(...) within the constructor of the job:

<?php
 
namespace App\Jobs;
 
 use Illuminate\Bus\Queueable;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 
class ProcessPodcast implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->onQueue('processing');
    }
}
slothstronaut
  • 921
  • 1
  • 13
  • 15