Skip to content

Commit 83a73ed

Browse files
authored
[Feature] Added select speedtest server job (alexjustesen#1831)
* added select speedtest server job * removed unused code * return null if config is empty * dont need to return early
1 parent e661d47 commit 83a73ed

File tree

4 files changed

+61
-43
lines changed

4 files changed

+61
-43
lines changed

app/Actions/Ookla/SelectSpeedtestServer.php

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

app/Actions/Ookla/StartSpeedtest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,12 @@ class StartSpeedtest
1616
public function handle(bool $scheduled = false, ?int $serverId = null): void
1717
{
1818
$result = Result::create([
19+
'data->server->id' => $serverId,
1920
'service' => ResultService::Ookla,
2021
'status' => ResultStatus::Started,
2122
'scheduled' => $scheduled,
2223
]);
2324

24-
if (blank($serverId)) {
25-
$serverId = SelectSpeedtestServer::run();
26-
}
27-
28-
if (! blank($serverId)) {
29-
$result->update([
30-
'data->server->id' => $serverId,
31-
]);
32-
}
33-
3425
ProcessSpeedtestBatch::dispatch(
3526
result: $result,
3627
);

app/Jobs/Ookla/ProcessSpeedtestBatch.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function handle(): void
3232
[
3333
new CheckForInternetConnectionJob($this->result),
3434
new SkipSpeedtestJob($this->result),
35+
new SelectSpeedtestServerJob($this->result),
3536
new RunSpeedtestJob($this->result),
3637
new BenchmarkSpeedtestJob($this->result),
3738
new CompleteSpeedtestJob($this->result),
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace App\Jobs\Ookla;
4+
5+
use App\Models\Result;
6+
use Illuminate\Bus\Batchable;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Queue\Queueable;
9+
use Illuminate\Support\Arr;
10+
11+
class SelectSpeedtestServerJob implements ShouldQueue
12+
{
13+
use Batchable, Queueable;
14+
15+
/**
16+
* Create a new job instance.
17+
*/
18+
public function __construct(
19+
public Result $result,
20+
) {}
21+
22+
/**
23+
* Execute the job.
24+
*/
25+
public function handle(): void
26+
{
27+
if ($this->batch()->cancelled()) {
28+
return;
29+
}
30+
31+
$serverId = $this->result->server_id
32+
?? $this->getConfigServer();
33+
34+
if ($this->result->server_id != $serverId) {
35+
$this->result->update([
36+
'data->server->id' => $serverId,
37+
]);
38+
}
39+
}
40+
41+
/**
42+
* Get a server from the config servers list.
43+
*/
44+
private function getConfigServer(): ?string
45+
{
46+
$servers = config('speedtest.servers');
47+
48+
$servers = array_filter(
49+
array_map(
50+
'trim',
51+
explode(',', $servers),
52+
),
53+
);
54+
55+
return count($servers) > 0
56+
? Arr::random($servers)
57+
: null;
58+
}
59+
}

0 commit comments

Comments
 (0)