Skip to content

Commit

Permalink
Market History: Handle if type is not available on the market (#394)
Browse files Browse the repository at this point in the history
* Market History: Handle if type is not available on the market

* fix: Ensured Market History jobs don't run in parallell
  • Loading branch information
moppa authored Mar 28, 2024
1 parent a1acfa7 commit 8c5b0d7
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions src/Jobs/Market/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Seat\Eveapi\Jobs\Market;

use Illuminate\Bus\Batchable;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Support\Facades\Redis;
use Seat\Eseye\Exceptions\RequestFailedException;
use Seat\Eveapi\Exception\TemporaryEsiOutageException;
Expand All @@ -40,6 +41,11 @@ class History extends EsiBase

const THE_FORGE = 10000002;

/**
* HISTORY_EXPIRY_DELAY forces lock release after 2 minutes.
*/
const HISTORY_EXPIRY_DELAY = 60 * 2;

// override the default from AbstractJob
public const JOB_EXECUTION_TIMEOUT = 60 * 60 * 24; // 1 day

Expand Down Expand Up @@ -132,6 +138,19 @@ public function setCurrentBatchCount($current)
return $this;
}

/**
* @return array
*/
public function middleware()
{
// Ensure market history jobs don't run in parallell
return array_merge(parent::middleware(), [
(new WithoutOverlapping('market-history-job'))
->releaseAfter(self::ANTI_RACE_DELAY)
->expireAfter(self::HISTORY_EXPIRY_DELAY),
]);
}

/**
* {@inheritdoc}
*
Expand Down Expand Up @@ -159,17 +178,28 @@ public function handle()
];

try {
// for each subsequent item, request ESI order stats using region in settings (The Forge is default).
$response = $this->retrieve([
'region_id' => $region_id,
]);

$prices = $response->getBody();

// search the more recent entry in returned history.
$price = collect($prices)->where('order_count', '>', 0)
->sortByDesc('date')
->first();
try {
// for each subsequent item, request ESI order stats using region in settings (The Forge is default).
$response = $this->retrieve([
'region_id' => $region_id,
]);

$prices = $response->getBody();

// search the more recent entry in returned history.
$price = collect($prices)->where('order_count', '>', 0)
->sortByDesc('date')
->first();
} catch (RequestFailedException $e) {
// If the item was not found on market, default to an empty price
if($e->getEsiResponse()->getErrorCode() == 404 || $e->getEsiResponse()->getErrorCode() == 400) {
logger()->error(sprintf('[Jobs][%s] History -> type_id %d not found on market: %s', $this->job->getJobId(), $type_id, $e->getMessage()));
$price = null;
} else {
// Rethrow exception for any other status code
throw $e;
}
}

if (is_null($price)) {
$price = (object) [
Expand Down Expand Up @@ -201,7 +231,7 @@ public function handle()

return true;
} catch (RequestFailedException $e) {
logger()->error($e->getMessage());
logger()->error(sprintf('[Jobs][%s] History -> ESI Error for type id %d: %s', $this->job->getJobId(), $type_id, $e->getMessage()));

return true;
}
Expand Down

0 comments on commit 8c5b0d7

Please sign in to comment.