mirror of
https://github.com/getnamingo/registry.git
synced 2025-07-13 22:35:08 +02:00
Improved system cache script
This commit is contained in:
parent
265a9eb1f7
commit
c7b4e3c631
2 changed files with 50 additions and 23 deletions
|
@ -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/registrar.php')->at('35 * * * *');
|
||||||
$scheduler->php('/opt/registry/automation/statistics.php')->at('59 * * * *');
|
$scheduler->php('/opt/registry/automation/statistics.php')->at('59 * * * *');
|
||||||
$scheduler->php('/opt/registry/automation/rdap-urls.php')->at('50 1 * * *');
|
$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/domain-lifecycle-manager.php')->at('*/5 * * * *');
|
||||||
$scheduler->php('/opt/registry/automation/auto-approve-transfer.php')->at('*/30 * * * *');
|
$scheduler->php('/opt/registry/automation/auto-approve-transfer.php')->at('*/30 * * * *');
|
||||||
|
|
|
@ -1,38 +1,65 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . '/../vendor/autoload.php';
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
require_once '/opt/registry/automation/helpers.php';
|
||||||
|
|
||||||
use League\Flysystem\Local\LocalFilesystemAdapter;
|
use League\Flysystem\Local\LocalFilesystemAdapter;
|
||||||
use League\Flysystem\Filesystem;
|
use League\Flysystem\Filesystem;
|
||||||
use MatthiasMullie\Scrapbook\Adapters\Flysystem as ScrapbookFlysystem;
|
use MatthiasMullie\Scrapbook\Adapters\Flysystem as ScrapbookFlysystem;
|
||||||
use MatthiasMullie\Scrapbook\Psr6\Pool;
|
use MatthiasMullie\Scrapbook\Psr6\Pool;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
|
||||||
|
$logFilePath = '/var/log/namingo/system-cache.log';
|
||||||
|
$log = setupLogger($logFilePath, 'SYSTEM_CACHE');
|
||||||
|
$log->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
|
// Set up Filesystem Cache
|
||||||
$cachePath = __DIR__ . '/../cache'; // Cache directory
|
$adapter = new LocalFilesystemAdapter($config['cache_path'], null, LOCK_EX);
|
||||||
$adapter = new LocalFilesystemAdapter($cachePath, null, LOCK_EX);
|
|
||||||
$filesystem = new Filesystem($adapter);
|
$filesystem = new Filesystem($adapter);
|
||||||
$cache = new Pool(new ScrapbookFlysystem($filesystem));
|
$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
|
// Check if the file is already cached
|
||||||
$cachedFile = $cache->getItem($cacheKey);
|
$cachedFile = $cache->getItem($config['cache_key']);
|
||||||
if (!$cachedFile->isHit()) {
|
if ($cachedFile->isHit()) {
|
||||||
// File is not cached, download it
|
$log->info('ICANN TLD List loaded from cache.');
|
||||||
$httpClient = new Client();
|
exit(0);
|
||||||
$response = $httpClient->get($fileUrl);
|
}
|
||||||
$fileContent = $response->getBody()->getContents();
|
|
||||||
|
|
||||||
// Save the file content to cache
|
// Download and cache the file
|
||||||
$cachedFile->set($fileContent);
|
$httpClient = new Client();
|
||||||
$cachedFile->expiresAfter(86400 * 7); // Cache for 7 days
|
$retryCount = 0;
|
||||||
$cache->save($cachedFile);
|
$success = false;
|
||||||
echo "ICANN TLD List downloaded and cached.".PHP_EOL;
|
|
||||||
} else {
|
while ($retryCount < $config['max_retries'] && !$success) {
|
||||||
// Retrieve the file content from the cache
|
try {
|
||||||
$fileContent = $cachedFile->get();
|
$response = $httpClient->get($config['file_url']);
|
||||||
echo "ICANN TLD List loaded from cache.".PHP_EOL;
|
$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.');
|
Loading…
Add table
Add a link
Reference in a new issue