Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
started work on updating the job for blocked servers
  • Loading branch information
alexjustesen committed Dec 6, 2024
commit 34c923dbb183ec00a49e8495db8e26298de453b7
38 changes: 0 additions & 38 deletions app/Actions/Ookla/ListServers.php

This file was deleted.

88 changes: 83 additions & 5 deletions app/Jobs/Ookla/SelectSpeedtestServerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class SelectSpeedtestServerJob implements ShouldQueue
{
Expand All @@ -28,14 +31,30 @@ public function handle(): void
return;
}

if (Arr::exists($this->result->data, 'server.id')) {
Log::info('Server exists for speedtest, skipping select server job.');

return;
}

$serverId = null;

if (! blank(config('speedtest.servers'))) {
$serverId = $this->getConfigServer();

$this->updateServerId($this->result, $serverId);

return;
}

$serverId = $this->filterServers()

$serverId = $this->result->server_id
?? $this->getConfigServer();

if ($this->result->server_id != $serverId) {
$this->result->update([
'data->server->id' => $serverId,
]);
}
$this->result->update([
'data->server->id' => $serverId,
]);
}

/**
Expand All @@ -56,4 +75,63 @@ private function getConfigServer(): ?string
? Arr::random($servers)
: null;
}

/**
* Filter servers from server list.
*/
private function filterServers()
{
$blocked = config('speedtest.blocked_servers');

$blocked = array_filter(
array_map(
'trim',
explode(',', $blocked),
),
);

$servers = $this->listServers();

$filtered = Arr::except($servers, $blocked);


}

/**
* 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 [];
}

return json_decode($process->getOutput(), true);
}

/**
* Update the result with the selected server Id.
*/
private function updateServerId(Result $result, int $serverId): void
{
$result->update([
'data->server->id' => $serverId,
]);
}
}
2 changes: 2 additions & 0 deletions config/speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

'servers' => env('SPEEDTEST_SERVERS'),

'blocked_servers' => env('SPEEDTEST_BLOCKED_SERVERS'),

/**
* IP filtering settings.
*/
Expand Down