Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 0 additions & 33 deletions app/Actions/Ookla/SelectSpeedtestServer.php

This file was deleted.

11 changes: 1 addition & 10 deletions app/Actions/Ookla/StartSpeedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ class StartSpeedtest
public function handle(bool $scheduled = false, ?int $serverId = null): void
{
$result = Result::create([
'data->server->id' => $serverId,
'service' => ResultService::Ookla,
'status' => ResultStatus::Started,
'scheduled' => $scheduled,
]);

if (blank($serverId)) {
$serverId = SelectSpeedtestServer::run();
}

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

ProcessSpeedtestBatch::dispatch(
result: $result,
);
Expand Down
1 change: 1 addition & 0 deletions app/Jobs/Ookla/ProcessSpeedtestBatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function handle(): void
[
new CheckForInternetConnectionJob($this->result),
new SkipSpeedtestJob($this->result),
new SelectSpeedtestServerJob($this->result),
new RunSpeedtestJob($this->result),
new BenchmarkSpeedtestJob($this->result),
new CompleteSpeedtestJob($this->result),
Expand Down
59 changes: 59 additions & 0 deletions app/Jobs/Ookla/SelectSpeedtestServerJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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\Support\Arr;

class SelectSpeedtestServerJob implements ShouldQueue
{
use Batchable, Queueable;

/**
* Create a new job instance.
*/
public function __construct(
public Result $result,
) {}

/**
* Execute the job.
*/
public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}

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

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

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