From f699ffeb8abdc46f1435705556782e889e743ce8 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Thu, 14 Jan 2016 02:04:15 +0100 Subject: [PATCH] Exception handling in queue handler logic --- lib/dbqueuemanager.php | 33 +++++++++++++------------- lib/noqueuehandlerexception.php | 41 +++++++++++++++++++++++++++++++++ lib/stompqueuemanager.php | 10 ++++---- lib/unqueuemanager.php | 18 +++++++-------- scripts/handlequeued.php | 20 ++++++++-------- 5 files changed, 81 insertions(+), 41 deletions(-) create mode 100644 lib/noqueuehandlerexception.php diff --git a/lib/dbqueuemanager.php b/lib/dbqueuemanager.php index 3adac08971..5497a1089a 100644 --- a/lib/dbqueuemanager.php +++ b/lib/dbqueuemanager.php @@ -89,23 +89,24 @@ class DBQueueManager extends QueueManager $rep = $this->logrep($item); $this->_log(LOG_DEBUG, "Got {$rep} for transport {$qi->transport}"); - $handler = $this->getHandler($qi->transport); - if ($handler) { - try { - $result = $handler->handle($item) - } catch (Exception $e) { - $result = false; - $this->_log(LOG_ERR, "[{$qi->transport}:$rep] Exception thrown: {$e->getMessage()}"); - } - if ($result) { - $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Successfully handled item"); - $this->_done($qi); - } else { - $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Failed to handle item"); - $this->_fail($qi); - } - } else { + try { + $handler = $this->getHandler($qi->transport); + $result = $handler->handle($item) + } catch (NoQueueHandlerException $e) { $this->noHandlerFound($qi, $rep); + return true; + } catch (Exception $e) { + $this->_log(LOG_ERR, "[{$qi->transport}:$rep] Exception thrown: {$e->getMessage()}"); + $this->_fail($qi); + return true; + } + + if ($result) { + $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Successfully handled item"); + $this->_done($qi); + } else { + $this->_log(LOG_INFO, "[{$qi->transport}:$rep] Failed to handle item"); + $this->_fail($qi); } return true; } diff --git a/lib/noqueuehandlerexception.php b/lib/noqueuehandlerexception.php new file mode 100644 index 0000000000..fabe1549f7 --- /dev/null +++ b/lib/noqueuehandlerexception.php @@ -0,0 +1,41 @@ +. + * + * @category Exception + * @package GNUsocial + * @author Mikael Nordfeldth + * @copyright 2016 Free Software Foundation, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://www.gnu.org/software/social/ + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +class NoQueueHandlerException extends ServerException +{ + public $transport; // The object with query that gave no results + + public function __construct($transport) + { + $this->transport = $transport; + parent::__construct(sprintf(_('No queue handler found for transport %s.'), _ve($this->transport))); + } +} diff --git a/lib/stompqueuemanager.php b/lib/stompqueuemanager.php index 4084470e21..7c3af4945e 100644 --- a/lib/stompqueuemanager.php +++ b/lib/stompqueuemanager.php @@ -509,15 +509,13 @@ class StompQueueManager extends QueueManager $frame->headers['created'] . " in queue $queue from $host"; $this->_log(LOG_DEBUG, "Dequeued $info"); - $handler = $this->getHandler($queue); - if (!$handler) { + try { + $handler = $this->getHandler($queue); + $ok = $handler->handle($item); + } catch (NoQueueHandlerException $e) { $this->_log(LOG_ERR, "Missing handler class; skipping $info"); $this->stats('badhandler', $queue); return false; - } - - try { - $ok = $handler->handle($item); } catch (Exception $e) { $this->_log(LOG_ERR, "Exception on queue $queue: " . $e->getMessage()); $ok = false; diff --git a/lib/unqueuemanager.php b/lib/unqueuemanager.php index 785de7c8ce..0540b767d2 100644 --- a/lib/unqueuemanager.php +++ b/lib/unqueuemanager.php @@ -38,19 +38,17 @@ class UnQueueManager extends QueueManager * that can be processed quickly and don't need polling or long-running * connections to another server such as XMPP. * - * @param Notice $object + * @param Notice $object this specific manager just handles Notice objects anyway * @param string $queue */ - function enqueue($object, $queue) + function enqueue($object, $transport) { - $notice = $object; - - $handler = $this->getHandler($queue); - if ($handler) { - $handler->handle($notice); - } else { - if (Event::handle('UnqueueHandleNotice', array(&$notice, $queue))) { - throw new ServerException("UnQueueManager: Unknown queue: $queue"); + try { + $handler = $this->getHandler($transport); + $handler->handle($object); + } catch (NoQueueHandlerException $e) { + if (Event::handle('UnqueueHandleNotice', array(&$object, $transport))) { + throw new ServerException("UnQueueManager: Unknown queue transport: $transport"); } } } diff --git a/scripts/handlequeued.php b/scripts/handlequeued.php index eb7d829713..0d77030c3f 100755 --- a/scripts/handlequeued.php +++ b/scripts/handlequeued.php @@ -38,19 +38,21 @@ $queue = trim($args[0]); $noticeId = intval($args[1]); $qm = QueueManager::get(); -$handler = $qm->getHandler($queue); -if (!$handler) { +try { + $handler = $qm->getHandler($queue); + $notice = Notice::getByID($noticeId); + $result = $handler->handle($notice); +} catch (NoQueueHandlerException $e) { print "No handler for queue '$queue'.\n"; exit(1); -} - -$notice = Notice::getKV('id', $noticeId); -if (empty($notice)) { - print "Invalid notice id $noticeId\n"; +} catch (NoResultException $e) { + print "{$e->getMessage()}\n"; + exit(1); +} catch (Exception $e) { + print "Exception thrown while handling: {$e->getMessage()}\n"; exit(1); } - -if (!$handler->handle($notice)) { +if (!$result) { print "Failed to handle notice id $noticeId on queue '$queue'.\n"; exit(1); } -- 2.39.5