X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=maintenance%2FsyncFileBackend.php;h=f0be709f9b4f0e2cab714b4a91b8f08455ab5ecb;hb=0182f3fb638750850b7a34018730e77f2fffd048;hp=8f6e41929da3882b5a0e58dfd3fdc84b94e9ab62;hpb=b4ceb49513aff36e08894f7fc8b7b336b8dac636;p=lhc%2Fweb%2Fwiklou.git diff --git a/maintenance/syncFileBackend.php b/maintenance/syncFileBackend.php index 8f6e41929d..f0be709f9b 100644 --- a/maintenance/syncFileBackend.php +++ b/maintenance/syncFileBackend.php @@ -21,7 +21,7 @@ * @ingroup Maintenance */ -require_once( __DIR__ . '/Maintenance.php' ); +require_once __DIR__ . '/Maintenance.php'; /** * Maintenance script that syncs one file backend to another based on @@ -40,6 +40,7 @@ class SyncFileBackend extends Maintenance { $this->addOption( 'posdir', 'Directory to read/record journal positions', false, true ); $this->addOption( 'posdump', 'Just dump current journal position into the position dir.' ); $this->addOption( 'postime', 'For position dumps, get the ID at this time', false, true ); + $this->addOption( 'backoff', 'Stop at entries younger than this age (sec).', false, true ); $this->addOption( 'verbose', 'Verbose mode', false, false, 'v' ); $this->setBatchSize( 50 ); } @@ -88,7 +89,13 @@ class SyncFileBackend extends Maintenance { } else { $startFromPosFile = false; } - $end = $this->getOption( 'end', INF ); + + if ( $this->hasOption( 'backoff' ) ) { + $time = time() - $this->getOption( 'backoff', 0 ); + $end = (int)$src->getJournal()->getPositionAtTime( $time ); + } else { + $end = $this->getOption( 'end', INF ); + } $this->output( "Synchronizing backend '{$dst->getName()}' to '{$src->getName()}'...\n" ); $this->output( "Starting journal position is $start.\n" ); @@ -96,8 +103,15 @@ class SyncFileBackend extends Maintenance { $this->output( "Ending journal position is $end.\n" ); } + // Periodically update the position file + $callback = function( $pos ) use ( $startFromPosFile, $posFile, $start ) { + if ( $startFromPosFile && $pos >= $start ) { // successfully advanced + file_put_contents( $posFile, $pos, LOCK_EX ); + } + }; + // Actually sync the dest backend with the reference backend - $lastOKPos = $this->syncBackends( $src, $dst, $start, $end ); + $lastOKPos = $this->syncBackends( $src, $dst, $start, $end, $callback ); // Update the sync position file if ( $startFromPosFile && $lastOKPos >= $start ) { // successfully advanced @@ -131,9 +145,12 @@ class SyncFileBackend extends Maintenance { * @param $dst FileBackend * @param $start integer Starting journal position * @param $end integer Starting journal position + * @param $callback Closure Callback to update any position file * @return integer|false Journal entry ID or false if there are none */ - protected function syncBackends( FileBackend $src, FileBackend $dst, $start, $end ) { + protected function syncBackends( + FileBackend $src, FileBackend $dst, $start, $end, Closure $callback + ) { $lastOKPos = 0; // failed $first = true; // first batch @@ -164,6 +181,7 @@ class SyncFileBackend extends Maintenance { $status = $this->syncFileBatch( array_keys( $pathsInBatch ), $src, $dst ); if ( $status->isOK() ) { $lastOKPos = max( $lastOKPos, $lastPosInBatch ); + $callback( $lastOKPos ); // update position file } else { $this->error( print_r( $status->getErrorsArray(), true ) ); break; // no gaps; everything up to $lastPos must be OK @@ -278,4 +296,4 @@ class SyncFileBackend extends Maintenance { } $maintClass = "SyncFileBackend"; -require_once( RUN_MAINTENANCE_IF_MAIN ); +require_once RUN_MAINTENANCE_IF_MAIN;