Skip to content

Commit 7fc35a6

Browse files
authored
Refactored running speedtest process (alexjustesen#2206)
1 parent 53d0bca commit 7fc35a6

File tree

6 files changed

+59
-84
lines changed

6 files changed

+59
-84
lines changed

app/Actions/CheckForScheduledSpeedtests.php

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

33
namespace App\Actions;
44

5-
use App\Actions\Ookla\StartSpeedtest;
5+
use App\Actions\Ookla\RunSpeedtest;
66
use Cron\CronExpression;
77
use Lorisleiva\Actions\Concerns\AsAction;
88

@@ -18,7 +18,7 @@ public function handle(): void
1818
return;
1919
}
2020

21-
StartSpeedtest::runIf(
21+
RunSpeedtest::runIf(
2222
$this->isSpeedtestDue(schedule: $schedule),
2323
scheduled: true,
2424
);

app/Actions/Ookla/RunSpeedtest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Actions\Ookla;
4+
5+
use App\Enums\ResultService;
6+
use App\Enums\ResultStatus;
7+
use App\Events\SpeedtestWaiting;
8+
use App\Jobs\CheckForInternetConnectionJob;
9+
use App\Jobs\Ookla\BenchmarkSpeedtestJob;
10+
use App\Jobs\Ookla\CompleteSpeedtestJob;
11+
use App\Jobs\Ookla\RunSpeedtestJob;
12+
use App\Jobs\Ookla\SelectSpeedtestServerJob;
13+
use App\Jobs\Ookla\SkipSpeedtestJob;
14+
use App\Jobs\Ookla\StartSpeedtestJob;
15+
use App\Models\Result;
16+
use Illuminate\Bus\Batch;
17+
use Illuminate\Support\Facades\Bus;
18+
use Illuminate\Support\Facades\Log;
19+
use Lorisleiva\Actions\Concerns\AsAction;
20+
use Throwable;
21+
22+
class RunSpeedtest
23+
{
24+
use AsAction;
25+
26+
public function handle(bool $scheduled = false, ?int $serverId = null): mixed
27+
{
28+
$result = Result::create([
29+
'data->server->id' => $serverId,
30+
'service' => ResultService::Ookla,
31+
'status' => ResultStatus::Waiting,
32+
'scheduled' => $scheduled,
33+
]);
34+
35+
SpeedtestWaiting::dispatch($result);
36+
37+
Bus::batch([
38+
[
39+
new StartSpeedtestJob($result),
40+
new CheckForInternetConnectionJob($result),
41+
new SkipSpeedtestJob($result),
42+
new SelectSpeedtestServerJob($result),
43+
new RunSpeedtestJob($result),
44+
new BenchmarkSpeedtestJob($result),
45+
new CompleteSpeedtestJob($result),
46+
],
47+
])->catch(function (Batch $batch, ?Throwable $e) {
48+
Log::error(sprintf('Speedtest batch "%s" failed for an unknown reason.', $batch->id));
49+
})->name('Ookla Speedtest')->dispatch();
50+
51+
return $result;
52+
}
53+
}

app/Actions/Ookla/StartSpeedtest.php

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

app/Http/Controllers/Api/V1/RunSpeedtest.php

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

33
namespace App\Http\Controllers\Api\V1;
44

5-
use App\Actions\Ookla\StartSpeedtest;
5+
use App\Actions\Ookla\RunSpeedtest as RunSpeedtestAction;
66
use App\Http\Resources\V1\ResultResource;
77
use Illuminate\Http\Request;
88
use Illuminate\Http\Response;
@@ -51,7 +51,7 @@ public function __invoke(Request $request)
5151
);
5252
}
5353

54-
$result = StartSpeedtest::run(
54+
$result = RunSpeedtestAction::run(
5555
serverId: $validated['server_id'] ?? null,
5656
);
5757

app/Jobs/Ookla/ProcessSpeedtestBatch.php

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

app/Livewire/Topbar/RunSpeedtestAction.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\Livewire\Topbar;
44

55
use App\Actions\GetOoklaSpeedtestServers;
6-
use App\Actions\Ookla\StartSpeedtest;
6+
use App\Actions\Ookla\RunSpeedtest;
77
use App\Helpers\Ookla;
88
use Filament\Actions\Action;
99
use Filament\Actions\Concerns\InteractsWithActions;
@@ -51,8 +51,7 @@ public function speedtestAction(): Action
5151
->action(function (array $data) {
5252
$serverId = $data['server_id'] ?? null;
5353

54-
StartSpeedtest::run(
55-
scheduled: false,
54+
RunSpeedtest::run(
5655
serverId: $serverId,
5756
);
5857

0 commit comments

Comments
 (0)