Understanding the queues or tasks


I have finished the part of an application dedicated to backups. Now when I try to implement it using the queues and tasks of laravel 5.3, I feel incapable.

I have some sort of commands

cpr:backups {full|acct} {full|account} {daily|weekly|monthly}
cpr:backup {account|system} {full|account} {daily|weekly|monthly}

The first cpr: backups can generate (or must generate because I have already lost) or a task or as many tasks as users are in the independent system, which should be "glued" to be executed later.

in cron

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

In the file that controls the backups

foreach ($users as $user)
   // $type puede ser daily|weekly|monthly
   $job = new MakeBackups($user->id,$model);

In the app \ Jobs \ MakeBackups.php file


namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Log;

class MakeBackups implements ShouldQueue
    use InteractsWithQueue, Queueable, SerializesModels;

    protected $userId;
    protected $model;

     * Create a new job instance.
     * @return void
    public function __construct($userId,$model)
        $this->userId = $userId;
        $this->model = $model;

     * Execute the job.
     * @return void
    public function handle()
        // Do something 
        Log::info('Make a backups for user with id '.$this->userId.' within the User class');

When I verify, a job has been effectively added to the jobs table by each of the users

queue -> default
payload -> {"job":"Illuminate\Queue\CallQueuedHandler@call","data":{"commandName":"App\Jobs\MakeBackups","command":"O:20:\"App\Jobs\MakeBackups\":5:{s:9:\"\u0000*\u0000userId\";s:1:\"1\";s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:5:\"delay\";N;}"}}

But if I try to manually execute the queue.

artisan schedule:run 
No scheduled commands are ready to run.

At this point, I understand that I have not comrpendido the basic operation of the tasks or queues, in deferred.

asked by abkrim 29.10.2016 в 11:42

1 answer


You are confusing or mixing two concepts that may be related, but which are different, at least in Laravel.

Queues or "queues" are jobs that run in the background, one after another, organized and in order not to reload the server, which will be executed when the server "can" do it, either 1 second or 1 hour after being added to the row, queue or queue. You could support yourself in php artisan queue:work to execute the jobs added to the row as soon as possible, depending on what you need to do in your application at a specific time.

Process the registration of a user (depending on the stages you have) can be added to the queue, send a verification mail or a reminder, or generate / store certain information that can take a long time or consume many resources (and if we do not want 10 of these processes to occur at the same time), they are examples of jobs that can be added to the queue and that do not need to be executed immediately.

Task scheduling is used to schedule tasks and execute them in a moment or with a determined frequency. To execute these tasks is to use the schedule run command, preferably in cron: * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 .

Making a backup of certain content at X exact time can be vital for the application, send the daily / weekly newsletter, clean log storage, indexing, are some examples of tasks that we should execute with certain frequency at a certain moment, and "must" be at that moment.

How could the two concepts be related?

A scheduled task (to be executed with schedule run ) can add jobs to the queue or queue, for example sending the newsletter should be done at midnight, but suppose it is not vital that all users receive it at that time, then we can add the work to the row by parts and it will be executed "when the server can or when your application requires / allows".

The same example can be applied to your backups: You could schedule the task of adding them to the queue at a specific time of the day.

The opposite is also possible: When you execute a job that you added to the queue or queue, it will add / generate a scheduled task that must be executed at a certain time.

answered by 29.10.2016 / 19:41