modified update wallet journal job to dispatch multiple jobs

This commit is contained in:
2021-04-24 01:56:04 +09:00
parent 19447c6a11
commit 229aa84426
3 changed files with 156 additions and 6 deletions

View File

@@ -58,7 +58,7 @@ class UpdateAllianceWalletJournal implements ShouldQueue
$pages = $fHelper->GetAllianceWalletJournalPages(1, $config['primary']);
for($i = 1; $i < $pages; $i++) {
for($i = 1; $i <= $pages; $i++) {
UpdateAllianceWalletJournalPage::dispatch(1, $config['primary'], $page)->onQueue('finances');
}
}

View File

@@ -1,25 +1,45 @@
<?php
namespace App\Jobs;
namespace App\Jobs\Commands\Finances;
//Internal Library
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Carbon\Carbon;
use Log;
//Application Library
use Seat\Eseye\Exceptions\RequestFailedException;
use Seat\Eseye\Cache\NullCache;
use Seat\Eseye\Configuration;
use App\Library\Esi\Esi;
use App\Library\Helpers\LookupHelper;
//Models
use App\Models\Finances\AllianceWalletJournal;
class UpdateAllianceWalletJournalPage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $division;
private $charId;
private $page;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
public function __construct($division, $charId, $page)
{
//
$this->division = $division;
$this->charId = $charId;
$this->page = $page;
}
/**
@@ -29,6 +49,96 @@ class UpdateAllianceWalletJournalPage implements ShouldQueue
*/
public function handle()
{
//
//Declare variables in the handler
$lookup = new LookupHelper;
$esiHelper = new Esi;
//Setup the esi container.
$token = $esiHelper->GetRefreshToken($this->charId);
$esi = $esiHelper->SetupEsiAuthentication($token);
//Check the scope
if(!$esiHelper->HaveEsiScope($this->charId, 'esi-wallet.read_corporation_wallets.v1')) {
Log::critical('Scope check failed for esi-wallet.read_corporation_wallets.v1 for character id: ' . $charId);
return null;
}
if($esiHelper->TokenExpired($token)) {
$token = $esiHelper->GetRefreshToken($this->charId);
$esi = $esiHelper->SetupEsiAuthentication($token);
}
//Reference the character id to the corporation id
$char = $lookup->GetCharacterInfo($this->charId);
$corpId = $char->corporation_id;
/**
* Attempt to get the data from the esi api. If it fails, we skip the page, and go onto the next page, unless
* the failed page is the first page.
*/
try {
$journals = $esi->page($currentPage)
->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [
'corporation_id' => $corpId,
'division' => $division,
]);
} catch(RequestFailedException $e) {
Log::warning('Failed to get wallet journal page ' . $currentPage . ' for character id: ' . $charId);
Log::warning($e);
$this->delete();
}
//Decode the json data, and return it as an array
$wallet = json_decode($journals->raw, true);
//Foreach journal entry, add the journal entry to the table
foreach($wallet as $entry) {
//See if we find the entry id in the database already
$found = AllianceWalletJournal::where([
'id' => $entry['id'],
])->count();
if($found == 0) {
$awj = new AllianceWalletJournal;
$awj->id = $entry['id'];
$awj->corporation_id = $corpId;
$awj->division = $division;
if(isset($entry['amount'])) {
$awj->amount = $entry['amount'];
}
if(isset($entry['balance'])) {
$awj->balance = $entry['balance'];
}
if(isset($entry['context_id'])) {
$awj->context_id = $entry['context_id'];
}
if(isset($entry['date'])) {
$awj->date = $esiHelper->DecodeDate($entry['date']);
}
if(isset($entry['description'])) {
$awj->description = $entry['description'];
}
if(isset($entry['first_party_id'])) {
$awj->first_party_id = $entry['first_party_id'];
}
if(isset($entry['reason'])) {
$awj->reason = $entry['reason'];
}
if(isset($entry['ref_type'])) {
$awj->ref_type = $entry['ref_type'];
}
if(isset($entry['tax'])) {
$awj->tax = $entry['tax'];
}
if(isset($entry['tax_receiver_id'])) {
$awj->tax_receiver_id = $entry['tax_receiver_id'];
}
$awj->save();
}
}
//Return as completed
return 0;
}
}

View File

@@ -26,7 +26,6 @@ class FinanceHelper {
public function GetApiWalletJournal($division, $charId) {
//Declare class variables
$lookup = new LookupHelper;
$finance = new FinanceHelper;
$esiHelper = new Esi;
//Setup the esi container.
@@ -159,6 +158,47 @@ class FinanceHelper {
return 0;
}
/**
* Get the pages for the alliance wallet journal
*/
public function GetAllianceWalletJournalPages() {
$lookup = new LookupHelper;
$esiHelper = new Esi;
//Setup the esi container.
$token = $esiHelper->GetRefreshToken($charId);
$esi = $esiHelper->SetupEsiAuthentication($token);
//Check the scope
if(!$esiHelper->HaveEsiScope($charId, 'esi-wallet.read_corporation_wallets.v1')) {
Log::critical('Scope check failed for esi-wallet.read_corporation_wallets.v1 for character id: ' . $charId);
return null;
}
//Reference the character id to the corporation id
$char = $lookup->GetCharacterInfo($charId);
$corpId = $char->corporation_id;
/**
* Attempt to get the data from the esi api. If it fails, we skip the page, and go onto the next page, unless
* the failed page is the first page.
*/
try {
$journals = $esi->page(1)
->invoke('get', '/corporations/{corporation_id}/wallets/{division}/journal/', [
'corporation_id' => $corpId,
'division' => $division,
]);
} catch(RequestFailedException $e) {
Log::warning('Failed to get wallet journal pages for character id: ' . $charId);
Log::warning($e);
return 0;
}
//Return the total pages
return $journals->pages;
}
private function GetPIMaterialsArray() {
//Setup array for PI items
$pi_items = [