I'm creating a job, pushing it on to a custom queue, and trying to use the Redis driver to then handle the job when it hits the queue, without success:
class MyController extends Controller {
public function method() {
$job = (new UpdateLiveThreadJob())->onQueue('live');
$this->dispatch($job);
}
}
Here is my queue config:
'default' => env('QUEUE_DRIVER'),
'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'expire' => 60,
],
Here is my .env
file:
# Drivers (Queues & Broadcasts)
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis
BROADCAST_DRIVER=redis
Here's my job:
class UpdateLiveThreadJob extends Job implements SelfHandling, ShouldQueue
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// Rerender content
$templatedOutput = view('templates.livethreadcontents')->with([
'updates' => collect(Redis::lrange('live:updates', 0, -1))->reverse()->map(function($update) {
return json_decode($update);
})
])->render();
// Connect to external service
// Update Thread
}
}
Indeed, I can change the handle
method to do nothing to ensure it's nothing in the job that's actually causing it to fail, and it still doesn't process:
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
print_r('test');
}
Using Redis, I can see it's pushed onto the queue:
> lrange queues:live 0 -1
> // json encoded job present
> llen queues:live
> // shows there is a job in the queue
Yet, it never actually fires, to my knowledge. Watching php artisan queue:listen
shows nothing (only unrelated event broadcasts). What's going on here?
Laravel has a unified queueing API that lets you choose from various technologies such as Redis, Amazon SQS, Beanstalkd, or even an old-fashioned relational database system.
Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time.
$this->mailer->queue($view, $data, function ($message) use ($toEmail, $toName, $subject) { $message ->to($toEmail, $toName) ->subject($subject); }); This will successfully run, but if the queue is not 'listening', the job gets pushed on to the job table, forever. I am looking for something like \Queue::isListening();
With Laravel 5.3 there were changes to queues. Now you would run php artisan queue:work --queue=live
and that should do what you need.
I’ve left my original answer below.
Are you remembering to run php artisan queue:listen --queue=live
?
You need to define the queue name when running the listen command otherwise you end up only listening to the default
queue.
If you want to run multiple queues and managing things in production you can use something like Upstart (not directly related to setting up Laravel queues, but provides a good starting point) or Supervisor to manage the processes. Both of these are available on Forge and Homestead.
Finally assuming you are on Laravel 5 you may want to consider running php artisan queue:work --daemon --queue=live
as this reduces the CPU overhead of running the worker as it doesn't reload the framework with each job. But you must remember to restart the worker when you deploy new code for your jobs otherwise things won't be picked up.
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