forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunSpeedtestJob.php
More file actions
96 lines (80 loc) · 2.41 KB
/
RunSpeedtestJob.php
File metadata and controls
96 lines (80 loc) · 2.41 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace App\Jobs\Ookla;
use App\Enums\ResultStatus;
use App\Events\SpeedtestFailed;
use App\Events\SpeedtestRunning;
use App\Helpers\Ookla;
use App\Models\Result;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\SkipIfBatchCancelled;
use Illuminate\Support\Arr;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class RunSpeedtestJob implements ShouldQueue
{
use Batchable, Queueable;
/**
* 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,
) {}
/**
* Get the middleware the job should pass through.
*
* @return array<int, object>
*/
public function middleware(): array
{
return [
new SkipIfBatchCancelled,
];
}
/**
* Execute the job.
*/
public function handle(): void
{
$this->result->update([
'status' => ResultStatus::Running,
]);
SpeedtestRunning::dispatch($this->result);
$command = array_filter([
'speedtest',
'--accept-license',
'--accept-gdpr',
'--format=json',
$this->result->server_id ? '--server-id='.$this->result->server_id : null,
config('speedtest.interface') ? '--interface='.config('speedtest.interface') : null,
]);
$process = new Process($command);
try {
$process->mustRun();
} catch (ProcessFailedException $exception) {
$this->result->update([
'data->type' => 'log',
'data->level' => 'error',
'data->message' => Ookla::getErrorMessage($exception),
'status' => ResultStatus::Failed,
]);
$this->batch()->cancel();
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,
]);
}
}