updated all jobs to use queue

This commit is contained in:
2019-05-04 22:50:32 -05:00
parent 65a6759f08
commit d7717e5468
12 changed files with 544 additions and 133 deletions
+5
View File
@@ -2,9 +2,11 @@
namespace Commands\Library;
//Internal Libraries
use DB;
use Carbon\Carbon;
//Models
use App\Models\ScheduledTask\ScheduleJob;
class CommandHelper {
@@ -38,6 +40,9 @@ class CommandHelper {
]);
}
public function CleanJobStatusTable() {
DB::table('schedule_jobs')->where('system_time', '<', Carbon::now()->subMonths(6))->delete();
}
}
?>
+30 -28
View File
@@ -6,21 +6,21 @@ use Illuminate\Console\Command;
use Carbon\Carbon;
use DB;
use App\Jobs\SendEveMail;
//Jobs
use App\Jobs\SendEveMailJob;
//Libraries
use Commands\Library\CommandHelper;
use App\Library\Finances\Helper\FinanceHelper;
use App\Library\Structures\StructureTaxHelper;
use App\Library\Esi\Esi;
use App\Library\Esi\Mail;
//Models
use App\Models\Market\MonthlyMarketTax;
use App\Models\ScheduledTask\ScheduleJob;
use App\Models\Finances\CorpMarketJournal;
use App\Models\Corporation\CorpStructure;
use App\Models\User\UserToCorporation;
use App\Models\Mail\EveMail;
use App\Models\Esi\EsiScope;
use App\Models\Esi\EsiToken;
use App\Models\Jobs\JobSendEveMail;
class CalculateMarketTaxCommand extends Command
{
@@ -73,29 +73,31 @@ class CalculateMarketTaxCommand extends Command
$corps = CorpStructure::select('corporation_id')->groupBy('corporation_id')->get();
$this->line('Got all of the corps with markets.' . sizeof($corps));
foreach($corps as $corp) {
$finalTaxes = $sHelper->GetTaxes($corp->corporation_id, 'Market', $start, $end);
if($finalTaxes < 0.00) {
$finalTaxes = 0.00;
if($corp->corporation_id != 98287666) {
$finalTaxes = $sHelper->GetTaxes($corp->corporation_id, 'Market', $start, $end);
if($finalTaxes < 0.00) {
$finalTaxes = 0.00;
}
//Get the info about the structures from the database
$info = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first();
$character = UserToCorporation::where(['character_id' => $info->character_id])->first();
$mail = new JobSendEveMail;
$mail->sender = 93738489;
$mail->subject = 'Market Taxes Owed';
$mail->body = 'Year ' . $start->year . ' ' .
'Month: ' .
$start->month .
'<br>Market Taxes Owed: ' .
number_format($finalTaxes, 2, '.', ',') .
'<br>Please remit to Spatial Forces';
$mail->recipient = (int)$info->character_id;
$mail->recipient_type = 'character';
SendEveMailJob::dispatch($mail);
}
//Get the info about the structures from the database
$info = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first();
$character = UserToCorporation::where(['character_id' => $info->character_id])->first();
$mail = new EveMail;
$mail->sender = 93738489;
$mail->subject = 'Market Taxes Owed';
$mail->body = 'Year ' . $start->year . ' ' .
'Month: ' .
$start->month .
'<br>Market Taxes Owed: ' .
number_format($finalTaxes, 2, '.', ',') .
'<br>Please remit to Spatial Forces';
$mail->recipient = (int)$info->character_id;
$mail->recipient_type = 'character';
//$mail->save();
SendEveMail::dispatch($mail);
}
//Mark the job as finished
+26 -2
View File
@@ -15,6 +15,7 @@ use App\Jobs\ProcessWalletJournalJob;
//Models
use App\Models\Corporation\CorpStructure;
use App\Models\Jobs\JobProcessWalletJournal;
class CorpJournalCommand extends Command
{
@@ -51,23 +52,46 @@ class CorpJournalCommand extends Command
{
//Create the command helper container
$task = new CommandHelper('CorpJournal');
//Add the entry into the jobs table saying the job is starting
$task->SetStartStatus();
//Setup the Finances Container
$finance = new FinanceHelper();
//Setup an array to store corporations which have been logged so we don't keep calling the same ones. We need
//this step in order to save time during the cronjob.
$finishedCorps = array();
$corpCompleted = false;
//Get the corps with structures logged in the database
$corps = CorpStructure::select('corporation_id')->groupBy('corporation_id')->get();
//For all of the corporations, go through each structure and get wallet data
foreach($corps as $corp) {
if($charId->character_id != 93738489) {
//If the corporation isn't the holding corporation, then process the data.
//We process holding corporation data elsewhere.
if($corp->corporation_id != 98287666) {
$charId = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first();
$pages = $finance->GetJournalPageCount(1, $charId);
for($i = 1; $i <= $pages; $i++) {
$job = new JobProcessWalletJournal;
$job->division = 1;
$job->charId = $charId;
$job->page = $i;
ProcessWalletJournalJob::dispatch($job);
}
}
}
/*
foreach($corps as $corp) {
if($corp->corporation_id != 98287666) {
$charId = CorpStructure::where(['corporation_id' => $corp->corporation_id])->first();
$finance->GetWalletJournal(1, $charId->character_id);
}
}
*/
//Mark the job as finished
$task->SetStopStatus();
+4 -3
View File
@@ -48,16 +48,17 @@ class HoldingFinancesCommand extends Command
{
//Create the command helper container
$task = new CommandHelper('HoldingFinances');
//Add the entry into the jobs table saying the job is starting
$task->SetStartStatus();
//Setup the Finances container
$finance = new FinanceHelper();
//$finance->GetHoldingWalletJournal(1, 93738489);
//$finance->GetWalletJournal(1, 93738489);
//Get the total pages for the journal for the holding corporation
$pages = $finance->GetJournalPageCount(1, 93738489);
//Dispatch a single job for each page to process
for($i = 1; $i <= $pages; $i++) {
$job = new JobProcessWalletJournal;
$job->division = 1;
+9 -7
View File
@@ -3,19 +3,22 @@
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Jobs\SendEveMail;
use Carbon\Carbon;
use DB;
//Jobs
use App\Jobs\SendEveMailJob;
//Library
use Commands\Library\CommandHelper;
use App\Library\Moons\MoonMailer;
use App\Library\Moons\MoonCalc;
use DB;
//Models
use App\Models\Moon\Moon;
use App\Models\Moon\MoonRent;
use App\Models\Mail\EveMail;
use App\Models\Mail\SentMail;
use App\Models\Jobs\JobSendEveMail;
class MoonMailerCommand extends Command
{
@@ -99,16 +102,15 @@ class MoonMailerCommand extends Command
}
//Send a mail to the contact listing for the moon
$mail = new EveMail;
$mail = new JobSendEveMail;
$mail->sender = 93738489;
$mail->subject = "Moon Rental";
$mail->body = $body;
$mail->recipient = (int)$rental->Contact;
$mail->recipient_type = 'character';
//$mail->save();
//Dispatch the job and cycle to the next moon rental
SendEveMail::dispatch($mail);
SendEveMailJob::dispatch($mail);
//After the mail is dispatched, saved the sent mail record,
$sentmail = new SentMail;
+19 -1
View File
@@ -4,9 +4,16 @@ namespace App\Console\Commands;
use Illuminate\Console\Command;
//Library
use Commands\Library\CommandHelper;
use App\Library\Finances\Helper\FinanceHelper;
//Jobs
use App\Jobs\ProcessWalletJournalJob;
//Models
use App\Models\Jobs\JobProcessWalletJournal;
class PiTransactionsCommand extends Command
{
/**
@@ -42,13 +49,24 @@ class PiTransactionsCommand extends Command
{
//Create the command helper container
$task = new CommandHelper('PiTransactions');
//Add the entry into the jobs table saying the job is starting
$task->SetStartStatus();
//Setup the Finances container
$finance = new FinanceHelper();
$finance->GetWalletTransaction(3, 94415555);
//Get the total pages for the transactions
$pages = $finance->GetTransactionPageCount(3, 94415555);
//Dispatch a single job for each page to process
for($i = 1; $i <= $pages; $i++) {
$job = new JobProcessWalletTransaction;
$job->division = 3;
$job->charId = 94415555;
$job->page = $i;
ProcessWalletTransactionJob::dispatch($job);
}
//Mark the job as finished
$task->SetStopStatus();
+76
View File
@@ -0,0 +1,76 @@
<?php
namespace App\Jobs;
//Internal Libraries
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
//App Library
use App\Library\Finances\Helper\FinanceHelper;
//App Models
use App\Models\Jobs\JobProcessWalletTransaction;
class ProcessWallettransactionJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 300;
public $tries = 3;
private $division;
private $charId;
private $page;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(JobProcessWalletTransaction $pwt)
{
$this->division = $pwt->division;
$this->charId = $pwt->charId;
$this->page = $pwt->page;
$this->delay = 10;
$this->connection = 'database';
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//Declare the class variables
$finance = new FinanceHelper();
$finance->GetWalletTransactionPage($this->division, $this->charId, $this->page);
//After the job is completed, delete the job
$this->delete();
}
/**
* The job failed to process
*
* @param Exception $exception
* @return void
*/
public function failed($exception) {
// Send user notification of the failure, etc.
dd($exception);
}
}
+113
View File
@@ -0,0 +1,113 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Seat\Eseye\Configuration;
use Seat\Eseye\Containers\EsiAuthentication;
use Seat\Eseye\Eseye;
use Seat\Eseye\Exceptions\RequestFailedException;
use App\Models\Esi\EsiScope;
use App\Models\Esi\EsiToken;
use App\Models\Mail\EveMail;
class SendEveMailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Timeout in seconds
*
* @var int
*/
public $timeout = 120;
/**
* Retries
*
* @var int
*/
public $retries = 3;
private $body;
private $recipient;
private $recipient_type;
private $subject;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(EveMail $mail) {
$this->body = $mail->body;
$this->recipient = $mail->recipient;
$this->recipient_type = $mail->recipient_type;
$this->subject = $mail->subject;
$this->connection = 'database';
}
/**
* Execute the job.
* Utilized by using SendEveMail::dispatch($mail);
* The model is passed into the dispatch function, then added to the queue
* for processing.
*
* @return void
*/
public function handle()
{
//Retrieve the token for main character to send mails from
$token = EsiToken::where(['character_id'=> 93738489])->get();
//Create the ESI authentication container
$config = config('esi');
$authentication = new EsiAuthentication([
'client_id' => $config['client_id'],
'secret' => $config['secret'],
'refresh_token' => $token[0]->refresh_token,
]);
//Setup the Eseye class
$esi = new Eseye($authentication);
//Attemp to send the mail
try {
$esi->setBody([
'approved_cost' => 0,
'body' => $this->body,
'recipients' => [[
'recipient_id' => (int)$this->recipient,
'recipient_type' => $this->recipient_type,
]],
'subject' => $this->subject,
])->invoke('post', '/characters/{character_id}/mail/', [
'character_id'=> 93738489,
]);
} catch(RequestFailedException $e) {
return null;
}
$this->eveMail->delete();
}
/**
* The job failed to process.
*
* @param Exception $exception
* @return void
*/
public function failed($exception)
{
// Send user notification of failure, etc...
dd($exception);
}
}
+212 -90
View File
@@ -31,101 +31,127 @@ use Seat\Eseye\Exceptions\RequestFailedException;
class FinanceHelper {
public function GetTransactionPageCount($division, $charId) {
//Declare the class variables
$lookups = new LookupHelper;
//Get the ESI refresh token for the data
$tokenData = $this->TokenInfo($charId);
$token = $tokenData['token'];
$scope = $tokenData['scope'];
//If the token is not found, send the user an eve mail, and just exit out of the function
if($this->TokenNotFound($token, $scope, $charId)) {
printr("Token not found\n");
return null;
}
//Reference to see if the character is in our look up table for corporations and characters
$corpId = $lookups->LookupCharacter($charId);
//Create an ESI authentication container
$config = config('esi');
$authentication = new EsiAuthentication([
'client_id' => $config['client_id'],
'secret' => $config['secret'],
'refresh_token' => $token[0]->refresh_token,
]);
//Create the esi class varialble
$esi = new Eseye($authentication);
//Call the first page so we can get the header data for the number of pages
try {
$journals = $esi->invoke('get', '/corporations/{corporation_id}/wallets/{division}/transactions/', [
'corporation_id' => 98251577,
'division' => 3,
]);
} catch(RequestFailedException $e) {
return $e->getEsiResponse();
}
//Return the number of pages needed to be handled
return $journals->pages;
}
public function GetWalletTransaction($division, $charId) {
//Declare the lookup class helper
$lookups = new LookupHelper;
//Setup array for PI items
$pi_items = [
//R0 Materials
'2073',
'2667',
'2268',
'2270',
'2272',
'2286',
'2287',
'2288',
'2305',
'2306',
'2307',
'2308',
'2309',
'2310',
'2311',
//P1 Materials
'2389',
'2390',
'2392',
'2393',
'2395',
'2396',
'2397',
'2398',
'2399',
'2400',
'2401',
'3645',
'3683',
'3779',
'9828',
//P2 Materials
'44',
'2312',
'2317',
'2319',
'2321',
'2327',
'2328',
'2329',
'2463',
'3689',
'3691',
'3693',
'3695',
'3697',
'3725',
'3775',
'3828',
'9830',
'9832',
'9836',
'9838',
'9840',
'9842',
'15317',
//P3 Materials
'2344',
'2345',
'2346',
'2348',
'2349',
'2351',
'2352',
'2354',
'2358',
'2360',
'2361',
'2366',
'2367',
'9834',
'9846',
'9848',
'12836',
'17136',
'17392',
'17898',
'28974',
//P4 Materials
'2867',
'2868',
'2869',
'2870',
'2871',
'2872',
'2875',
'2876',
];
$pi_items = $this->GetPIMaterialsArray();
//Get the ESI refresh token for the corporation to add new wallet journals into the database
$tokenData = $this->TokenInfo($charId);
$token = $tokenData['token'];
$scope = $tokenData['scope'];
//If the token is not found, send the user an eve mail, and just exit out of the function
if($this->TokenNotFound($token, $scope, $charId)) {
printr("Token not found\n");
return null;
}
//Reference to see if the character is in our look up table for corporations and characters
$corpId = $lookups->LookupCharacter($charId);
//Create an ESI authentication container
$config = config('esi');
$authentication = new EsiAuthentication([
'client_id' => $config['client_id'],
'secret' => $config['secret'],
'refresh_token' => $token[0]->refresh_token,
]);
//Create the esi class varialble
$esi = new Eseye($authentication);
//Set our current page to 1 which is the one we are starting on.
$currentPage = 1;
//Set our default total pages to 1 in case our try section fails out.
$totalPages = 1;
//If more than one page is found, decode the first set of wallet entries, then call for the next pages
do {
//Call the first page of the wallet journal, as we are always going to get at least one page.
//If we have more pages, then we will continue through the while loop.
try {
$journals = $esi->page($currentPage)
->invoke('get', '/corporations/{corporation_id}/wallets/{division}/transactions/', [
'corporation_id' => 98251577,
'division' => 3,
]);
} catch(RequestFailedException $e) {
return $e->getEsiResponse();
}
//Set the total pages we need to cycle through.
$totalPages = $journals->pages;
//Decode the wallet from json into an array
$wallet = json_decode($journals->raw, true);
//For each journal entry, attempt to store it in the database.
//The PutWalletJournal function checks to see if it's already in the database.
foreach($wallet as $entry) {
if($division == 3 && $charId == 94415555) {
if(in_array($entry['type_id'], $pi_items, false)) {
$pi = new PISale();
$pi->InsertPISale($entry, 98287666);
}
}
}
//Increment the current page we are on.
$currentPage++;
//Continue looping through the do while loop until the current page is greater than or equal to the total pages.
} while ($currentPage < $totalPages);
}
public function GetWalletTransactionPage($division, $charId, $page = 1) {
//Declare the lookup class helper
$lookups = new LookupHelper;
//Setup array for PI items
$pi_items = $this->GetPIMaterialsArray();
//Get the ESI refresh token for the corporation to add new wallet journals into the database
$tokenData = $this->TokenInfo($charId);
@@ -423,6 +449,102 @@ class FinanceHelper {
return false;
}
private function GetPIMaterialsArray() {
//Setup array for PI items
$pi_items = [
//R0 Materials
'2073',
'2667',
'2268',
'2270',
'2272',
'2286',
'2287',
'2288',
'2305',
'2306',
'2307',
'2308',
'2309',
'2310',
'2311',
//P1 Materials
'2389',
'2390',
'2392',
'2393',
'2395',
'2396',
'2397',
'2398',
'2399',
'2400',
'2401',
'3645',
'3683',
'3779',
'9828',
//P2 Materials
'44',
'2312',
'2317',
'2319',
'2321',
'2327',
'2328',
'2329',
'2463',
'3689',
'3691',
'3693',
'3695',
'3697',
'3725',
'3775',
'3828',
'9830',
'9832',
'9836',
'9838',
'9840',
'9842',
'15317',
//P3 Materials
'2344',
'2345',
'2346',
'2348',
'2349',
'2351',
'2352',
'2354',
'2358',
'2360',
'2361',
'2366',
'2367',
'9834',
'9846',
'9848',
'12836',
'17136',
'17392',
'17898',
'28974',
//P4 Materials
'2867',
'2868',
'2869',
'2870',
'2871',
'2872',
'2875',
'2876',
];
return $pi_items;
}
}
?>
+2 -2
View File
@@ -10,7 +10,7 @@ class JobProcessWalletJournal extends Model
//public $table = 'job_process_wallet_journal';
//Timestamps
public $timestamps = true;
public $timestamps = false;
/**
* The attributes that are mass assignable
@@ -24,4 +24,4 @@ class JobProcessWalletJournal extends Model
];
}
?>
?>
@@ -0,0 +1,24 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class JobProcessWalletTransaction extends Model
{
//Timestamps
public $timestamps = false;
/**
* The attributes that are mass assignable
*
* @var array
*/
protected $fillable = [
'charId',
'division',
'page',
];
}
?>
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Models\Jobs;
use Illuminate\Database\Eloquent\Model;
class JobSendEveMail extends Model
{
//Table Name
//protected $table = 'eve_mails';
//Timestamps
public $timestamps = true;
protected $fillable = [
'sender',
'recipient',
'recipient_type',
'subject',
'body',
'created_at',
'updated_at',
];
}