Skip to content

Commit be306a9

Browse files
authored
[Chore] Refactor running an Ookla speedtest (alexjustesen#1007)
1 parent 89e39e2 commit be306a9

File tree

4 files changed

+96
-82
lines changed

4 files changed

+96
-82
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Jobs\ExecSpeedtest;
6+
use App\Settings\GeneralSettings;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Facades\Log;
10+
11+
class RunOoklaSpeedtest extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'app:run-ookla-speedtest
19+
{--scheduled : Option used to determine if the command was run from the scheduler}
20+
{server? : Specify an Ookla server to run the test against}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = "Run a speedtest against Ookla's service.";
28+
29+
/**
30+
* Execute the console command.
31+
*/
32+
public function handle()
33+
{
34+
$config = [];
35+
36+
$settings = new GeneralSettings();
37+
38+
if (is_array($settings->speedtest_server) && count($settings->speedtest_server)) {
39+
$config = array_merge($config, [
40+
'ookla_server_id' => Arr::random($settings->speedtest_server),
41+
]);
42+
}
43+
44+
if (! $this->option('scheduled')) {
45+
if ($this->argument('server')) {
46+
$config = array_merge($config, [
47+
'ookla_server_id' => $this->argument('server'),
48+
]);
49+
}
50+
51+
try {
52+
ExecSpeedtest::dispatch(
53+
speedtest: $config,
54+
scheduled: false
55+
);
56+
} catch (\Throwable $th) {
57+
Log::warning($th);
58+
59+
return Command::FAILURE;
60+
}
61+
62+
return Command::SUCCESS;
63+
}
64+
65+
try {
66+
ExecSpeedtest::dispatch(
67+
speedtest: $config,
68+
scheduled: true
69+
);
70+
} catch (\Throwable $th) {
71+
Log::warning($th);
72+
73+
return Command::FAILURE;
74+
}
75+
76+
return Command::SUCCESS;
77+
}
78+
}

app/Console/Commands/RunSpeedtest.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

app/Console/Kernel.php

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace App\Console;
44

5+
use App\Console\Commands\RunOoklaSpeedtest;
56
use App\Console\Commands\VersionChecker;
6-
use App\Jobs\ExecSpeedtest;
77
use App\Settings\GeneralSettings;
88
use Cron\CronExpression;
99
use Illuminate\Console\Scheduling\Schedule;
@@ -26,38 +26,17 @@ protected function schedule(Schedule $schedule): void
2626
->timezone($settings->timezone ?? 'UTC');
2727

2828
/**
29-
* Check for speedtests that need to run.
29+
* Check if an Ookla Speedtest needs to run.
3030
*/
31-
$schedule->call(function () use ($settings) {
32-
$ooklaServerId = null;
33-
34-
if (! blank($settings->speedtest_server)) {
35-
$item = array_rand($settings->speedtest_server);
36-
37-
$ooklaServerId = $settings->speedtest_server[$item];
38-
}
39-
40-
$speedtest = [
41-
'ookla_server_id' => $ooklaServerId,
42-
];
43-
44-
ExecSpeedtest::dispatch(
45-
speedtest: $speedtest,
46-
scheduled: true
47-
);
48-
})
49-
->everyMinute()
31+
$schedule->command(RunOoklaSpeedtest::class, ['--scheduled'])->everyMinute()
5032
->timezone($settings->timezone ?? 'UTC')
5133
->when(function () use ($settings) {
52-
// Don't run if the schedule is missing (aka disabled)
5334
if (blank($settings->speedtest_schedule)) {
5435
return false;
5536
}
5637

57-
// Evaluate if a run is needed based on the schedule
58-
$cron = new CronExpression($settings->speedtest_schedule);
59-
60-
return $cron->isDue(now()->timezone($settings->timezone ?? 'UTC'));
38+
return (new CronExpression($settings->speedtest_schedule))
39+
->isDue(now()->timezone($settings->timezone ?? 'UTC'));
6140
});
6241
}
6342

app/Filament/Pages/Dashboard.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
namespace App\Filament\Pages;
44

5+
use App\Console\Commands\RunOoklaSpeedtest;
56
use App\Filament\Widgets\RecentJitterChartWidget;
67
use App\Filament\Widgets\RecentPingChartWidget;
78
use App\Filament\Widgets\RecentSpeedChartWidget;
89
use App\Filament\Widgets\StatsOverviewWidget;
9-
use App\Jobs\ExecSpeedtest;
1010
use App\Settings\GeneralSettings;
1111
use Filament\Actions\Action;
1212
use Filament\Notifications\Notification;
1313
use Filament\Pages\Dashboard as BasePage;
14+
use Illuminate\Support\Facades\Artisan;
1415

1516
class Dashboard extends BasePage
1617
{
@@ -57,22 +58,20 @@ protected function getHeaderWidgets(): array
5758
];
5859
}
5960

60-
public function queueSpeedtest(GeneralSettings $settings)
61+
public function queueSpeedtest(): void
6162
{
62-
$ookla_server_id = null;
63-
64-
if (! blank($settings->speedtest_server)) {
65-
$item = array_rand($settings->speedtest_server);
66-
67-
$ookla_server_id = $settings->speedtest_server[$item];
63+
try {
64+
Artisan::call(RunOoklaSpeedtest::class);
65+
} catch (\Throwable $th) {
66+
Notification::make()
67+
->title('Manual speedtest failed!')
68+
->body('The starting a manual speedtest failed, check the logs.')
69+
->warning()
70+
->sendToDatabase(auth()->user());
71+
72+
return;
6873
}
6974

70-
$speedtest = [
71-
'ookla_server_id' => $ookla_server_id,
72-
];
73-
74-
ExecSpeedtest::dispatch(speedtest: $speedtest, scheduled: false);
75-
7675
Notification::make()
7776
->title('Speedtest added to the queue')
7877
->success()

0 commit comments

Comments
 (0)