I am experimenting on how to write jobs on Laravel.
Following the Laravel documentation, I created a simple job that sends email using mail:queue.
I also added a cron job that will call the scheduler every minute which in turn will run the job every 5 minutes.
Everything works fine when I manually run the command queue:listen.
Later I added a print statement inside the scheduler and I found a strange issue.
Whenever I run the queue:listen from the terminal, I see the print message that is inside the schedule function and it gets printed every 5 seconds.
So my question is does queue:listen call the schedule function? If yes, why do we need to add schedule:run as a cron job?
The code that I used:
protected function schedule(Schedule $schedule){
error_log("inside scheduler");
$schedule->call(function () {
dispatch(new \App\Jobs\SendEmail);
})->everyFiveMinutes();
}
This is the command I ran on the terminal.
php artisan queue:listen
Output that I get (every 5 seconds)
inside scheduler
inside scheduler
inside scheduler
...
Can someone explain what is happening here?
-Adding the job code for better understanding
SendEmail.php
public function handle() {
//do some task here
Mail::queue("emails.send_update", [], function($message) {
$message->to("someone@somedomain.com");
$message->subject('Status has been updated');
});
}