0
votes

I have a job to send emails when the user registers in the application.

SendWelcomeEmail.php

<?php

namespace App\Jobs;

use App\Jobs\Job;
use App\User;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendWelcomeEmail extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $user;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(Mailer $mailer)
    {
        $user = &$this->user;

        $message = sprintf('Hello %s', $user->name);

        $mailer->raw($message, function ($m) use ($user){
            $m->from('[email protected]', 'Lucas Lopes');
            $m->to($user->email, $user->name);
        });
    }
}

I would like to create a job to execute the php artisan queue: work command every minute to send the emails that are in the queue.

2

2 Answers

0
votes

You are wanting to run the queue daemon, not run a command. You need to do this within your server environment, not using laravels scheduled commands.

You could use something like Linux's screen to place your command in.

This will consinuiously poll your queue server for new jobs, then process them.

0
votes

Easiest way would be to set up Supervisor.

https://laravel.com/docs/5.4/queues#supervisor-configuration, it will automatically restart your queue:work process if it fails.

To solve the confusion for you:

In app/Console/Kernel.php file, inside the schedule function you would add a command that you want to be ran at a specific interval. An example would be : $schedule->command('SendFailedLoginsReport')->weekly()->mondays()->at('03:00');

On your server, you can add your cron like this * * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

This Cron will call the Laravel command scheduler every minute