forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectSpeedtestServerJob.php
More file actions
176 lines (145 loc) · 4.22 KB
/
SelectSpeedtestServerJob.php
File metadata and controls
176 lines (145 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace App\Jobs\Ookla;
use App\Models\Result;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\SkipIfBatchCancelled;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class SelectSpeedtestServerJob implements ShouldQueue
{
use Batchable, Queueable;
/**
* Create a new job instance.
*/
public function __construct(
public Result $result,
) {}
/**
* Get the middleware the job should pass through.
*/
public function middleware(): array
{
return [
new SkipIfBatchCancelled,
];
}
/**
* Execute the job.
*/
public function handle(): void
{
// If the server id is already set, we don't need to do anything.
if (Arr::get($this->result->data, 'server.id')) {
return;
}
// If preferred servers are set in the config, we can use that,
// but only if the test is scheduled.
if ($this->result->scheduled && ! blank(config('speedtest.servers'))) {
$this->updateServerId(
result: $this->result,
serverId: $this->getConfigServer(),
);
return;
}
// If blocked servers config is blank, we can skip picking a server.
if (blank(config('speedtest.blocked_servers'))) {
return;
}
$serverId = $this->filterBlockedServers();
if (blank($serverId)) {
Log::info('Failed to select a server for Ookla speedtest, skipping blocked server filter.', [
'result_id' => $this->result->id,
]);
return;
}
$this->updateServerId($this->result, $serverId);
}
/**
* Get a list of servers from config blocked servers.
*/
private function getConfigBlockedServers(): array
{
$blocked = config('speedtest.blocked_servers');
$blocked = array_filter(
array_map(
'trim',
explode(',', $blocked),
),
);
if (blank($blocked)) {
return [];
}
return collect($blocked)->mapWithKeys(function (int $serverId) {
return [$serverId => $serverId];
})->toArray();
}
/**
* Get a server from the config servers list.
*/
private function getConfigServer(): ?string
{
$servers = config('speedtest.servers');
$servers = array_filter(
array_map(
'trim',
explode(',', $servers),
),
);
return count($servers) > 0
? Arr::random($servers)
: null;
}
/**
* Filter servers from server list.
*/
private function filterBlockedServers(): mixed
{
$blocked = $this->getConfigBlockedServers();
$servers = $this->listServers();
$filtered = Arr::except($servers, $blocked);
return Arr::first($filtered);
}
/**
* Get a list of servers.
*/
private function listServers(): array
{
$command = [
'speedtest',
'--accept-license',
'--accept-gdpr',
'--servers',
'--format=json',
];
$process = new Process($command);
try {
$process->run();
} catch (ProcessFailedException $e) {
Log::error('Failed listing Ookla speedtest servers.', [
'error' => $e->getMessage(),
]);
return [];
}
$servers = Arr::get(
array: json_decode($process->getOutput(), true),
key: 'servers',
default: [],
);
return collect($servers)->mapWithKeys(function (array $server) {
return [$server['id'] => $server['id']];
})->toArray();
}
/**
* Update the result with the selected server Id.
*/
private function updateServerId(Result $result, int $serverId): void
{
$result->update([
'data->server->id' => $serverId,
]);
}
}