Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top