body = $body; $this->recipient = $recipient; $this->recipient_type = $recipient_type; $this->subject = $subject; $this->sender = $sender; $this->connection = 'redis'; } /** * Execute the job. * Utilized by using ProcessSendEveMailJob::dispatch($mail); * The model is passed into the dispatch function, then added to the queue * for processing. * * @return void */ public function handle() { //Declare some variables $esiHelper = new Esi; //Get the esi configuration $config = config('esi'); //Retrieve the token for main character to send mails from $token = $esiHelper->GetRefreshToken($config['primary']); //Create the ESI authentication container $esi = $esiHelper->SetupEsiAuthentication($token); //Attemp to send the mail try { $esi->setBody([ 'approved_cost' => 100, 'body' => $this->body, 'recipients' => [[ 'recipient_id' => $this->recipient, 'recipient_type' => $this->recipient_type, ]], 'subject' => $this->subject, ])->invoke('post', '/characters/{character_id}/mail/', [ 'character_id'=> $this->sender, ]); } catch(RequestFailedException $e) { Log::warning($e); return null; } $this->SaveSentRecord($this->sender, $this->subject, $this->body, $this->recipient, $this->recipient_type); $this->delete(); } /** * Middleware to only allow 4 jobs to be run per minute * After a failed job, the job is released back into the queue for at least 1 minute x the number of times attempted * */ public function middleware() { //Allow 4 jobs per minute, and implement a rate limited backoff on failed jobs $rateLimitedMiddleware = (new RateLimited()) ->allow(4) ->everySeconds(60) ->releaseAfterOneMinute() ->releaseAfterBackoff($this->attempts()); return [new RateLimited()]; } /* * Determine the time at which the job should timeout. * */ public function retryUntil() : \DateTime { return Carbon::now()->addDay(); } /** * The job failed to process. * * @param Exception $exception * @return void */ public function failed($exception) { Log::critical($exception); } private function SaveSentRecord($sender, $subject, $body, $recipient, $recipientType) { $sentmail = new SentMail; $sentmail->sender = $sender; $sentmail->subject = $subject; $sentmail->body = $body; $sentmail->recipient = $recipient; $sentmail->recipient_type = $recipientType; $sentmail->save(); } }