Skip to content

Commit

Permalink
fix: reduce market history jobs duration (#354)
Browse files Browse the repository at this point in the history
* make timeout configurable
* fix scheduling
* make the numbers a bit more pretty
  • Loading branch information
recursivetree authored May 28, 2023
1 parent 66876db commit 97e9b22
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/Commands/Esi/Update/Prices.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ class Prices extends Command
*/
protected $description = 'Schedule updater jobs which will collect market price stats.';

const HISTORY_BATCH_SIZE = 1000;

/**
* Execute the console command.
*/
public function handle()
{
PricesJob::dispatch();

// collect all items which can be sold on the market.
$types = InvType::whereNotNull('marketGroupID')
->where('published', true)
->select('typeID')
->inRandomOrder()
->limit(60 * 300)//60 minutes between default schedule invocation, 300 jobs per minute
->get();

PricesJob::dispatch();
->pluck('typeID');

// build small batch of a maximum of 200 entries to avoid long running job.
$types->chunk(50)->each(function ($chunk) {
$ids = $chunk->pluck('typeID')->toArray();
History::dispatch($ids)->delay(rand(20, 300));
//this is a guess that's only valid in the best case. In reality, we will probably be a bit slower.
$batch_processing_duration = (int) (History::ENDPOINT_RATE_LIMIT_WINDOW / History::ENDPOINT_RATE_LIMIT_CALLS * self::HISTORY_BATCH_SIZE);
$types->chunk(self::HISTORY_BATCH_SIZE)->each(function ($chunk, $index) use ($batch_processing_duration) {
$ids = $chunk->toArray();
History::dispatch($ids)->delay($index * $batch_processing_duration);
});
}
}
13 changes: 10 additions & 3 deletions src/Jobs/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ abstract class AbstractJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* The duration in seconds how long a job is allowed to execute.
*/
public const JOB_EXECUTION_TIMEOUT = 60 * 60; //1 hour

/**
* Execute the job.
*
Expand Down Expand Up @@ -78,10 +83,10 @@ public function tags(): array
public function failed(Exception $exception)
{
// Analytics. Report only the Exception class and message.
dispatch((new Analytics((new AnalyticsContainer)
dispatch(new Analytics((new AnalyticsContainer)
->set('type', 'exception')
->set('exd', get_class($exception) . ':' . $exception->getMessage())
->set('exf', 1))))->onQueue('default');
->set('exf', 1)))->onQueue('default');
}

/**
Expand All @@ -91,6 +96,8 @@ public function failed(Exception $exception)
*/
public function retryUntil()
{
return now()->addSeconds(3600);
// using self::JOB_EXECUTION_TIMEOUT makes it that you can't override the constant in class that inherit from AbstractJob.
// see https://stackoverflow.com/questions/13613594/overriding-class-constants-vs-properties
return now()->addSeconds(static::JOB_EXECUTION_TIMEOUT);
}
}
19 changes: 18 additions & 1 deletion src/Jobs/Market/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ class History extends EsiBase
{
const THE_FORGE = 10000002;

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

/**
* Describes how long the rate limit window lasts in seconds before resetting.
*
* @var int
*/
const ENDPOINT_RATE_LIMIT_WINDOW = 60; // to be on the safe side, we set it to 61 rather than 60

/**
* Describes how many calls can be made in the timespan described in ENDPOINT_RATE_LIMIT_WINDOW.
*
* @var int
*/
const ENDPOINT_RATE_LIMIT_CALLS = 280;

/**
* @var string
*/
Expand Down Expand Up @@ -83,7 +100,7 @@ public function handle()

while(count($this->type_ids) > 0) {
//don't go quite to the limit, maybe ccp_round() is involved somewhere along
Redis::throttle('market-history-throttle')->allow(300)->every(61)->then(function () use ($region_id) {
Redis::throttle('market-history-throttle')->allow(self::ENDPOINT_RATE_LIMIT_CALLS)->every(self::ENDPOINT_RATE_LIMIT_WINDOW)->then(function () use ($region_id) {
$type_id = array_shift($this->type_ids);

$this->query_string = [
Expand Down

0 comments on commit 97e9b22

Please sign in to comment.