| 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 |
|