]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/dbqueuemanager.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / dbqueuemanager.php
index 51553b899906b9ae64e58ea9ceaa5f70bc9185f6..45c4b694d2e4848237bcae4563461fbb79ab55be 100644 (file)
@@ -44,7 +44,7 @@ class DBQueueManager extends QueueManager
         $qi->created   = common_sql_now();
         $result        = $qi->insert();
 
-        if (!$result) {
+        if ($result === false) {
             common_log_db_error($qi, 'INSERT', __FILE__);
             throw new ServerException('DB error inserting queue item');
         }
@@ -71,55 +71,59 @@ class DBQueueManager extends QueueManager
      */
     public function poll()
     {
-        $this->_log(LOG_DEBUG, 'Checking for notices...');
+        //$this->_log(LOG_DEBUG, 'Checking for notices...');
         $qi = Queue_item::top($this->activeQueues());
-        if (empty($qi)) {
-            $this->_log(LOG_DEBUG, 'No notices waiting; idling.');
+        if (!$qi instanceof Queue_item) {
+            //$this->_log(LOG_DEBUG, 'No notices waiting; idling.');
             return false;
         }
 
-        $queue = $qi->transport;
-        $item = $this->decode($qi->frame);
+        try {
+            $item = $this->decode($qi->frame);
+        } catch (Exception $e) {
+            $this->_log(LOG_INFO, "[{$qi->transport}] Discarding: ".$e->getMessage());
+            $this->_done($qi);
+            return true;
+        }
 
-        if ($item) {
-            $rep = $this->logrep($item);
-            $this->_log(LOG_INFO, "Got $rep for transport $queue");
-            
-            $handler = $this->getHandler($queue);
-            if ($handler) {
-                if ($handler->handle($item)) {
-                    $this->_log(LOG_INFO, "[$queue:$rep] Successfully handled item");
-                    $this->_done($qi);
-                } else {
-                    $this->_log(LOG_INFO, "[$queue:$rep] Failed to handle item");
-                    $this->_fail($qi);
-                }
-            } else {
-                $this->_log(LOG_INFO, "[$queue:$rep] No handler for queue $queue; discarding.");
+        $rep = $this->logrep($item);
+        $this->_log(LOG_DEBUG, "Got {$rep} for transport {$qi->transport}");
+        
+        $handler = $this->getHandler($qi->transport);
+        if ($handler) {
+            if ($handler->handle($item)) {
+                $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 {
-            $this->_log(LOG_INFO, "[$queue] Got empty/deleted item, discarding");
-            $this->_done($qi);
+            $this->noHandlerFound($qi, $rep);
         }
         return true;
     }
 
+    // What to do if no handler was found. For example, the OpportunisticQM
+    // should avoid deleting items just because it can't reach XMPP queues etc.
+    protected function noHandlerFound(Queue_item $qi, $rep=null) {
+        $this->_log(LOG_INFO, "[{$qi->transport}:{$rep}] No handler for queue {$qi->transport}; discarding.");
+        $this->_done($qi);
+    }
+
     /**
      * Delete our claimed item from the queue after successful processing.
      *
      * @param QueueItem $qi
      */
-    protected function _done($qi)
+    protected function _done(Queue_item $qi)
     {
-        $queue = $qi->transport;
-
         if (empty($qi->claimed)) {
-            $this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item $qi->id from $qi->queue");
+            $this->_log(LOG_WARNING, "Reluctantly releasing unclaimed queue item {$qi->id} from {$qi->transport}");
         }
         $qi->delete();
 
-        $this->stats('handled', $queue);
+        $this->stats('handled', $qi->transport);
     }
 
     /**
@@ -128,16 +132,16 @@ class DBQueueManager extends QueueManager
      *
      * @param QueueItem $qi
      */
-    protected function _fail($qi)
+    protected function _fail(Queue_item $qi, $releaseOnly=false)
     {
-        $queue = $qi->transport;
-
         if (empty($qi->claimed)) {
-            $this->_log(LOG_WARNING, "[$queue:item $qi->id] Ignoring failure for unclaimed queue item");
+            $this->_log(LOG_WARNING, "[{$qi->transport}:item {$qi->id}] Ignoring failure for unclaimed queue item");
         } else {
             $qi->releaseClaim();
         }
 
-        $this->stats('error', $queue);
+        if (!$releaseOnly) {
+            $this->stats('error', $qi->transport);
+        }
     }
 }