updated process send eve mail job

updated test command to work on resposne codes
This commit is contained in:
2021-03-16 01:00:02 +09:00
parent 85d39b867b
commit 61224e5611
4 changed files with 218 additions and 12 deletions

View File

@@ -49,11 +49,28 @@ class Test extends Command
*/
public function handle()
{
$helper = new FinanceHelper;
$esiHelper = new Esi;
$config = config('esi');
$startTime = time();
$receipt = $helper->GetApiWalletJournal(1, $config['primary']);
$endTime = time();
var_dump($endTime - $startTime);
$refreshToken = $esiHelper->GetRefreshToken($config['primary']);
$esi = $esiHelper->SetupEsiAuthentication($refreshToken);
try {
$response = $esi->setBody([
'approved_cost' => 100,
'body' => "Welcome to this test message.",
'recipients' => [[
'recipient_id' => $config['primary'],
'recipient_type' => 'character',
]],
'subject' => 'Just a Test',
])->invoke('post', '/characters/{character_id}/mail/', [
'character_id' => $config['primary'],
]);
} catch(RequestFailedException $e) {
return null;
}
dd($response);
}
}

View File

@@ -55,6 +55,7 @@ class MiningTaxesInvoices extends Command
$lookup = new LookupHelper;
$config = config('esi');
$task = new CommandHelper('MiningTaxesInvoices');
$mailDelay = 15;
//Set the task as started
$task->SetStartStatus();
@@ -125,7 +126,7 @@ class MiningTaxesInvoices extends Command
$recipient = $charId;
//Send the Eve Mail Job to the queue to be dispatched
ProcessSendEveMailJob::dispatch($body, $recipient, $recipientType, $subject, $sender)->onQueue('mail');
ProcessSendEveMailJob::dispatch($body, $recipient, $recipientType, $subject, $sender)->onQueue('mail')->delay(Carbon::now()->addSeconds($mailDelay));
//Save the invoice model
$invoice = new Invoice;
@@ -147,6 +148,9 @@ class MiningTaxesInvoices extends Command
'invoiced' => 'Yes',
'invoice_id' => $invoiceId,
]);
//update the delay
$mailDelay = $mailDelay + 20;
}
//Set the task as stopped

View File

@@ -42,6 +42,13 @@ class ProcessSendEveMailJob implements ShouldQueue
*/
//public $retries = 3;
/**
* Middleware for the job
*
* @var \Spatie\RateLimitedMiddleware\RateLimited
*/
private $middleware;
private $sender;
private $body;
private $recipient;
@@ -54,13 +61,17 @@ class ProcessSendEveMailJob implements ShouldQueue
* @return void
*/
public function __construct($body, $recipient, $recipient_type, $subject, $sender) {
//Set the connection
$this->connection = 'redis';
//Private variables
$this->body = $body;
$this->recipient = $recipient;
$this->recipient_type = $recipient_type;
$this->subject = $subject;
$this->sender = $sender;
$this->connection = 'redis';
}
/**
@@ -80,14 +91,19 @@ class ProcessSendEveMailJob implements ShouldQueue
$config = config('esi');
//Retrieve the token for main character to send mails from
$token = $esiHelper->GetRefreshToken($config['primary']);
$refreshToken = $esiHelper->GetRefreshToken($config['primary']);
//Create the ESI authentication container
$esi = $esiHelper->SetupEsiAuthentication($token);
$esi = $esiHelper->SetupEsiAuthentication($refreshToken);
//Check to see if the token is valid or not
if($esiHelper->TokenExpired($refreshToken)) {
$refreshToken = $esiHelper->GetRefreshToken($config['primary']);
$esi = $esiHelper->SetupEsiAuthentication($refreshToken);
}
//Attemp to send the mail
try {
$esi->setBody([
$reponse = $esi->setBody([
'approved_cost' => 100,
'body' => $this->body,
'recipients' => [[
@@ -122,7 +138,7 @@ class ProcessSendEveMailJob implements ShouldQueue
->releaseAfterOneMinute()
->releaseAfterBackoff($this->attempts());
return [new RateLimited()];
return [$rateLimitedMiddleware];
}
/*

View File

@@ -0,0 +1,169 @@
<?php
namespace App\Jobs\Commands\Eve;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Spatie\RateLimitedMiddleware\RateLimited;
use Log;
use Carbon\Carbon;
//Library
use App\Library\Esi\Esi;
use Seat\Eseye\Exceptions\RequestFailedException;
use Seat\Eseye\Cache\NullCache;
use Seat\Eseye\Configuration;
//Models
use App\Models\Esi\EsiScope;
use App\Models\Esi\EsiToken;
use App\Models\Jobs\JobStatus;
use App\Models\Mail\SentMail;
class ProcessSendEveMailJobRL implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
* With new rate limiting, we shouldn't use this timeout
* @var int
*/
//public $timeout = 3600;
/**
* Retries
* With new rate limiting, we shouldn't use this timeout
* @var int
*/
//public $retries = 3;
/**
* Middleware for the job
*
* @var \Spatie\RateLimitedMiddleware\RateLimited
*/
private $middleware;
private $sender;
private $body;
private $recipient;
private $recipient_type;
private $subject;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($body, $recipient, $recipient_type, $subject, $sender) {
//Set the connection
$this->connection = 'redis';
//Private variables
$this->body = $body;
$this->recipient = $recipient;
$this->recipient_type = $recipient_type;
$this->subject = $subject;
$this->sender = $sender;
}
/**
* 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 [$rateLimitedMiddleware];
}
/*
* 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();
}
}