PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.9.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.9.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Preload / Monitor / Monitor.php

Monitor.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.9.0, at src/Performance/Preload/Monitor/Monitor.php

109 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The preload monitor to check for a stalled preloader.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Preload\Monitor;
11
12 use SolidWP\Performance\Preload\Monitor\Exceptions\PreloadMonitorMaxRetriesException;
13
14 /**
15 * The preload monitor to check for a stalled preloader.
16 *
17 * @package SolidWP\Performance
18 */
19 class Monitor {
20
21 /**
22 * @var Repository
23 */
24 private Repository $repository;
25
26 /**
27 * How long in seconds the count can remain stale for.
28 *
29 * @var int
30 */
31 private int $timeout;
32
33 /**
34 * The maximum number of times we retry a stalled preloader before giving up.
35 *
36 * @var int
37 */
38 private int $max_retries;
39
40 /**
41 * @param Repository $repository The monitor repository.
42 * @param int $timeout How long in seconds the count can remain stale for.
43 * @param int $max_retries The maximum number of times we retry a stalled preloader before giving up.
44 */
45 public function __construct( Repository $repository, int $timeout = 12, int $max_retries = 10 ) {
46 $this->repository = $repository;
47 $this->timeout = $timeout;
48 $this->max_retries = $max_retries;
49 }
50
51 /**
52 * Check if the preloader is stalled out.
53 *
54 * @param int $url_count The number of URLs remaining.
55 *
56 * @throws PreloadMonitorMaxRetriesException If we exhausted all of our retries.
57 *
58 * @return bool
59 */
60 public function is_stalled( int $url_count ): bool {
61 $status = $this->repository->get();
62 $count = $status['count'] ?? 0;
63 $last_activity = $status['last_activity'] ?? 0;
64 $retries = $status['retries'] ?? 0;
65
66 // This is the first run.
67 if ( ! $count && ! $last_activity ) {
68 $this->repository->set( $url_count );
69
70 return false;
71 }
72
73 // Only check every x seconds...
74 if ( time() - $last_activity < $this->timeout ) {
75 return false;
76 }
77
78 // URL count has not changed, we're stalled.
79 $is_stalled = $count === $url_count;
80
81 // Check if we've reached the max retries.
82 if ( $is_stalled ) {
83 ++$retries;
84
85 if ( $retries >= $this->max_retries ) {
86 throw new PreloadMonitorMaxRetriesException(
87 sprintf(
88 'The Preload Monitor tried to restart the preloader %d times and gave up.',
89 $this->max_retries
90 )
91 );
92 }
93 }
94
95 $this->repository->set( $url_count, $retries );
96
97 return $is_stalled;
98 }
99
100 /**
101 * Clean up any monitor data.
102 *
103 * @return bool
104 */
105 public function clean(): bool {
106 return $this->repository->delete();
107 }
108 }
109