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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With