From a1acfa749223a2ad3699678ab767004ef85a5885 Mon Sep 17 00:00:00 2001 From: recursivetree <60423027+recursivetree@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:43:59 +0100 Subject: [PATCH] improve schedule seeding (#395) * improve schedule seeding * styleci * bump dependency version --- composer.json | 2 +- src/database/seeders/ScheduleSeeder.php | 302 +++++++++++------------- 2 files changed, 145 insertions(+), 159 deletions(-) diff --git a/composer.json b/composer.json index 56b3dc09..05381ddf 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "laravel/framework": "^10.0", "maatwebsite/excel": "^3.1.45", "eveseat/eseye": "^3.0", - "eveseat/services": "^5.0.1", + "eveseat/services": "^5.0.6", "guzzlehttp/guzzle": "^7.0", "doctrine/dbal": "^3.0", "psr/simple-cache": "^3.0", diff --git a/src/database/seeders/ScheduleSeeder.php b/src/database/seeders/ScheduleSeeder.php index ec2084e5..592951bf 100644 --- a/src/database/seeders/ScheduleSeeder.php +++ b/src/database/seeders/ScheduleSeeder.php @@ -22,173 +22,140 @@ namespace Seat\Eveapi\Database\Seeders; -use Illuminate\Database\Seeder; use Illuminate\Support\Arr; -use Illuminate\Support\Facades\DB; +use Seat\Services\Seeding\AbstractScheduleSeeder; /** * Class ScheduleSeeder. * * @package Seat\Eveapi\database\seeds */ -class ScheduleSeeder extends Seeder +class ScheduleSeeder extends AbstractScheduleSeeder { /** - * @var array - */ - protected $schedules = [ - [ // ESI Status | Every Minute - 'command' => 'esi:update:status', - 'expression' => '* * * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // EVE Server Status | Every Minute - 'command' => 'eve:update:status', - 'expression' => '* * * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // SDE Data | Monthly - 'command' => 'eve:update:sde', - 'expression' => '0 0 1 * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Update a set of tokens | Every 2 Minutes - 'command' => 'seat:buckets:update', - 'expression' => '*/2 * * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Public Data | Daily at 12am - 'command' => 'esi:update:public', - 'expression' => '0 0 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Character Affiliation | Every two hours - 'command' => 'esi:update:affiliations', - 'expression' => '0 */2 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Character Notifications | Every twenty minutes - 'command' => 'esi:update:notifications', - 'expression' => '*/20 * * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Killmails | Every fifteen minutes - 'command' => 'esi:update:killmails', - 'expression' => '*/15 * * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Contracts | Every fifteen minutes - 'command' => 'esi:update:contracts', - 'expression' => '*/15 * * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Prices | Once a day - 'command' => 'esi:update:prices', - 'expression' => '0 13 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Alliances | Once a day - 'command' => 'esi:update:alliances', - 'expression' => '0 14 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Insurances Data | Once a day - 'command' => 'esi:update:insurances', - 'expression' => '0 7 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Sovereignty Data | Once a day - 'command' => 'esi:update:sovereignty', - 'expression' => '0 19 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Stations Data | Once a day - 'command' => 'esi:update:stations', - 'expression' => '0 1 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - [ // Horizon remove batches older than 48 hours | Once a day - 'command' => 'queue:prune-batches --hours=48', - 'expression' => '0 3 * * *', - 'allow_overlap' => false, - 'allow_maintenance' => false, - 'ping_before' => null, - 'ping_after' => null, - ], - ]; - - /** - * Run the database seeds. + * Returns an array of schedules that should be seeded whenever the stack boots up. * - * @return void + * @return array */ - public function run() + public function getSchedules(): array { - // add randomness to default schedules - $this->seedRandomize(); - - // drop deprecated commands - DB::table('schedules')->whereIn('command', [ - 'alerts:run', - 'esi:update:serverstatus', - 'esi:update:esistatus', - 'esi:update:characters', - 'esi:update:corporations', - ])->delete(); + $schedules = [ + [ // ESI Status | Every Minute + 'command' => 'esi:update:status', + 'expression' => '* * * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // SDE Data | Monthly + 'command' => 'eve:update:sde', + 'expression' => '0 0 1 * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Update a set of tokens | Every 2 Minutes + 'command' => 'seat:buckets:update', + 'expression' => '*/2 * * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Public Data | Daily at 12am + 'command' => 'esi:update:public', + 'expression' => '0 0 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Character Affiliation | Every two hours + 'command' => 'esi:update:affiliations', + 'expression' => '0 */2 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Character Notifications | Every twenty minutes + 'command' => 'esi:update:notifications', + 'expression' => '*/20 * * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Killmails | Every fifteen minutes + 'command' => 'esi:update:killmails', + 'expression' => '*/15 * * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Contracts | Every fifteen minutes + 'command' => 'esi:update:contracts', + 'expression' => '*/15 * * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Prices | Once a day + 'command' => 'esi:update:prices', + 'expression' => '0 13 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Alliances | Once a day + 'command' => 'esi:update:alliances', + 'expression' => '0 14 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Insurances Data | Once a day + 'command' => 'esi:update:insurances', + 'expression' => '0 7 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Sovereignty Data | Once a day + 'command' => 'esi:update:sovereignty', + 'expression' => '0 19 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Stations Data | Once a day + 'command' => 'esi:update:stations', + 'expression' => '0 1 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + [ // Horizon remove batches older than 48 hours | Once a day + 'command' => 'queue:prune-batches --hours=48', + 'expression' => '0 3 * * *', + 'allow_overlap' => false, + 'allow_maintenance' => false, + 'ping_before' => null, + 'ping_after' => null, + ], + ]; - // Check if we have the schedules, else, - // insert them - foreach ($this->schedules as $job) { - if (DB::table('schedules')->where('command', $job['command'])->exists()) { - DB::table('schedules')->where('command', $job['command'])->update([ - 'expression' => $job['expression'], - ]); - } else { - DB::table('schedules')->insert($job); - } - } + // spread out ESI load + return $this->seedRandomize($schedules); } /** @@ -197,16 +164,16 @@ public function run() * * @see https://github.com/eveseat/seat/issues/731 */ - private function seedRandomize() + private function seedRandomize($schedules): array { // except utc 11 and utc 12 $hours = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]; - foreach ($this->schedules as $key => $schedule) { + foreach ($schedules as $key => $schedule) { switch ($schedule['command']) { // use random minute - every 2 hours case 'esi:update:affiliations': - $this->schedules[$key]['expression'] = sprintf('%d */2 * * *', rand(0, 59)); + $schedules[$key]['expression'] = sprintf('%d */2 * * *', rand(0, 59)); break; // use random minute and hour, once a day case 'esi:update:public': @@ -215,9 +182,28 @@ private function seedRandomize() case 'esi:update:insurances': case 'esi:update:sovereignty': case 'esi:update:stations': - $this->schedules[$key]['expression'] = sprintf('%d %d * * *', rand(0, 59), Arr::random($hours)); + $schedules[$key]['expression'] = sprintf('%d %d * * *', rand(0, 59), Arr::random($hours)); break; } } + + return $schedules; + } + + /** + * Returns a list of commands to remove from the schedule. + * + * @return array + */ + public function getDeprecatedSchedules(): array + { + // these commands are no longer in use, remove them + return [ + 'alerts:run', + 'esi:update:serverstatus', + 'esi:update:esistatus', + 'esi:update:characters', + 'esi:update:corporations', + ]; } }