[icinga-checkins] icinga.org: icingaweb2-module-director/feature-ddo-playground: EventstreamCommand: add new processStates action

git at icinga.org git at icinga.org
Tue Jul 19 10:31:52 CEST 2016


Module: icingaweb2-module-director
Branch: feature-ddo-playground
Commit: 6752fddc569a9246d92b4d5377ba3e269ea8ade6
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=6752fddc569a9246d92b4d5377ba3e269ea8ade6

Author: Thomas Gelf <thomas at gelf.net>
Date:   Tue Jun 21 13:51:07 2016 +0200

EventstreamCommand: add new processStates action

---

 application/clicommands/EventstreamCommand.php |   50 +++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/application/clicommands/EventstreamCommand.php b/application/clicommands/EventstreamCommand.php
index 2fb69e5..71d6c4f 100644
--- a/application/clicommands/EventstreamCommand.php
+++ b/application/clicommands/EventstreamCommand.php
@@ -3,10 +3,11 @@
 namespace Icinga\Module\Director\Clicommands;
 
 use Exception;
-use Icinga\Module\Director\Cli\Command;
+use Icinga\Module\Director\Cli\DdoCommand;
+use Icinga\Module\Director\Ddo\StateList;
 use Icinga\Module\Director\Redis;
 
-class EventstreamCommand extends Command
+class EventstreamCommand extends DdoCommand
 {
     public function streamAction()
     {
@@ -30,6 +31,51 @@ class EventstreamCommand extends Command
         );
     }
 
+    public function processStatesAction()
+    {
+        $redis = Redis::instance();
+        $time = time();
+        $cnt = 0;
+        $hasTransaction = false;
+        $ddo = $this->ddo();
+        $db = $ddo->getDbAdapter();
+        $list = new StateList($ddo);
+
+        // TODO: 0 is forever, leave loop after a few sec and enter again
+        while (true) {
+
+            while ($res = $redis->brpop('icinga2::events', 3)) {
+                // res = array(queuename, value)
+                $object = $list->processCheckResult(json_decode($res[1]));
+
+                $cnt++;
+
+                if ($object->hasBeenModified()) {
+                    printf("%s has been modified\n", $object->getUniqueName());
+
+                    if (! $hasTransaction) {
+                        $db->beginTransaction();
+                        $hasTransaction = true;
+                    }
+                    $object->store();
+                } else {
+                    printf("%s has not been modified\n", $object->getUniqueName());
+                }
+
+                if (($cnt >= 1000)
+                    || (($newtime = time()) - $time > 1)
+                ) {
+                    $time = $newtime;
+                    echo "Committing $cnt events\n";
+                    $cnt = 0;
+                    $db->commit();
+                    $hasTransaction = false;
+                }
+            }
+            echo "Got nothing for 3secs\n";
+        }
+    }
+
     public function storeEventsAction()
     {
         $redis = Redis::instance();



More information about the icinga-checkins mailing list