Skip to content

Commit

Permalink
improve schedule seeding (#395)
Browse files Browse the repository at this point in the history
* improve schedule seeding

* styleci

* bump dependency version
  • Loading branch information
recursivetree authored Mar 28, 2024
1 parent 30d2ef7 commit a1acfa7
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 159 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
302 changes: 144 additions & 158 deletions src/database/seeders/ScheduleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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':
Expand All @@ -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',
];
}
}

0 comments on commit a1acfa7

Please sign in to comment.