diff --git a/app/Console/Commands/RunOoklaSpeedtest.php b/app/Console/Commands/RunOoklaSpeedtest.php new file mode 100644 index 000000000..f14d4bdee --- /dev/null +++ b/app/Console/Commands/RunOoklaSpeedtest.php @@ -0,0 +1,78 @@ +speedtest_server) && count($settings->speedtest_server)) { + $config = array_merge($config, [ + 'ookla_server_id' => Arr::random($settings->speedtest_server), + ]); + } + + if (! $this->option('scheduled')) { + 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; + } + + 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..590f2828d 100644 --- a/app/Filament/Pages/Dashboard.php +++ b/app/Filament/Pages/Dashboard.php @@ -2,15 +2,16 @@ namespace App\Filament\Pages; +use App\Console\Commands\RunOoklaSpeedtest; use App\Filament\Widgets\RecentJitterChartWidget; 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; use Filament\Pages\Dashboard as BasePage; +use Illuminate\Support\Facades\Artisan; class Dashboard extends BasePage { @@ -57,22 +58,20 @@ 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()