mirror of
https://github.com/alexjustesen/speedtest-tracker.git
synced 2026-06-23 04:20:08 +00:00
2c618e2d4e
* added public dashboard middleware * fixed lint issues
120 lines
3.2 KiB
PHP
120 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Speedtests;
|
|
|
|
use App\Enums\ResultStatus;
|
|
use App\Events\SpeedtestCompleted;
|
|
use App\Events\SpeedtestFailed;
|
|
use App\Models\Result;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Arr;
|
|
use JJG\Ping;
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class ExecuteOoklaSpeedtest implements ShouldBeUnique, ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
/**
|
|
* The number of seconds the job can run before timing out.
|
|
*
|
|
* @var int
|
|
*/
|
|
public $timeout = 120;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(
|
|
public Result $result,
|
|
public ?int $serverId,
|
|
) {}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
if (! $this->checkForInternetConnection()) {
|
|
return;
|
|
}
|
|
|
|
$options = array_filter([
|
|
'speedtest',
|
|
'--accept-license',
|
|
'--accept-gdpr',
|
|
'--format=json',
|
|
optional($this->serverId) ? '--server-id='.$this->serverId : false,
|
|
]);
|
|
|
|
$process = new Process($options);
|
|
|
|
try {
|
|
$process->mustRun();
|
|
} catch (ProcessFailedException $exception) {
|
|
$messages = explode(PHP_EOL, $exception->getMessage());
|
|
|
|
$message = collect(array_filter($messages, 'json_validate'))->last();
|
|
|
|
$this->result->update([
|
|
'server_id' => $this->serverId,
|
|
'data' => json_decode($message, true),
|
|
'status' => ResultStatus::Failed,
|
|
]);
|
|
|
|
SpeedtestFailed::dispatch($this->result);
|
|
|
|
return;
|
|
}
|
|
|
|
$output = json_decode($process->getOutput(), true);
|
|
|
|
$this->result->update([
|
|
'ping' => Arr::get($output, 'ping.latency'),
|
|
'download' => Arr::get($output, 'download.bandwidth'),
|
|
'upload' => Arr::get($output, 'upload.bandwidth'),
|
|
'data' => $output,
|
|
'status' => ResultStatus::Completed,
|
|
]);
|
|
|
|
SpeedtestCompleted::dispatch($this->result);
|
|
}
|
|
|
|
protected function checkForInternetConnection(): bool
|
|
{
|
|
// Skip checking for internet connection if ping url isn't set (disabled)
|
|
if (blank(config('speedtest.ping_url'))) {
|
|
return true;
|
|
}
|
|
|
|
$ping = new Ping(
|
|
host: config('speedtest.ping_url'),
|
|
timeout: 3,
|
|
);
|
|
|
|
if ($ping->ping() === false) {
|
|
$this->result->update([
|
|
'server_id' => $this->serverId,
|
|
'data' => [
|
|
'type' => 'log',
|
|
'level' => 'error',
|
|
'message' => 'Could not resolve host.',
|
|
],
|
|
'status' => ResultStatus::Failed,
|
|
]);
|
|
|
|
SpeedtestFailed::dispatch($this->result);
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|