mirror of
https://github.com/alexjustesen/speedtest-tracker.git
synced 2026-06-23 07:10:10 +00:00
60 lines
1.3 KiB
PHP
60 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\Result;
|
|
use App\Models\Speedtest;
|
|
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 Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class ExecSpeedtest implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
public Speedtest|null $speedtest = null
|
|
) {}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$process = new Process(['speedtest', '--accept-license', '--format=json']);
|
|
$process->run();
|
|
|
|
if (! $process->isSuccessful()) {
|
|
throw new ProcessFailedException($process);
|
|
|
|
return 0;
|
|
}
|
|
|
|
$output = $process->getOutput();
|
|
|
|
if (! blank($this->speedtest)) {
|
|
$this->speedtest->results()->create([
|
|
'data' => $output,
|
|
]);
|
|
} else {
|
|
Result::create([
|
|
'data' => $output,
|
|
]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|