diff --git a/automation/cron.php b/automation/cron.php index cb891be..16ef7eb 100644 --- a/automation/cron.php +++ b/automation/cron.php @@ -51,7 +51,7 @@ $scheduler->php('/opt/registry/automation/write-zone.php')->at('*/15 * * * *'); $scheduler->php('/opt/registry/automation/registrar.php')->at('35 * * * *'); $scheduler->php('/opt/registry/automation/statistics.php')->at('59 * * * *'); $scheduler->php('/opt/registry/automation/rdap-urls.php')->at('50 1 * * *'); -$scheduler->php('/var/www/cp/bin/file_cache.php')->at('0 0 * * 2,6'); +$scheduler->php('/var/www/cp/bin/file_cache.php')->at('0 0 * * 1,4'); $scheduler->php('/opt/registry/automation/domain-lifecycle-manager.php')->at('*/5 * * * *'); $scheduler->php('/opt/registry/automation/auto-approve-transfer.php')->at('*/30 * * * *'); diff --git a/cp/bin/file_cache.php b/cp/bin/file_cache.php index b8608bb..3e98b1a 100644 --- a/cp/bin/file_cache.php +++ b/cp/bin/file_cache.php @@ -1,38 +1,65 @@ info('job started.'); + +// Configuration +$config = [ + 'cache_path' => __DIR__ . '/../cache', // Cache directory + 'cache_key' => 'tlds_alpha_by_domain', + 'file_url' => 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt', + 'cache_duration' => 86400 * 7, // Cache for 7 days + 'max_retries' => 3, // Retry up to 3 times +]; // Set up Filesystem Cache -$cachePath = __DIR__ . '/../cache'; // Cache directory -$adapter = new LocalFilesystemAdapter($cachePath, null, LOCK_EX); +$adapter = new LocalFilesystemAdapter($config['cache_path'], null, LOCK_EX); $filesystem = new Filesystem($adapter); $cache = new Pool(new ScrapbookFlysystem($filesystem)); -// Define the cache key and the URL of the file you want to cache -$cacheKey = 'tlds_alpha_by_domain'; -$fileUrl = 'https://data.iana.org/TLD/tlds-alpha-by-domain.txt'; - // Check if the file is already cached -$cachedFile = $cache->getItem($cacheKey); -if (!$cachedFile->isHit()) { - // File is not cached, download it - $httpClient = new Client(); - $response = $httpClient->get($fileUrl); - $fileContent = $response->getBody()->getContents(); +$cachedFile = $cache->getItem($config['cache_key']); +if ($cachedFile->isHit()) { + $log->info('ICANN TLD List loaded from cache.'); + exit(0); +} - // Save the file content to cache - $cachedFile->set($fileContent); - $cachedFile->expiresAfter(86400 * 7); // Cache for 7 days - $cache->save($cachedFile); - echo "ICANN TLD List downloaded and cached.".PHP_EOL; -} else { - // Retrieve the file content from the cache - $fileContent = $cachedFile->get(); - echo "ICANN TLD List loaded from cache.".PHP_EOL; -} \ No newline at end of file +// Download and cache the file +$httpClient = new Client(); +$retryCount = 0; +$success = false; + +while ($retryCount < $config['max_retries'] && !$success) { + try { + $response = $httpClient->get($config['file_url']); + $fileContent = $response->getBody()->getContents(); + + // Save the file content to cache + $cachedFile->set($fileContent); + $cachedFile->expiresAfter($config['cache_duration']); + $cache->save($cachedFile); + + $log->info('ICANN TLD list downloaded and cached successfully.'); + $success = true; + } catch (RequestException $e) { + $retryCount++; + $log->error("Error downloading file (attempt $retryCount): " . $e->getMessage()); + if ($retryCount >= $config['max_retries']) { + $log->error('Max retries reached. File download failed.'); + exit(1); + } + } +} + +$log->info('job finished successfully.'); \ No newline at end of file