3

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

John
  • 339
  • 2
  • 7
  • 17
  • I think you better use cronjob – Amir Bar Dec 31 '13 at 12:00
  • Can you give me some feedback..?i am new to laravel and i need some advice on how to implement your proposal.. – John Dec 31 '13 at 12:21
  • Checkout a tutorial like http://glenntaylor.co.uk/blog/read/laravel-queues-with-beanstalkd about Laravel queues, it will really help you out understanding how queues work. – JackPoint Dec 31 '13 at 12:45

3 Answers3

8

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.

Community
  • 1
  • 1
aebersold
  • 11,286
  • 2
  • 20
  • 29
  • 1
    Where should i place my queue...i need it to run without the need to call a url... – John Dec 31 '13 at 12:36
  • Thank you very much..Is there some kind of tutorial to learn how to work with cron..?. – John Dec 31 '13 at 12:59
  • Yes, [here](http://maxoffsky.com/code-blog/practical-laravel-using-cron-jobs-in-laravel/). I'd appreciate it, if you accept my answer. – aebersold Dec 31 '13 at 13:33
2

You might have saved the SendEmail class where Laravel doesn't know where to look.

If you are using composer, then take a look at composer.json and the 'autoload' and 'classmap' array, then add your directory there. Remember to run 'composer dump-autoload' or 'composer update'.

Or try adding your directory to app/start/global.php ClassLoader function. That should help with the class not found.

WoodyDRN
  • 1,221
  • 20
  • 26
1

If you want to create a queue 'worker' process and have it run forever you should take a look at this: http://supervisord.org/ Supervisor will monitor your (worker) process and will restart it if it ever exits, fails on a fatal error, etc.

phirschybar
  • 8,357
  • 12
  • 50
  • 66