Skip to content

Commit

Permalink
[Chore] Refactor running an Ookla speedtest (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen authored Dec 21, 2023
1 parent 89e39e2 commit be306a9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 82 deletions.
78 changes: 78 additions & 0 deletions app/Console/Commands/RunOoklaSpeedtest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Console\Commands;

use App\Jobs\ExecSpeedtest;
use App\Settings\GeneralSettings;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class RunOoklaSpeedtest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:run-ookla-speedtest
{--scheduled : Option used to determine if the command was run from the scheduler}
{server? : Specify an Ookla server to run the test against}';

/**
* The console command description.
*
* @var string
*/
protected $description = "Run a speedtest against Ookla's service.";

/**
* Execute the console command.
*/
public function handle()
{
$config = [];

$settings = new GeneralSettings();

if (is_array($settings->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;
}
}
42 changes: 0 additions & 42 deletions app/Console/Commands/RunSpeedtest.php

This file was deleted.

31 changes: 5 additions & 26 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
});
}

Expand Down
27 changes: 13 additions & 14 deletions app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit be306a9

Please sign in to comment.