11

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?

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Wait, really? I can't believe it's this simple! I have never been told or shown this, and the documentation doesn't imply it. How can I run all queues? Is there some sort of * attribute I can provide?
Unfortunately as far as I'm aware it's only a single queue listener rather than listening to all available queues, there's not really an API that suggests Laravel has an awareness of all possible queues. I've just run a worker for each queue name then I can adjust things like retries, and time to execute on a per-queue basis.
Just added a few more notes about how to automate starting the queues, should make it a bit easier. Not quite the wildcard you are looking for but should help you get a bit closer.
Hi Marcus, so I've set my supervisord instance to run: command=php /home/vagrant/myproj/artisan queue:work redis --queue=default,live,email,uploads --sleep=3 --tries=3 --daemon, but still, only the events and jobs on the default queue are being run, now, jobs which should go on the live queue aren't even being queued (even less helpful than before). What's going on here?
@YassineMokni thanks for the heads up. It’s been a while since I’ve used Laravel. I’ve updated my answer for any passers by.
|

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.