dpl/app/Jobs/SendEmailJob.php

46 lines
1.2 KiB
PHP

<?php
namespace App\Jobs;
use App\Services\MailService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $params;
public $tries = 3;
public $timeout = 30;
public $backoff = [15, 60, 180];
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($params, $queue = 'send_email')
{
$this->onQueue($queue);
$params['message_key'] ??= (string) \Illuminate\Support\Str::uuid();
$this->params = $params;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$this->params['message_key'] ??= $this->job?->uuid() ?? (string) \Illuminate\Support\Str::uuid();
$mailLog = MailService::sendEmail($this->params);
if ($mailLog['retryable'] ?? false) {
$this->release($this->backoff[min($this->attempts() - 1, count($this->backoff) - 1)]);
}
}
}