diff options
author | Brion Vibber <brion@pobox.com> | 2010-09-28 15:45:00 -0700 |
---|---|---|
committer | Brion Vibber <brion@pobox.com> | 2010-09-28 15:59:29 -0700 |
commit | 62d9b66dffb0000122cd611a9d991da008f9a527 (patch) | |
tree | 39911f2ab88c83edd916d9167d5b8aa1222db222 /plugins/YammerImport/lib | |
parent | c62e4d08008faf7fdfb67f0e5eda3db6d8d1ceef (diff) |
Made YammerImport more robust against errors; can now pause/resume/reset the import state from the admin interface.
Diffstat (limited to 'plugins/YammerImport/lib')
-rw-r--r-- | plugins/YammerImport/lib/yammerimporter.php | 1 | ||||
-rw-r--r-- | plugins/YammerImport/lib/yammerprogressform.php | 39 | ||||
-rw-r--r-- | plugins/YammerImport/lib/yammerqueuehandler.php | 23 | ||||
-rw-r--r-- | plugins/YammerImport/lib/yammerrunner.php | 42 |
4 files changed, 90 insertions, 15 deletions
diff --git a/plugins/YammerImport/lib/yammerimporter.php b/plugins/YammerImport/lib/yammerimporter.php index ed1191525..80cbcff8e 100644 --- a/plugins/YammerImport/lib/yammerimporter.php +++ b/plugins/YammerImport/lib/yammerimporter.php @@ -132,6 +132,7 @@ class YammerImporter if ($noticeId) { return Notice::staticGet('id', $noticeId); } else { + $notice = Notice::staticGet('uri', $data['options']['uri']); $content = $data['content']; $user = User::staticGet($data['profile']); diff --git a/plugins/YammerImport/lib/yammerprogressform.php b/plugins/YammerImport/lib/yammerprogressform.php index 776efa100..add8d9ab2 100644 --- a/plugins/YammerImport/lib/yammerprogressform.php +++ b/plugins/YammerImport/lib/yammerprogressform.php @@ -9,7 +9,7 @@ class YammerProgressForm extends Form */ function id() { - return 'yammer-progress'; + return 'yammer-progress-form'; } /** @@ -39,8 +39,11 @@ class YammerProgressForm extends Form */ function formData() { + $this->out->hidden('subaction', 'progress'); + $runner = YammerRunner::init(); + $error = $runner->lastError(); $userCount = $runner->countUsers(); $groupCount = $runner->countGroups(); $fetchedCount = $runner->countFetchedNotices(); @@ -86,7 +89,13 @@ class YammerProgressForm extends Form $steps = array_keys($labels); $currentStep = array_search($runner->state(), $steps); - $this->out->elementStart('fieldset', array('class' => 'yammer-import')); + $classes = array('yammer-import'); + if ($error) { + $classes[] = 'yammer-error'; + } else { + $classes[] = 'yammer-running'; + } + $this->out->elementStart('fieldset', array('class' => implode(' ', $classes))); $this->out->element('legend', array(), _m('Import status')); foreach ($steps as $step => $state) { if ($state == 'init') { @@ -104,7 +113,8 @@ class YammerProgressForm extends Form $this->progressBar($state, 'progress', $labels[$state]['label'], - $labels[$state]['progress']); + $labels[$state]['progress'], + $error); } else { // This step has not yet been done. $this->progressBar($state, @@ -116,13 +126,34 @@ class YammerProgressForm extends Form $this->out->elementEnd('fieldset'); } - private function progressBar($state, $class, $label, $status) + private function progressBar($state, $class, $label, $status, $error=null) { // @fixme prettify ;) $this->out->elementStart('div', array('class' => "import-step import-step-$state $class")); $this->out->element('div', array('class' => 'import-label'), $label); $this->out->element('div', array('class' => 'import-status'), $status); + if ($class == 'progress') { + if ($state == 'done') { + $this->out->submit('abort-import', _m('Reset import state')); + } else { + if ($error) { + $this->errorBox($error); + } else { + $this->out->submit('pause-import', _m('Pause import')); + } + } + } $this->out->elementEnd('div'); } + private function errorBox($msg) + { + $errline = sprintf(_m('Encountered error "%s"'), $msg); + $this->out->elementStart('fieldset', array('class' => 'import-error')); + $this->out->element('legend', array(), _m('Paused')); + $this->out->element('p', array(), $errline); + $this->out->submit('continue-import', _m('Continue')); + $this->out->submit('abort-import', _m('Abort import')); + $this->out->elementEnd('fieldset'); + } } diff --git a/plugins/YammerImport/lib/yammerqueuehandler.php b/plugins/YammerImport/lib/yammerqueuehandler.php index acc807311..0c4e8aec1 100644 --- a/plugins/YammerImport/lib/yammerqueuehandler.php +++ b/plugins/YammerImport/lib/yammerqueuehandler.php @@ -38,21 +38,24 @@ class YammerQueueHandler extends QueueHandler { $runner = YammerRunner::init(); if ($runner->hasWork()) { - if ($runner->iterate()) { - if ($runner->hasWork()) { - // More to do? Shove us back on the queue... - $runner->startBackgroundImport(); + try { + if ($runner->iterate()) { + if ($runner->hasWork()) { + // More to do? Shove us back on the queue... + $runner->startBackgroundImport(); + } + } + } catch (Exception $e) { + try { + $runner->recordError($e->getMessage()); + } catch (Exception $f) { + common_log(LOG_ERR, "Error while recording error in Yammer background import: " . $e->getMessage() . " " . $f->getMessage()); } - return true; - } else { - // Something failed? - // @fixme should we be trying again here, or should we give warning? - return false; } } else { // We're done! common_log(LOG_INFO, "Yammer import has no work to do at this time; discarding."); - return true; } + return true; } } diff --git a/plugins/YammerImport/lib/yammerrunner.php b/plugins/YammerImport/lib/yammerrunner.php index e0aec0d16..3e53f3361 100644 --- a/plugins/YammerImport/lib/yammerrunner.php +++ b/plugins/YammerImport/lib/yammerrunner.php @@ -298,7 +298,10 @@ class YammerRunner $this->state->state = 'save-messages'; } else { foreach ($messages as $item) { - Yammer_notice_stub::record($item['id'], $item); + $stub = Yammer_notice_stub::staticGet($item['id']); + if (!$stub) { + Yammer_notice_stub::record($item['id'], $item); + } $oldest = $item['id']; } $this->state->messages_oldest = $oldest; @@ -395,4 +398,41 @@ class YammerRunner $qm->enqueue('YammerImport', 'yammer'); } + /** + * Record an error condition from a background run, which we should + * display in progress state for the admin. + * + * @param string $msg + */ + public function recordError($msg) + { + // HACK HACK HACK + try { + $temp = new Yammer_state(); + $temp->query('ROLLBACK'); + } catch (Exception $e) { + common_log(LOG_ERR, 'Exception while confirming rollback while recording error: ' . $e->getMessage()); + } + $old = clone($this->state); + $this->state->last_error = $msg; + $this->state->update($old); + } + + /** + * Clear the error state. + */ + public function clearError() + { + $this->recordError(''); + } + + /** + * Get the last recorded background error message, if any. + * + * @return string + */ + public function lastError() + { + return $this->state->last_error; + } } |