From 32b693761bfbbf6a907c65db16d9c12293c0986a Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Thu, 21 Dec 2023 10:49:52 -0500 Subject: [PATCH 1/3] refactored running an ookla speedtest --- app/Console/Commands/RunOoklaSpeedtest.php | 82 ++++++++++++++++++++++ app/Console/Commands/RunSpeedtest.php | 42 ----------- app/Console/Kernel.php | 31 ++------ app/Filament/Pages/Dashboard.php | 28 ++++---- 4 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 app/Console/Commands/RunOoklaSpeedtest.php delete mode 100644 app/Console/Commands/RunSpeedtest.php diff --git a/app/Console/Commands/RunOoklaSpeedtest.php b/app/Console/Commands/RunOoklaSpeedtest.php new file mode 100644 index 000000000..e3066b7e4 --- /dev/null +++ b/app/Console/Commands/RunOoklaSpeedtest.php @@ -0,0 +1,82 @@ +speedtest_server) && count($settings->speedtest_server)) { + $config = array_merge($config, [ + 'ookla_server_id' => Arr::random($settings->speedtest_server), + ]); + } + + if (! $this->option('scheduled')) { + Log::info('Running unscheduled speedtest...'); + + if ($this->argument('server')) { + $config = array_merge($config, [ + 'ookla_server_id' => $this->argument('server'), + ]); + } + + try { + ExecSpeedtest::dispatch( + speedtest: $config, + scheduled: false + ); + } catch (\Throwable $th) { + Log::warning($th); + + return Command::FAILURE; + } + + return Command::SUCCESS; + } + + Log::info('Running scheduled speedtest...'); + + try { + ExecSpeedtest::dispatch( + speedtest: $config, + scheduled: true + ); + } catch (\Throwable $th) { + Log::warning($th); + + return Command::FAILURE; + } + + return Command::SUCCESS; + } +} diff --git a/app/Console/Commands/RunSpeedtest.php b/app/Console/Commands/RunSpeedtest.php deleted file mode 100644 index bc253583d..000000000 --- a/app/Console/Commands/RunSpeedtest.php +++ /dev/null @@ -1,42 +0,0 @@ -argument('server')) { - $speedtest = array_merge($speedtest, ['ookla_server_id' => $this->argument('server')]); - } - - ExecSpeedtest::dispatch(speedtest: $speedtest); - - $this->info('✅ added manual speedtest to the queue'); - - return 0; - } -} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 6896760f2..5fcdf0264 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -2,8 +2,8 @@ namespace App\Console; +use App\Console\Commands\RunOoklaSpeedtest; use App\Console\Commands\VersionChecker; -use App\Jobs\ExecSpeedtest; use App\Settings\GeneralSettings; use Cron\CronExpression; use Illuminate\Console\Scheduling\Schedule; @@ -26,38 +26,17 @@ protected function schedule(Schedule $schedule): void ->timezone($settings->timezone ?? 'UTC'); /** - * Check for speedtests that need to run. + * Check if an Ookla Speedtest needs to run. */ - $schedule->call(function () use ($settings) { - $ooklaServerId = null; - - if (! blank($settings->speedtest_server)) { - $item = array_rand($settings->speedtest_server); - - $ooklaServerId = $settings->speedtest_server[$item]; - } - - $speedtest = [ - 'ookla_server_id' => $ooklaServerId, - ]; - - ExecSpeedtest::dispatch( - speedtest: $speedtest, - scheduled: true - ); - }) - ->everyMinute() + $schedule->command(RunOoklaSpeedtest::class, ['--scheduled'])->everyMinute() ->timezone($settings->timezone ?? 'UTC') ->when(function () use ($settings) { - // Don't run if the schedule is missing (aka disabled) if (blank($settings->speedtest_schedule)) { return false; } - // Evaluate if a run is needed based on the schedule - $cron = new CronExpression($settings->speedtest_schedule); - - return $cron->isDue(now()->timezone($settings->timezone ?? 'UTC')); + return (new CronExpression($settings->speedtest_schedule)) + ->isDue(now()->timezone($settings->timezone ?? 'UTC')); }); } diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 5e8522dfa..34c40311e 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -2,6 +2,7 @@ namespace App\Filament\Pages; +use App\Console\Commands\RunOoklaSpeedtest; use App\Filament\Widgets\RecentJitterChartWidget; use App\Filament\Widgets\RecentPingChartWidget; use App\Filament\Widgets\RecentSpeedChartWidget; @@ -11,6 +12,7 @@ use Filament\Actions\Action; use Filament\Notifications\Notification; use Filament\Pages\Dashboard as BasePage; +use Illuminate\Support\Facades\Artisan; class Dashboard extends BasePage { @@ -57,25 +59,25 @@ protected function getHeaderWidgets(): array ]; } - public function queueSpeedtest(GeneralSettings $settings) + public function queueSpeedtest(): void { - $ookla_server_id = null; - - if (! blank($settings->speedtest_server)) { - $item = array_rand($settings->speedtest_server); - - $ookla_server_id = $settings->speedtest_server[$item]; + try { + Artisan::call(RunOoklaSpeedtest::class); + } catch (\Throwable $th) { + Notification::make() + ->title('Manual speedtest failed!') + ->body('The starting a manual speedtest failed, check the logs.') + ->warning() + ->sendToDatabase(auth()->user()); + + return; } - $speedtest = [ - 'ookla_server_id' => $ookla_server_id, - ]; - - ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: false); - Notification::make() ->title('Speedtest added to the queue') ->success() ->send(); + + return; } } From 2bfbb9c0ce92e93ad995fcb41f967e0f86fb1dab Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Thu, 21 Dec 2023 10:52:04 -0500 Subject: [PATCH 2/3] code quality --- app/Filament/Pages/Dashboard.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/Filament/Pages/Dashboard.php b/app/Filament/Pages/Dashboard.php index 34c40311e..590f2828d 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -7,7 +7,6 @@ use App\Filament\Widgets\RecentPingChartWidget; use App\Filament\Widgets\RecentSpeedChartWidget; use App\Filament\Widgets\StatsOverviewWidget; -use App\Jobs\ExecSpeedtest; use App\Settings\GeneralSettings; use Filament\Actions\Action; use Filament\Notifications\Notification; @@ -77,7 +76,5 @@ public function queueSpeedtest(): void ->title('Speedtest added to the queue') ->success() ->send(); - - return; } } From 3073920502ce0b7d434d456ff25593bed755986b Mon Sep 17 00:00:00 2001 From: Alex Justesen Date: Thu, 21 Dec 2023 12:23:14 -0500 Subject: [PATCH 3/3] removed unnecessary logging --- app/Console/Commands/RunOoklaSpeedtest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/Console/Commands/RunOoklaSpeedtest.php b/app/Console/Commands/RunOoklaSpeedtest.php index e3066b7e4..f14d4bdee 100644 --- a/app/Console/Commands/RunOoklaSpeedtest.php +++ b/app/Console/Commands/RunOoklaSpeedtest.php @@ -42,8 +42,6 @@ public function handle() } if (! $this->option('scheduled')) { - Log::info('Running unscheduled speedtest...'); - if ($this->argument('server')) { $config = array_merge($config, [ 'ookla_server_id' => $this->argument('server'), @@ -64,8 +62,6 @@ public function handle() return Command::SUCCESS; } - Log::info('Running scheduled speedtest...'); - try { ExecSpeedtest::dispatch( speedtest: $config,