mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-17 12:42:15 +01:00
Spread self-hosting update and telemetry requests over the day
This commit is contained in:
committed by
Constantin Graf
parent
df00200464
commit
0f21fabd37
@@ -22,13 +22,27 @@ class Kernel extends ConsoleKernel
|
|||||||
->when(fn (): bool => config('scheduling.tasks.auth_send_mails_expiring_api_tokens'))
|
->when(fn (): bool => config('scheduling.tasks.auth_send_mails_expiring_api_tokens'))
|
||||||
->everyTenMinutes();
|
->everyTenMinutes();
|
||||||
|
|
||||||
$schedule->command('self-host:check-for-update')
|
if (config('scheduling.tasks.self_hosting_check_for_update') || config('scheduling.tasks.self_hosting_telemetry')) {
|
||||||
->when(fn (): bool => config('scheduling.tasks.self_hosting_check_for_update'))
|
// Convert string to a stable integer for seeding
|
||||||
->twiceDaily();
|
/** @var int $seed Take the first 8 hex chars → 32-bit int */
|
||||||
|
$seed = hexdec(substr(hash('md5', config('app.key')), 0, 8));
|
||||||
|
$seed = abs($seed); // Ensure it's positive
|
||||||
|
mt_srand($seed);
|
||||||
|
$firstHour = mt_rand(0, 23);
|
||||||
|
$secondHour = ($firstHour + 12) % 24;
|
||||||
|
$minuteOffset = mt_rand(0, 59);
|
||||||
|
mt_srand(null); // Reset the random number generator
|
||||||
|
|
||||||
$schedule->command('self-host:telemetry')
|
if (config('scheduling.tasks.self_hosting_check_for_update')) {
|
||||||
->when(fn (): bool => config('scheduling.tasks.self_hosting_telemetry'))
|
$schedule->command('self-host:check-for-update')
|
||||||
->twiceDaily();
|
->twiceDailyAt($firstHour, $secondHour, $minuteOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config('scheduling.tasks.self_hosting_telemetry')) {
|
||||||
|
$schedule->command('self-host:telemetry')
|
||||||
|
->twiceDailyAt($firstHour, $secondHour, $minuteOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$schedule->command('self-host:database-consistency')
|
$schedule->command('self-host:database-consistency')
|
||||||
->when(fn (): bool => config('scheduling.tasks.self_hosting_database_consistency'))
|
->when(fn (): bool => config('scheduling.tasks.self_hosting_database_consistency'))
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ class HandleInertiaRequests extends Middleware
|
|||||||
$hasBilling = Module::has('Billing') && Module::isEnabled('Billing');
|
$hasBilling = Module::has('Billing') && Module::isEnabled('Billing');
|
||||||
$hasInvoicing = Module::has('Invoicing') && Module::isEnabled('Invoicing');
|
$hasInvoicing = Module::has('Invoicing') && Module::isEnabled('Invoicing');
|
||||||
$hasServices = Module::has('Services') && Module::isEnabled('Services');
|
$hasServices = Module::has('Services') && Module::isEnabled('Services');
|
||||||
|
|
||||||
/** @var BillingContract $billing */
|
/** @var BillingContract $billing */
|
||||||
$billing = app(BillingContract::class);
|
$billing = app(BillingContract::class);
|
||||||
|
|
||||||
|
|||||||
114
tests/Unit/Console/KernelTest.php
Normal file
114
tests/Unit/Console/KernelTest.php
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Unit\Console;
|
||||||
|
|
||||||
|
use App\Console\Kernel;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
#[CoversClass(Kernel::class)]
|
||||||
|
class KernelTest extends TestCase
|
||||||
|
{
|
||||||
|
public function test_self_host_commands_schedule_time_is_consistent_with_app_key(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
config([
|
||||||
|
'app.key' => 'base64:cOXN4GLMXYjcdG0fKosnFogofXw1pNoXkLAViRH+a5Y=',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$schedule1 = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$firstRunEvents = collect($schedule1->events())->filter(fn ($event) => str_contains($event->command, 'self-host:check-for-update') ||
|
||||||
|
str_contains($event->command, 'self-host:telemetry')
|
||||||
|
);
|
||||||
|
|
||||||
|
$schedule2 = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$secondRunEvents = collect($schedule2->events())->filter(fn ($event) => str_contains($event->command, 'self-host:check-for-update') ||
|
||||||
|
str_contains($event->command, 'self-host:telemetry')
|
||||||
|
);
|
||||||
|
config([
|
||||||
|
'app.key' => 'base64:eP58hkQ8l3guqf8wvWJR7pB0weVQtnpjMdYpaVwX4Jw=',
|
||||||
|
]);
|
||||||
|
$schedule3 = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$thirdRunEvents = collect($schedule3->events())->filter(fn ($event) => str_contains($event->command, 'self-host:check-for-update') ||
|
||||||
|
str_contains($event->command, 'self-host:telemetry')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(2, $firstRunEvents);
|
||||||
|
$this->assertCount(2, $secondRunEvents);
|
||||||
|
$this->assertCount(2, $thirdRunEvents);
|
||||||
|
|
||||||
|
foreach ($firstRunEvents as $index => $event) {
|
||||||
|
$this->assertSame('52 9,21 * * *', $firstRunEvents[$index]->expression);
|
||||||
|
$this->assertSame('52 9,21 * * *', $secondRunEvents[$index]->expression);
|
||||||
|
$this->assertSame('48 13,1 * * *', $thirdRunEvents[$index]->expression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_self_hosting_telemetry_can_be_activated(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
config([
|
||||||
|
'scheduling.tasks.self_hosting_telemetry' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$schedule = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'self-host:telemetry')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(1, $events);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_self_hosting_telemetry_can_be_deactivated(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
config([
|
||||||
|
'scheduling.tasks.self_hosting_telemetry' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$schedule = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'self-host:telemetry')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(0, $events);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_self_hosting_check_for_update_can_be_activated(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
config([
|
||||||
|
'scheduling.tasks.self_hosting_check_for_update' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$schedule = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'self-host:check-for-update')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(1, $events);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_self_hosting_check_for_update_can_be_deactivated(): void
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
config([
|
||||||
|
'scheduling.tasks.self_hosting_check_for_update' => false,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$schedule = app()->make(Kernel::class)->resolveConsoleSchedule();
|
||||||
|
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'self-host:check-for-update')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
$this->assertCount(0, $events);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user