2

I know, the question is very strange...

Scenario: I have a Job class that sends an email, but the content of this email is modified, since the template of the email is selected before being of dispatch.

I do not know if it's a truth, but apparently Laravel maintain a cache of content that he fired for the first time. Even by changing the value of the properties, Job sends exactly the same email.

If this is true, I would like to know how to use the same job class to send different emails or what would be the best alternative.

\app\Jobs\SenderEmail001.php

/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($template_id, $subject)
{
    $this->template_id = $template_id;

    $this->subject     = $subject;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    $template = Template::findOrFail($this->template_id);

    \Mail::send([], [], function($message) use ($template)
    {
        $message
            ->replyTo('source@domain.com', 'Email Source')
            ->from('source@domain.com', 'Email Source')
            ->to('target@domain.com', 'Email Target')
            ->subject($this->subject)
            ->setBody($template->markup, 'text/html');
    });

}

MyController

\App\Jobs\SenderEmail001::dispatch(6, 'subject subject subject')
    ->delay(now()->addSecond(100))
    ->onQueue('default');
Magno Alberto
  • 628
  • 14
  • 27
  • 1
    Did you try restarting queue? Or setting queue driver to sync? Just for testing purposes? Normally this should not be happening. – Nick Surmanidze Feb 15 '19 at 12:31
  • Thanks for the feedback. It really is weird ... If I do it directly with the \Mail class along with later(), it works correctly. The only detail is that if I use \Mail::later (), it does not let it be a raw e-mail, but it works normally, that is, the queue "appears" is ok ... since I can postpone the sending email. And answering your question: yes, the queue is QUEUE_CONNECTION=database. – Magno Alberto Feb 15 '19 at 12:44
  • Are you sure you can have an empty array as the first argument of mail::send? I usually use mailables so haven't tried your approach. Does it work outside the queue? – Nick Surmanidze Feb 15 '19 at 13:01
  • Or maybe you could try mail::raw('subject', function(){}); ? – Nick Surmanidze Feb 15 '19 at 13:02
  • I do not know if there is any sense but apparently if I make some change in the Job class, I need to stop the "artisan queue:work" and run again. From what I've been able to observe, as queue:work keeps running constantly by Supervisor, changes in in my Job class seem to take effect only after rerun "artisan queue:work". – Magno Alberto Feb 15 '19 at 14:11
  • 1
    Yes, that's true. You need to restart the queue every time whenever you edit job class, because it is being cached. That's why I asked before if you tried restarting the queue. I would recommend using laravel horizon as well. – Nick Surmanidze Feb 15 '19 at 14:35

1 Answers1

0

Because queue workers are long-lived processes, they will not recognise code changes without restarting. To gracefully restart the workers during deployment... run.

php artisan queue:restart

see more: https://laravel.com/docs/5.7/queues#queue-workers-and-deployment