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
60 lines (48 loc) · 1.79 KB
/
RunSpeedtestJob.php
File metadata and controls
60 lines (48 loc) · 1.79 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
<?php
namespace App\Jobs\Speedtest;
use App\Enums\ResultService;
use App\Enums\ResultStatus;
use App\Models\Result;
use App\Services\Speedtest\Drivers\CloudflareDriver;
use App\Services\Speedtest\Drivers\FastDriver;
use App\Services\Speedtest\Drivers\OoklaDriver;
use Illuminate\Bus\Batchable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class RunSpeedtestJob implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 600;
public function __construct(
public Result $result
) {}
public function handle(): void
{
$this->result->update(['status' => ResultStatus::Running]);
// Dispatch SpeedtestRunning event if needed.
// App\Events\SpeedtestRunning::dispatch($this->result);
try {
$driver = match ($this->result->service) {
ResultService::Ookla => new OoklaDriver(),
ResultService::Fast => new FastDriver(),
ResultService::Cloudflare => new CloudflareDriver(),
default => throw new \Exception("Unsupported service: {$this->result->service->value}"),
};
$driver->run($this->result);
} catch (\Throwable $e) {
Log::error($e);
$this->result->update([
'status' => ResultStatus::Failed,
'data->error' => $e->getMessage(),
]);
// App\Events\SpeedtestFailed::dispatch($this->result);
if ($this->batch()) {
$this->batch()->cancel();
}
}
}
}