eliashaeussler / cache-warmup

🔥 PHP library to warm up caches of URLs located in XML sitemaps

Home Page:https://cache-warmup.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE] Display failures URL

jcvignoli opened this issue · comments

Is your feature request related to a problem?

Can't get cache-warmup to show the failed URLs

Describe the solution you'd like

I'd like to see what URL failed to warm

Describe alternatives you've considered

I'm not using the command-line but rather the PHP version. I've created a class like this:


namespace Services\Cache;

use \EliasHaeussler\CacheWarmup\CacheWarmer;

class WarmCache {

	/**
	 * Start the Warmer
	 *
	 * @param string|array<int, string> $xml_url Url(s) of XML files to be processed
	 * @param null|int $limit Number of cache pages to warm
	 * @return string Message succeeded and failed URLs
	 */
	public function start( array|string $xml_url, int $limit ): string {

		$cacheWarmer = new CacheWarmer( $limit );

		if ( is_string( $xml_url ) === true ) {
			$cacheWarmer->addSitemaps( $xml_url );
		}

		if ( is_array( $xml_url ) === true ) {
			foreach ( $xml_url as $string ) {
				$cacheWarmer->addSitemaps( $string );
			}
		}

		$result = $cacheWarmer->run();

		$successfulUrls = $result->getSuccessful(); 
		$failedUrls = $result->getFailed(); 

		return 'Success:' . json_encode( $successfulUrls ) . "\n<br />Failed:" . json_encode( $failedUrls ) . "\n";
	}

}

$xml_url is the injected sitemaps.

It is called through a home-made cron. Unfortunately, both the successful and failed URLs are returned empty ( with a {} for each link).

Would be great to see which links failed.

Additional context

No response

Hi @jcvignoli, thanks for your issue. The problem you described is because of the usage of json_encode on the list of CrawlingResult objects. Since this class does not implement the JsonSerializable interface, running json_encode on it results in empty objects.

Try mapping the objects to their string representation prior to JSON-encoding it:

-return 'Success:' . json_encode( $successfulUrls ) . "\n<br />Failed:" . json_encode( $failedUrls ) . "\n";
+return 'Success:' . json_encode( array_map('strval', $successfulUrls) ) . "\n<br />Failed:" . json_encode( array_map('strval', $failedUrls) ) . "\n";

it did the trick. Thanks so much!