forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecSpeedtest.php
More file actions
81 lines (69 loc) · 2.19 KB
/
ExecSpeedtest.php
File metadata and controls
81 lines (69 loc) · 2.19 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
<?php
namespace App\Jobs;
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\Facades\Log;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class ExecSpeedtest implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(
public array|null $speedtest = null,
public bool $scheduled = false
) {
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$args = [
'speedtest',
'--accept-license',
'--accept-gdpr',
'--format=json',
];
if (! blank($this->speedtest)) {
if (! blank($this->speedtest['ookla_server_id'])) {
$args = array_merge($args, ['--server-id='.$this->speedtest['ookla_server_id']]);
}
}
$process = new Process($args);
$process->run();
if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
return 0;
}
try {
$output = $process->getOutput();
$results = json_decode($output, true);
Result::create([
'ping' => $results['ping']['latency'],
'download' => $results['download']['bandwidth'],
'upload' => $results['upload']['bandwidth'],
'server_id' => $results['server']['id'],
'server_name' => $results['server']['name'],
'server_host' => $results['server']['host'].':'.$results['server']['port'],
'url' => $results['result']['url'],
'scheduled' => $this->scheduled,
'data' => $output,
]);
} catch (\Exception $e) {
Log::error($e->getMessage());
}
return 0;
}
}