artisan jobs schedule

This commit is contained in:
2021-03-30 19:54:26 +09:00
parent e6022ded9c
commit 40ed5dd5a5
14 changed files with 725 additions and 19 deletions

View File

@@ -19,7 +19,7 @@ class MiningTaxesDataCleanup extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'MiningTaxes:CleanupData'; protected $signature = 'mining:cleanup';
/** /**
* The console command description. * The console command description.

View File

@@ -27,7 +27,7 @@ class MiningTaxesInvoices extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'MiningTax:Invoice'; protected $signature = 'mining:Invoice';
/** /**
* The console command description. * The console command description.

View File

@@ -29,7 +29,7 @@ class MiningTaxesInvoicesNew extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'MiningTax:InvoiceNew'; protected $signature = 'mining:invoice_new';
/** /**
* The console command description. * The console command description.

View File

@@ -11,7 +11,7 @@ class MiningTaxesLateInvoices extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'command:name'; protected $signature = 'mining:late';
/** /**
* The console command description. * The console command description.

View File

@@ -23,7 +23,7 @@ class MiningTaxesLedgers extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'MiningTax:Ledgers'; protected $signature = 'mining:Ledgers';
/** /**
* The console command description. * The console command description.

View File

@@ -27,7 +27,7 @@ class MiningTaxesObservers extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'MiningTax:Observer'; protected $signature = 'mining:Observer';
/** /**
* The console command description. * The console command description.

View File

@@ -28,7 +28,7 @@ class MiningTaxesPayments extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'MiningTax:Payments'; protected $signature = 'mining:Payments';
/** /**
* The console command description. * The console command description.

View File

@@ -6,6 +6,15 @@ namespace App\Console;
use Illuminate\Console\Scheduling\Schedule; use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
//Jobs
use App\Jobs\Commands\MiningTaxes\FetchMiningTaxesLedgersJob;
use App\Jobs\Commands\MiningTaxes\FetchMiningTaxesObserversJob;
use App\Jobs\Commands\MiningTaxes\ProcessMiningTaxesPaymentsJob;
use App\Jobs\Commands\MiningTaxes\SendMiningTaxesInvoicesJob;
use App\Jobs\Commands\Finances\UpdateAllianceWalletJournalJob;
use App\Jobs\Commands\Finances\UpdateItemPricesJob;
use App\Jobs\Commands\Data\PurgeUsersJob;
class Kernel extends ConsoleKernel class Kernel extends ConsoleKernel
{ {
/** /**
@@ -18,6 +27,7 @@ class Kernel extends ConsoleKernel
Commands\Data\Test::class, Commands\Data\Test::class,
Commands\Eve\ItemPricesUpdateCommand::class, Commands\Eve\ItemPricesUpdateCommand::class,
Commands\Finances\UpdateAllianceWalletJournal::class, Commands\Finances\UpdateAllianceWalletJournal::class,
Commands\MiningTaxes\MiningTaxesDataCleanup::class,
Commands\MiningTaxes\MiningTaxesInvoices::class, Commands\MiningTaxes\MiningTaxesInvoices::class,
Commands\MiningTaxes\MiningTaxesInvoicesNew::class, Commands\MiningTaxes\MiningTaxesInvoicesNew::class,
Commands\MiningTaxes\MiningTaxesLedgers::class, Commands\MiningTaxes\MiningTaxesLedgers::class,
@@ -42,32 +52,39 @@ class Kernel extends ConsoleKernel
/** /**
* Purge Data Schedule * Purge Data Schedule
*/ */
$schedule->command('data:PurgeUsers') $schedule->job(new PurgeUsersJob)->onQueue('default')
->dailyAt('23:00'); ->weekly();
/** /**
* Finances Update Schedule * Finances Update Schedule
*/ */
$schedule->command('finances:UpdateJournals') $schedule->job(new UpdateAllianceWalletJournalJob)->onQueue('default')
->timezone('UTC')
->hourlyAt('45') ->hourlyAt('45')
->withoutOverlapping(); ->withoutOverlapping();
/** /**
* Item Update Schedule * Item Update Schedule
*/ */
$schedule->command('services:ItemPriceUpdate') $schedule->job(new UpdateItemPricesJob)->onQueue('default')
->hourlyAt('30'); ->timezone('UTC')
->hourlyAT('30')
->withoutOverlapping();
/** /**
* Mining Tax Schedule * Mining Tax Schedule
*/ */
$schedule->command('MiningTax:Observers') $schedule->job(new FetchMiningTaxesObserversJob)->onQueue('miningtaxes')
->timezone('UTC')
->dailyAt('22:00'); ->dailyAt('22:00');
$schedule->command('MiningTax:Ledgers') $schedule->job(new FetchMiningTaxesLedgersJob)->onQueue('miningtaxes')
->timezone('UTC')
->dailyAt('20:00'); ->dailyAt('20:00');
$schedule->command('MiningTax:Invoices') $schedule->job(new SendMiningTaxesInvoicesJob)->onQueue('miningtaxes')
->weeklyOn(1, '6:00'); ->timezone('UTC')
$schedule->command('MiningTax:Payments') ->weeklyOn(1, '06:00');
$schedule->job(new ProcessMiningTaxesPaymentsJob)->onQueue('miningtaxes')
->timezone('UTC')
->hourlyAt('15'); ->hourlyAt('15');
} }

View File

@@ -0,0 +1,193 @@
<?php
namespace App\Jobs\Commands\Data;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use Carbon\Carbon;
//Libraries
use Seat\Eseye\Exceptions\RequestFailedException;
use App\Library\Esi\Esi;
//Models
use App\Models\User\User;
use App\Models\User\UserAlt;
use App\Models\Esi\EsiScope;
use App\Models\Esi\EsiToken;
use App\Models\User\UserPermission;
use App\Models\User\UserRole;
use App\Models\Admin\AllowedLogin;
class PurgeUsersJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3600;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare some variables
$esiHelper = new Esi;
//Setup the esi variable
$esi = $esiHelper->SetupEsiAuthentication();
//Get all of the users from the database
$users = User::all();
//Get the allowed logins
$legacy = AllowedLogin::where(['login_type' => 'Legacy'])->pluck('entity_id')->toArray();
$renter = AllowedLogin::where(['login_type' => 'Renter'])->pluck('entity_id')->toArray();
//Cycle through all of the users, and either update their role, or delete them.
foreach($users as $user) {
//Set the fail bit to false for the next user to check
$failed = false;
//Note a screen entry for when doing cli stuff
printf("Processing character with id of " . $user->character_id . "\r\n");
//Get the character information
try {
$character_info = $esi->invoke('get', '/characters/{character_id}/', [
'character_id' => $user->character_id,
]);
$corp_info = $esi->invoke('get', '/corporations/{corporation_id}/', [
'corporation_id' => $character_info->corporation_id,
]);
} catch(RequestFailedException $e) {
Log::warning('Failed to get character information in purge user command for user ' . $user->character_id);
$failed = true;
}
//If the fail bit is still false, then continue
if($failed === false) {
//Get the user's role
$role = UserRole::where(['character_id' => $user->character_id])->first();
//We don't want to modify Admin and SuperUsers. Admins and SuperUsers are removed via a different process.
if($role->role != 'Admin') {
//Check if the user is allowed to login
if(isset($corp_info->alliance_id)) {
//Warped Intentions is allowed to login
if($corp_info->alliance_id == '99004116') {
//If the alliance is Warped Intentions, then modify the role if we need to
if($role->role != 'User') {
//Upate the role of the user
UserRole::where([
'character_id' => $user->character_id,
])->update([
'role' => 'User',
]);
//Update the user type
User::where([
'character_id' => $user->character_id,
])->update([
'user_type' => 'W4RP',
]);
}
} else if(in_array($corp_info->alliance_id, $legacy)) { //Legacy Users
if($role->role != 'User') {
//Update the role of the user
UserRole::where([
'character_id' => $user->character_id,
])->update([
'role' => 'User',
]);
//Update the user type
User::where([
'character_id' => $user->character_id,
])->update([
'user_type' => 'Legacy',
]);
}
} else if(in_array($corp_info->alliance_id, $renter)) { //Renter Users
if($role->role != 'Renter') {
//Update the role of the user
UserRole::where([
'character_id' => $user->character_id,
])->update([
'role' => 'Renter',
]);
//Update the user type
User::where([
'character_id' => $user->character_id,
])->update([
'user_type' => 'Renter',
]);
}
} else {
//If the user is part of no valid login group, then delete the user.
//Delete all of the permissions first
UserPermission::where([
'character_id' => $user->character_id,
])->delete();
//Delete the user's role
UserRole::where([
'character_id' => $user->character_id,
])->delete();
//Delete any alts the user might have registered.
$altCount = UserAlt::where(['main_id' => $user->character_id])->count();
if($altCount > 0) {
UserAlt::where([
'main_id' => $user->character_id,
])->delete();
}
//Delete the user from the user table
User::where([
'character_id' => $user->character_id,
])->delete();
EsiScope::where([
'character_id' => $user->character_id,
])->delete();
EsiToken::where([
'character_id' => $user->character_id,
])->delete();
}
}
}
}
}
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use Carbon\Carbon;
//Application Library
use App\Library\Helpers\FinanceHelper;
use Commands\Library\CommandHelper;
//Models
use App\Models\Finances\AllianceWalletJournal;
class UpdateAllianceWalletJournalJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 1800;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare variables
$fHelper = new FinanceHelper;
$config = config('esi');
$fHelper->GetApiWalletJournal(1, $config['primary']);
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Jobs\Commands\Finances;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use Carbon\Carbon;
class UpdateItemPricesJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 1800;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare variables
$moonHelper = new MoonCalc;
//Fetch new prices from fuzzwork.co.uk for the item pricing schemes
$moonHelper->FetchNewPrices();
}
}

View File

@@ -0,0 +1,188 @@
<?php
namespace App\Jobs\Commands\MiningTaxes;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use Carbon\Carbon;
//Application Library
use Seat\Eseye\Exceptions\RequestFailedException;
use App\Library\Esi\Esi;
use App\Library\Helpers\LookupHelper;
use App\Library\Helpers\StructureHelper;
class FetchMiningTaxesObserversJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 1800;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare variables
$config = config('esi');
$lookup = new LookupHelper;
$esiHelper = new Esi;
//Check for the esi scope
if(!$esiHelper->HaveEsiScope($config['primary'], 'esi-industry.read_corporation_mining.v1')) {
Log::critical('Esi scopes were not found for FetchMiningTaxesObserversJob.');
print("Esi scopes not found.");
return;
}
$char = $lookup->GetCharacterInfo($config['primary']);
//Get the refresh token for the character
$refreshToken = $esiHelper->GetRefreshToken($config['primary']);
//Get the esi variable
$esi = $esiHelper->SetupEsiAuthentication($refreshToken);
$response = $esi->invoke('get', '/corporation/{corporation_id}/mining/observers/', [
'corporation_id' => $char->corporation_id,
]);
$resp = json_decode($response->raw, false);
//Run through the mining observers, and add them to the database
foreach($resp as $observer) {
if($observer->observer_id > 1030000000000) {
$found = Observer::where([
'observer_id' => $observer->observer_id,
])->count();
if($found > 0) {
Observer::where([
'observer_id' => $observer->observer_id,
])->update([
'observer_id' => $observer->observer_id,
'observer_type' => $observer->observer_type,
'last_updated' => $observer->last_updated,
]);
} else {
$newObs = new Observer;
$newObs->observer_id = $observer->observer_id;
$newObs->observer_type = $observer->observer_type;
$newObs->last_updated = $observer->last_updated;
$newObs->save();
}
}
}
/**
* Cleanup stale data that hasn't been updated in at least 1 week.
*/
$date = Carbon::now()->subDays(7);
Observer::where('last_updated', '<', $date)->delete();
//Set the task as completed
$task->SetStopStatus();
//Set the end time for debugging and printint out to the screen
$endTime = time();
printf("Time to complete: " . ($endTime - $startTime) . "\n\r");
//Return 0 saying everything is fine
return 0;
}
/**
* The job failed to process
* @param Exception $exception
* @return void
*/
public function failed($exception) {
if(!exception instanceof RequestFailedException) {
//If not a failure due to ESI, then log it. Otherwise,
//deduce why the exception occurred.
Log::critical($exception);
}
if ((is_object($exception->getEsiResponse()) && (stristr($exception->getEsiResponse()->error, 'Too many errors') || stristr($exception->getEsiResponse()->error, 'This software has exceeded the error limit for ESI'))) ||
(is_string($exception->getEsiResponse()) && (stristr($exception->getEsiResponse(), 'Too many errors') || stristr($exception->getEsiResponse(), 'This software has exceeded the error limit for ESI')))) {
//We have hit the error rate limiter, wait 120 seconds before releasing the job back into the queue.
Log::info('FetchMiningTaxesObservers has hit the error rate limiter. Releasing the job back into the wild in 2 minutes.');
$this->release(120);
} else {
$errorCode = $exception->getEsiResponse()->getErrorCode();
switch($errorCode) {
case 400: //Bad Request
Log::critical("Bad request has occurred in FetchMiningTaxesObservers. Job has been discarded");
break;
case 401: //Unauthorized Request
Log::critical("Unauthorized request has occurred in FetchMiningTaxesObservers at " . Carbon::now()->toDateTimeString() . ".\r\nCancelling the job.");
break;
case 403: //Forbidden
Log::critical("FetchMiningTaxesObservers has incurred a forbidden error. Cancelling the job.");
break;
case 420: //Error Limited
Log::warning("Error rate limit occurred in FetchMiningTaxesObservers. Restarting job in 120 seconds.");
$this->release(120);
break;
case 500: //Internal Server Error
Log::critical("Internal Server Error for ESI in FetchMiningTaxesObservers. Attempting a restart in 120 seconds.");
$this->release(120);
break;
case 503: //Service Unavailable
Log::critical("Service Unavailabe for ESI in FetchMiningTaxesObservers. Releasing the job back to the queue in 30 seconds.");
$this->release(30);
break;
case 504: //Gateway Timeout
Log::critical("Gateway timeout in FetchMiningTaxesObservers. Releasing the job back to the queue in 30 seconds.");
$this->release(30);
break;
case 201: //Good response code
$this->delete();
break;
//If no code is given, then log and break out of switch.
default:
Log::warning("No response code received from esi call in FetchMiningTaxesObservers.\r\n");
$this->delete();
break;
}
}
}
/**
* Set the tags for Horizon
*
* @var array
*/
public function tags() {
return ['FetchMiningObservers'];
}
}

View File

@@ -0,0 +1,196 @@
<?php
namespace App\Jobs\Commands\MiningTaxes;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use Carbon\Carbon;
//Application Library
use App\Library\Helpers\LookupHelper;
//Models
use App\Models\MiningTax\Invoice;
use App\Models\MiningTax\Ledger;
use App\Models\User\UserAlt;
use App\Models\User\User;
//Jobs
use App\Jobs\Commands\Eve\ProcessSendEveMailJob;
class SendMiningTaxesInvoicesJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 3600;
/**
* Retries
*
* @var int
*/
public $retries = 3;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare variables
$lookup = new LookupHelper;
$config = config('esi');
$mailDelay = 15;
//Get the characters for each non-invoiced ledger entry
$charIds = Ledger::where([
'invoiced' => 'No',
])->distinct('character_id')
->pluck('character_id');
if($charIds == null) {
//Set the task status as done and Log the issue
$task->SetStopStatus();
Log::warning("No characters found to send invoices to in MiningTaxesInvoices Command.");
return 0;
}
//Foreach character tally up the mining ledger.
foreach($charIds as $charId) {
//Declare some variables we need for each iteration of the loop
$invoice = array();
$ores = array();
$totalPrice = 0.00;
$body = null;
//Get the row count to reduce overhead processing if we can.
$rowCount = Ledger::where([
'character_id' => $charId,
'invoiced' => 'No',
])->count();
//Get the actual rows
$rows = Ledger::where([
'character_id' => $charId,
'invoiced' => 'No',
])->get()->toArray();
//Taly up the item composition from each row and multiply by the quantity
if($rowCount > 0) {
foreach($rows as $row) {
if(!isset($ores[$row['type_id']])) {
$ores[$row['type_id']] = 0;
}
$ores[$row['type_id']] = $ores[$row['type_id']] + $row['quantity'];
//Add up the total price from the ledger rows for the report later
$totalPrice = $totalPrice + $row['amount'];
}
//Reduce the total price by the take percentage
$invoiceAmount = $totalPrice * $config['mining_tax'];
$invoiceAmount = round($invoiceAmount, 2);
//Get the character name from the character id
$charName = $lookup->CharacterIdToName($charId);
//Generate a unique invoice id
$invoiceId = uniqid();
//Set the due date of the invoice
$dateDue = Carbon::now()->addDays(7);
$invoiceDate = Carbon::now();
//Format the mining tax into a human readable number
$numberMiningTax = number_format(($config['mining_tax'] * 100.00), 2, ".", ",");
//Create the mail body
$body .= "Dear Miner,<br><br>";
$body .= "Mining Taxes are due for the following ores mined from alliance moons: <br>";
foreach($ores as $ore => $quantity) {
$oreName = $lookup->ItemIdToName($ore);
$body .= $oreName . ": " . number_format($quantity, 0, ".", ",") . "<br>";
}
$body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK.";
$body .= "<br><br>";
$body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "<br>";
$body .= "Set the reason for transfer as MMT: " . $invoiceId . "<br>";
$body .= "The mining taxes are currently set to " . $numberMiningTax . "%.<br>";
$body .= "<br><br>";
$body .= "You can also send a contract with the following ores in the contract with the reason set as MMT: " . $invoiceId . "<br>";
foreach($ores as $ore => $quantity) {
$oreName = $lookup->ItemIdToName($ore);
$body .= $oreName . ": " . number_format(round($quantity * $config['mining_tax']), 0, ".", ",") . "<br>";
}
$body .= "<br>";
$body .= "<br>Sincerely,<br>Warped Intentions Leadership<br>";
//Check if the mail body is greater than 2000 characters. If greater than 2,000 characters, then
if(strlen($body) > 2000) {
$body = "Dear Miner,<br><br>";
$body .= "Total Value of Ore Mined: " . number_format($totalPrice, 2, ".", ",") . " ISK.";
$body .= "<br><br>";
$body .= "Please remit " . number_format($invoiceAmount, 2, ".", ",") . " ISK to Spatial Forces by " . $dateDue . "<br>";
$body .= "Set the reason for transfer as MMT: " . $invoiceId . "<br>";
$body .= "The mining taxes are currently set to " . $numberMiningTax . "%.<br>";
$body .= "<br>";
$body .= "<br>Sincerely,<br>Warped Intentions Leadership<br>";
}
//Mail the invoice to the character if the character is in
//Warped Intentions or Legacy
$subject = 'Warped Intentions Mining Taxes';
$sender = $config['primary'];
$recipientType = 'character';
$recipient = $charId;
//Send the Eve Mail Job to the queue to be dispatched
ProcessSendEveMailJob::dispatch($body, $recipient, $recipientType, $subject, $sender)->onQueue('mail')->delay(Carbon::now()->addSeconds($mailDelay));
//Save the invoice model
$invoice = new Invoice;
$invoice->character_id = $charId;
$invoice->character_name = $charName;
$invoice->invoice_id = $invoiceId;
$invoice->invoice_amount = $invoiceAmount;
$invoice->date_issued = $invoiceDate;
$invoice->date_due = $dateDue;
$invoice->status = 'Pending';
$invoice->mail_body = $body;
$invoice->save();
//Update the ledger entries
Ledger::where([
'character_id' => $charId,
'invoiced' => 'No',
])->update([
'invoiced' => 'Yes',
'invoice_id' => $invoiceId,
]);
//update the delay
$mailDelay = $mailDelay + 20;
}
}
}
}

View File

@@ -81,8 +81,6 @@ return [
'block_for' => null, 'block_for' => null,
], ],
], ],
/* /*