Do not use STDOUT and STDERR constants
[phinde.git] / src / phinde / Queue.php
index 6c30faa7294c70c5af046aa4cbe2c61270445e44..a8ead3d56d5bc682bbcc24b0c0e00a613acdf0e7 100644 (file)
@@ -9,15 +9,18 @@ class Queue
     {
         $this->gmclient = new \GearmanClient();
         $this->gmclient->addServer('127.0.0.1');
+        $this->queueName = $GLOBALS['phinde']['queuePrefix'] . 'phinde_process';
     }
 
     public function addToProcessList($linkUrl, $actions)
     {
-        echo "Queuing for processing: $linkUrl"
+        Log::info(
+            "Queuing for processing: $linkUrl"
             . ' (' . implode(',', $actions) . ')'
-            . "\n";
+        );
+
         $this->gmclient->doBackground(
-            $GLOBALS['phinde']['queuePrefix'] . 'phinde_process',
+            $this->queueName,
             serialize(
                 array(
                     'url'     => $linkUrl,
@@ -26,11 +29,35 @@ class Queue
             )
         );
         if ($this->gmclient->returnCode() != GEARMAN_SUCCESS) {
-            echo 'Error queueing URL processing for '
+            Log::error(
+                'Error queueing URL processing for '
                 . $linkUrl . "\n"
-                . 'Error code: ' . $this->gmclient->returnCode() . "\n";
+                . 'Error code: ' . $this->gmclient->returnCode()
+            );
             exit(2);
         }
     }
+
+    public function getServerStatus()
+    {
+        $cmd = 'gearadmin --status'
+            . '| grep ' . escapeshellarg($this->queueName);
+        $line = exec($cmd);
+        if ($line === '') {
+            //job not registered
+            $parts = [0, -1, -1, -1];
+        } else {
+            $parts = preg_split('#\s+#', $line);
+            if (count($parts) !== 4) {
+                throw new \Exception('gearadmin status line does not have 4 parts');
+            }
+        }
+
+        return array(
+            'tasks'      => $parts[1],
+            'processing' => $parts[2],
+            'workers'    => $parts[3],
+        );
+    }
 }
 ?>