Laravel-4 : how to use queues to create a daemon

John picture John · Dec 31, 2013 · Viewed 7.8k times · Source

I need to create a Laravel daemon to get some data from the net and store them in a database.I would like to do this in Laravel in order to use Eloquent for my queries. I was told to take a look at queues but as I can see in the documentation a queue is called if you access a url first. Is there any way to start a queue and make it run forever? Will queues work in my local environment?So far I have the following code:

routes.php:

Route::get('daemon', function(){

    Queue::push('SendEmail', []);

});

SendEmail.php

<?php 

class SendEmail {

    public function fire($job, $data)
    {
       dd('ok');
    }

}

But I get class SendEmail does not exist

Answer

aebersold picture aebersold · Dec 31, 2013

If you want to run a task every n minutes, you should probably run a cronjob on an artisan command. This will fit your use case better.

Queues are something different. They are ment to be used to stack tasks in a queue list. In the background is a listener running, waiting for new tasks. Check the docs as well. You can start a listener by php artisan queue:listen in the console. The docs suggest to use supervisord to manage that task, altough its not necessary. The listener will run as long as you don't terminate it.

However, you can also combine artisan commands with queues. E.g. run a command via cronjob, which generates new queued tasks. The queue listener will later run these tasks.

Also, check this very good answer for cronjobs versus queues.