File tree Expand file tree Collapse file tree 4 files changed +61
-43
lines changed
Expand file tree Collapse file tree 4 files changed +61
-43
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -16,21 +16,12 @@ class StartSpeedtest
1616 public function handle (bool $ scheduled = false , ?int $ serverId = null ): void
1717 {
1818 $ result = Result::create ([
19+ 'data->server->id ' => $ serverId ,
1920 'service ' => ResultService::Ookla,
2021 'status ' => ResultStatus::Started,
2122 'scheduled ' => $ scheduled ,
2223 ]);
2324
24- if (blank ($ serverId )) {
25- $ serverId = SelectSpeedtestServer::run ();
26- }
27-
28- if (! blank ($ serverId )) {
29- $ result ->update ([
30- 'data->server->id ' => $ serverId ,
31- ]);
32- }
33-
3425 ProcessSpeedtestBatch::dispatch (
3526 result: $ result ,
3627 );
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ public function handle(): void
3232 [
3333 new CheckForInternetConnectionJob ($ this ->result ),
3434 new SkipSpeedtestJob ($ this ->result ),
35+ new SelectSpeedtestServerJob ($ this ->result ),
3536 new RunSpeedtestJob ($ this ->result ),
3637 new BenchmarkSpeedtestJob ($ this ->result ),
3738 new CompleteSpeedtestJob ($ this ->result ),
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace App \Jobs \Ookla ;
4+
5+ use App \Models \Result ;
6+ use Illuminate \Bus \Batchable ;
7+ use Illuminate \Contracts \Queue \ShouldQueue ;
8+ use Illuminate \Foundation \Queue \Queueable ;
9+ use Illuminate \Support \Arr ;
10+
11+ class SelectSpeedtestServerJob implements ShouldQueue
12+ {
13+ use Batchable, Queueable;
14+
15+ /**
16+ * Create a new job instance.
17+ */
18+ public function __construct (
19+ public Result $ result ,
20+ ) {}
21+
22+ /**
23+ * Execute the job.
24+ */
25+ public function handle (): void
26+ {
27+ if ($ this ->batch ()->cancelled ()) {
28+ return ;
29+ }
30+
31+ $ serverId = $ this ->result ->server_id
32+ ?? $ this ->getConfigServer ();
33+
34+ if ($ this ->result ->server_id != $ serverId ) {
35+ $ this ->result ->update ([
36+ 'data->server->id ' => $ serverId ,
37+ ]);
38+ }
39+ }
40+
41+ /**
42+ * Get a server from the config servers list.
43+ */
44+ private function getConfigServer (): ?string
45+ {
46+ $ servers = config ('speedtest.servers ' );
47+
48+ $ servers = array_filter (
49+ array_map (
50+ 'trim ' ,
51+ explode (', ' , $ servers ),
52+ ),
53+ );
54+
55+ return count ($ servers ) > 0
56+ ? Arr::random ($ servers )
57+ : null ;
58+ }
59+ }
You can’t perform that action at this time.
0 commit comments