Browse Source

add snaps update by timeout feature

main
yggverse 8 months ago
parent
commit
3a9efeabc5
  1. 3
      example/config.json
  2. 101
      src/cli/document/crawl.php

3
example/config.json

@ -256,7 +256,8 @@
}, },
"snap": "snap":
{ {
"enabled":true "enabled":true,
"timeout":2592000
} }
} }
} }

101
src/cli/document/crawl.php

@ -6,6 +6,37 @@ $microtime = microtime(true);
// Load dependencies // Load dependencies
require_once __DIR__ . '/../../../vendor/autoload.php'; require_once __DIR__ . '/../../../vendor/autoload.php';
// Define helpers
function getLastSnapTime(array $files): int
{
$time = [];
foreach ($files as $file)
{
if (in_array($file, ['.', '..']))
{
continue;
}
$time[] = preg_replace(
'/\D/',
'',
basename(
$file
)
);
}
if ($time)
{
return max(
$time
);
}
return 0;
}
// Init config // Init config
$config = json_decode( $config = json_decode(
file_get_contents( file_get_contents(
@ -702,6 +733,9 @@ foreach($index->search('')
@mkdir($filepath, 0755, true); @mkdir($filepath, 0755, true);
// Check latest snap older than defined in settings
if (time() - getLastSnapTime((array) scandir($filepath)) > $config->cli->document->crawl->snap->timeout)
{
$filename = sprintf( $filename = sprintf(
'%s/%s', '%s/%s',
$filepath, $filepath,
@ -711,12 +745,24 @@ foreach($index->search('')
) )
); );
if (!copy($tmp, $filename)) if (copy($tmp, $filename))
{
if ($config->cli->document->crawl->debug->level->notice)
{
echo sprintf(
_('[%s] [notice] save snap to "%s" on local storage') . PHP_EOL,
date('c'),
$filename
);
}
}
else
{ {
if ($config->cli->document->crawl->debug->level->error) if ($config->cli->document->crawl->debug->level->error)
{ {
echo sprintf( echo sprintf(
_('[%s] [error] could not copy "%" to "%" on local storage') . PHP_EOL, _('[%s] [error] could not copy "%s" to "%s" on local storage') . PHP_EOL,
date('c'), date('c'),
$tmp, $tmp,
$filename $filename
@ -724,6 +770,18 @@ foreach($index->search('')
} }
} }
} }
else
{
if ($config->cli->document->crawl->debug->level->notice)
{
echo sprintf(
_('[%s] [notice] local snap is up to date by timeout settings') . PHP_EOL,
date('c')
);
}
}
}
} }
// Copy to FTP storage on enabled // Copy to FTP storage on enabled
@ -824,16 +882,45 @@ foreach($index->search('')
true true
); );
if (!$remote->copy($tmp, $filename)) // Check latest snap older than defined in settings
if (time() - getLastSnapTime((array) $remote->nlist($filepath)) > $config->cli->document->crawl->snap->timeout)
{
if ($remote->copy($tmp, $filename))
{
if ($config->cli->document->crawl->debug->level->notice)
{
echo sprintf(
_('[%s] [notice] save snap to "%s" on remote host "%s"') . PHP_EOL,
date('c'),
$filename,
$ftp->connection->host
);
}
}
else
{ {
if ($config->cli->document->crawl->debug->level->error) if ($config->cli->document->crawl->debug->level->error)
{ {
echo sprintf( echo sprintf(
_('[%s] [error] could not copy "%" to "%" on destination "%s"') . PHP_EOL, _('[%s] [error] could not copy snap "%s" to "%s" on destination "%s"') . PHP_EOL,
date('c'), date('c'),
$tmp, $tmp,
$filename, $filename,
$ftp->connection->host, $ftp->connection->host
);
}
}
}
else
{
if ($config->cli->document->crawl->debug->level->notice)
{
echo sprintf(
_('[%s] [notice] remote snap on destination "%s" is up to date by timeout settings') . PHP_EOL,
date('c'),
$ftp->connection->host
); );
} }
} }
@ -887,7 +974,7 @@ foreach($index->search('')
if ($config->cli->document->crawl->debug->level->notice) if ($config->cli->document->crawl->debug->level->notice)
{ {
echo sprintf( echo sprintf(
_('[%s] [notice] remove tmp snap file %s') . PHP_EOL, _('[%s] [notice] remove tmp snap file "%s"') . PHP_EOL,
date('c'), date('c'),
$tmp $tmp
); );
@ -899,7 +986,7 @@ foreach($index->search('')
if ($config->cli->document->crawl->debug->level->error) if ($config->cli->document->crawl->debug->level->error)
{ {
echo sprintf( echo sprintf(
_('[%s] [error] could not remove tmp snap file %s') . PHP_EOL, _('[%s] [error] could not remove tmp snap file "%s"') . PHP_EOL,
date('c'), date('c'),
$tmp $tmp
); );

Loading…
Cancel
Save