status page
authorChristian Weiske <cweiske@cweiske.de>
Fri, 11 Nov 2016 19:54:12 +0000 (20:54 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 11 Nov 2016 19:54:12 +0000 (20:54 +0100)
data/templates/status.htm [new file with mode: 0644]
src/phinde/Elasticsearch.php
src/phinde/Queue.php
www/status.php [new file with mode: 0644]

diff --git a/data/templates/status.htm b/data/templates/status.htm
new file mode 100644 (file)
index 0000000..a5699ef
--- /dev/null
@@ -0,0 +1,45 @@
+{% extends "base.htm" %}
+
+{% block meta %}
+ <meta name="robots" value="noindex,nofollow"/>
+{% endblock %}
+
+{%block title %}phinde status{% endblock %}
+
+{% block maincontent %}
+<div class="container">
+ <div class="row">
+  <div class="span6 offset3">
+
+<h1>phinde status</h1>
+<table class="table table-striped">
+ <thead>
+  <tr>
+   <th>Item</th>
+   <th>Number</th>
+  </tr>
+ </thead>
+ <tbody>
+  <tr>
+   <td>Elasticsearch documents</td>
+   <td>{{esDocs}}</td>
+  </tr>
+  <tr>
+   <td>Open tasks</td>
+   <td>{{gearStatus.tasks}}</td>
+  </tr>
+  <tr>
+   <td>Tasks being processed</td>
+   <td>{{gearStatus.processing}}</td>
+  </tr>
+  <tr>
+   <td>Task workers</td>
+   <td>{{gearStatus.workers}}</td>
+  </tr>
+ </tbody>
+</table>
+
+  </div>
+ </div>
+</div>
+{% endblock %}
index 9babfee..069cf1f 100644 (file)
@@ -58,6 +58,16 @@ class Elasticsearch
         $r->send();
     }
 
+    public function countDocuments()
+    {
+        $r = new Elasticsearch_Request(
+            $this->baseUrl . 'document/_count',
+            \HTTP_Request2::METHOD_GET
+        );
+        $res = $r->send();
+        return json_decode($res->getBody())->count;
+    }
+
     public function search($query, $filters, $site, $page, $perPage, $sort)
     {
         if (preg_match_all('#nick:([^ ]*)#', $query, $matches)) {
index a58a257..1033d7a 100644 (file)
@@ -9,6 +9,7 @@ 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)
@@ -19,7 +20,7 @@ class Queue
         );
 
         $this->gmclient->doBackground(
-            $GLOBALS['phinde']['queuePrefix'] . 'phinde_process',
+            $this->queueName,
             serialize(
                 array(
                     'url'     => $linkUrl,
@@ -36,5 +37,23 @@ class Queue
             exit(2);
         }
     }
+
+    public function getServerStatus()
+    {
+        $cmd = 'gearadmin --status'
+            . '| grep ' . escapeshellarg($this->queueName);
+        $line = exec($cmd);
+
+        $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],
+        );
+    }
 }
 ?>
diff --git a/www/status.php b/www/status.php
new file mode 100644 (file)
index 0000000..3bc52cf
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+namespace phinde;
+require 'www-header.php';
+
+$es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
+$esDocs = $es->countDocuments();
+
+$queue = new Queue();
+$gearStatus = $queue->getServerStatus();
+
+render(
+    'status',
+    array(
+        'esDocs' => $esDocs,
+        'gearStatus' => $gearStatus,
+    )
+);
+?>