repository = $repository; $this->timeout = $timeout; $this->max_retries = $max_retries; } /** * Check if the preloader is stalled out. * * @param int $url_count The number of URLs remaining. * * @throws PreloadMonitorMaxRetriesException If we exhausted all of our retries. * * @return bool */ public function is_stalled( int $url_count ): bool { $status = $this->repository->get(); $count = $status['count'] ?? 0; $last_activity = $status['last_activity'] ?? 0; $retries = $status['retries'] ?? 0; // This is the first run. if ( ! $count && ! $last_activity ) { $this->repository->set( $url_count ); return false; } // Only check every x seconds... if ( time() - $last_activity < $this->timeout ) { return false; } // URL count has not changed, we're stalled. $is_stalled = $count === $url_count; // Check if we've reached the max retries. if ( $is_stalled ) { ++$retries; if ( $retries >= $this->max_retries ) { throw new PreloadMonitorMaxRetriesException( sprintf( 'The Preload Monitor tried to restart the preloader %d times and gave up.', $this->max_retries ) ); } } $this->repository->set( $url_count, $retries ); return $is_stalled; } /** * Clean up any monitor data. * * @return bool */ public function clean(): bool { return $this->repository->delete(); } }