0

When sending an email with queue job in laravel, it creates two processes, why? Is there a way to fix and keep only one? Is there any function that does this? for each dispatch, a single process.

Controller

public function sendMail()
{
    $email = "gustavo.gomes@teste.com.br";
    SendMail::dispatch($email);

    return view('site.email-enviado');    
}

Job

public function handle()
{
    Mail::to('teste@test.com')->queue(new MensagemTesteMail($this->email)); 
}

Email

public function build()
{
    return $this->subject('gustavocamalionti@gmail.com')
        ->from(env("MAIL_FROM_ADDRESS", null), 'Teste email')
        ->view('emails.cadastro-sucesso');
}

php artisan queue:work

CLI

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43

1 Answers1

1

You are not executing 2 processes (job workers)... You only have one...

  1. The first one is literally saying it is processing A job (SendMail) you have sent to the queue
  2. The second line is saying that the SendMail job was processed
  3. The third one is literally saying it is processing A job (MensagemTesteMail) (I recommend a lot to ONLY CODE IN ENGLISH)
  4. The fourth one is saying that the MensagemTesteMail job was processed

You have 2 jobs, because the first job (SendMail) is executing Mail::to('teste@test.com')->queue(new MensagemTesteMail($this->email)); and that literally creates the Mail (MensagemTesteMail). It is a job because you have it as so... You literally have ->queue(new MensagemTesteMail($this->email)), so that is the second job...

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • So what's the solution? I mean obviously he doesn't need the `SendMail` job if `MessageTesteMail` is queueable :) – dbf Jan 13 '23 at 13:26
  • So, is it possible to create just one job, in this case, the send email job? instead of generating two? I have this conceptual problem that is driving me crazy kkkkk I need that, when the job sends the email, it saves it in a third table in order to create a grid in my system that has the sending status "success, failure, pending". – Gustavo Gomes Jan 13 '23 at 13:39
  • I thought of doing it this way: in the job handle, create the record in the third table saving the uuid and other information with pending status, in AppServiceProvder, using Queue::after check if it failed or not, if not failed, update for success status , if it failed, it updated the status to fail. – Gustavo Gomes Jan 13 '23 at 13:41
  • It was the solution I found to be able to relate the job or the job_failed (if it happened), with the third table. make an insert with the uuid in a third table when the "handle" is triggered – Gustavo Gomes Jan 13 '23 at 13:43
  • 1
    @GustavoGomes So to come to terms, you don’t need the `SendMail` job, just use the mailable class and work from there. – dbf Jan 13 '23 at 14:05
  • I agree, but I would need to save the uuid of the job because if there is an error in the submission, I can consult this third table and save an exception from the jobs_failed table. – Gustavo Gomes Jan 13 '23 at 14:20
  • Is there a way to retrieve the uuid (getJobId()), in the mailable? – Gustavo Gomes Jan 13 '23 at 14:20
  • I applied queue and job because the idea would be for the user to send several emails and be able to continue discussing in the application. – Gustavo Gomes Jan 13 '23 at 14:20
  • 1
    @GustavoGomes ofcourse, don’t queue the mail in your job. Send the mail from your Job directly. – dbf Jan 17 '23 at 20:25