- if (!file_exists($sImportFile)) {
- // First check if there are new updates published (except for minutelies - there's always new diffs to process)
- if (CONST_Replication_Update_Interval > 60) {
- unset($aReplicationLag);
- exec($sCMDCheckReplicationLag, $aReplicationLag, $iErrorLevel);
- while ($iErrorLevel > 0 || $aReplicationLag[0] < 1) {
- if ($iErrorLevel) {
- echo "Error: $iErrorLevel. ";
- echo "Re-trying: ".$sCMDCheckReplicationLag." in ".CONST_Replication_Recheck_Interval." secs\n";
- } else {
- echo ".";
- }
+ $aLastState = chksql($oDB->getRow('SELECT * FROM import_status'));
+
+ if (!$aLastState['sequence_id']) {
+ echo "Updates not set up. Please run ./utils/update.php --init-updates.\n";
+ exit(1);
+ }
+
+ echo 'Currently at sequence '.$aLastState['sequence_id'].' ('.$aLastState['lastimportdate'].') - '.$aLastState['indexed']." indexed\n";
+
+ $sBatchEnd = $aLastState['lastimportdate'];
+ $iEndSequence = $aLastState['sequence_id'];
+
+ if ($aLastState['indexed'] == 't') {
+ // Sleep if the update interval has not yet been reached.
+ $fNextUpdate = $aLastState['lastimportdate'] + CONST_Replication_Update_Interval;
+ if ($fNextUpdate > $fStartTime) {
+ $iSleepTime = $fNextUpdate - $fStartTime;
+ echo "Waiting for next update for $iSleepTime sec.";
+ sleep($iSleepTime);
+ }
+
+ // Download the next batch of changes.
+ unlink($sImportFile);
+ do {
+ $fCMDStartTime = time();
+ $iNextSeq = (int) $aLastState['sequence_id'] + 1;
+ unset($aOutput);
+ echo "$sCMDDownload -I $iNextSeq\n";
+ exec($sCMDDownload.' -I '.$iNextSeq, $aOutput, $iResult);
+
+ if ($iResult == 3) {
+ echo 'No new updates. Sleeping for '.CONST_Replication_Recheck_Interval." sec.\n";