/** * Test state functionality. * * @return void */ public function testState() { $tmp = sys_get_temp_dir() . '/'; $date = '2016-07-12'; $manager = new StateManager($tmp); $manager->saveState('foo', 'bar', $date); $this->assertEquals(['foo', 'bar', $date], $manager->loadState()); $this->assertTrue(file_exists($tmp . 'last_state.txt')); $manager->clearState(); $this->assertFalse(file_exists($tmp . 'last_state.txt')); }
/** * Harvest all available documents. * * @return void */ public function launch() { // Normalize sets setting to an array: $sets = (array) $this->set; if (empty($sets)) { $sets = [null]; } // Load last state, if applicable (used to recover from server failure). if ($state = $this->stateManager->loadState()) { $this->write("Found saved state; attempting to resume.\n"); list($resumeSet, $resumeToken, $this->startDate) = $state; } // Loop through all of the selected sets: foreach ($sets as $set) { // If we're resuming and there are multiple sets, find the right one. if (isset($resumeToken) && $resumeSet != $set) { continue; } // If we have a token to resume from, pick up there now... if (isset($resumeToken)) { $token = $resumeToken; unset($resumeToken); } else { // ...otherwise, start harvesting at the requested date: $token = $this->getRecordsByDate($this->startDate, $set, $this->harvestEndDate); } // Keep harvesting as long as a resumption token is provided: while ($token !== false) { // Save current state in case we need to resume later: $this->stateManager->saveState($set, $token, $this->startDate); $token = $this->getRecordsByToken($token); } } // If we made it this far, all was successful, so we should clean up // the stored state. $this->stateManager->clearState(); }