Tinebase_ActionQueue - fix tests
authorPaul Mehrer <p.mehrer@metaways.de>
Tue, 20 Jun 2017 08:37:51 +0000 (10:37 +0200)
committerPaul Mehrer <p.mehrer@metaways.de>
Wed, 21 Jun 2017 05:50:09 +0000 (07:50 +0200)
Change-Id: Ibd48e4919a0ec7649c8c66e9ffd665d643400559
Reviewed-on: http://gerrit.tine20.com/customers/4905
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Paul Mehrer <p.mehrer@metaways.de>
Tested-by: Paul Mehrer <p.mehrer@metaways.de>
tests/tine20/Tinebase/DaemonTest.php
tine20/Tinebase/ActionQueue/Worker.php

index 0df99ef..8c6d633 100644 (file)
@@ -23,13 +23,11 @@ class Tinebase_DaemonTest extends PHPUnit_Framework_TestCase
 
     public static function setUpBeforeClass()
     {
-        static::$oldActionQueueConfig = Tinebase_Core::getConfig()->{Tinebase_Config::ACTIONQUEUE};
-        $actionQueueConfig = static::$oldActionQueueConfig;
+        static::$oldActionQueueConfig = Tinebase_Config::getInstance()->{Tinebase_Config::ACTIONQUEUE};
+        $actionQueueConfig = clone static::$oldActionQueueConfig;
         $actionQueueConfig->{Tinebase_Config::ACTIONQUEUE_BACKEND} = 'redis';
         $actionQueueConfig->{Tinebase_Config::ACTIONQUEUE_ACTIVE} = true;
 
-        Tinebase_Core::getConfig()->set(Tinebase_Config::ACTIONQUEUE, $actionQueueConfig);
-
         if (@is_file('/etc/tine20/actionQueue.ini')) {
             static::$oldIniFileContent = file_get_contents('/etc/tine20/actionQueue.ini');
         }
@@ -40,7 +38,7 @@ class Tinebase_DaemonTest extends PHPUnit_Framework_TestCase
 
     public static function tearDownAfterClass()
     {
-        Tinebase_Core::getConfig()->set(Tinebase_Config::ACTIONQUEUE, static::$oldActionQueueConfig);
+        Tinebase_Core::getConfig()->{Tinebase_Config::ACTIONQUEUE} = static::$oldActionQueueConfig;
         static::$oldActionQueueConfig = null;
 
         if (null !== static::$oldIniFileContent) {
@@ -51,6 +49,7 @@ class Tinebase_DaemonTest extends PHPUnit_Framework_TestCase
         }
 
         @unlink('/var/run/tine20/DummyController.txt');
+        Tinebase_ActionQueue::destroyInstance();
     }
 
     protected function setUp()
@@ -74,8 +73,6 @@ class Tinebase_DaemonTest extends PHPUnit_Framework_TestCase
 
     public function testStartStop()
     {
-        static::markTestSkipped('FIXME');
-
         clearstatcache();
         $this->assertFalse(is_file('/var/run/tine20/actionQueue.pid'), 'found old pid file');
 
@@ -91,11 +88,10 @@ class Tinebase_DaemonTest extends PHPUnit_Framework_TestCase
 
     public function testGracefulShutDown()
     {
-        static::markTestSkipped('FIXME');
-        
         $this->testStart();
 
         @unlink('/var/run/tine20/DummyController.txt');
+        Tinebase_ActionQueue::destroyInstance();
         Tinebase_ActionQueue::getInstance()->queueAction('Tinebase_FOO_DummyController.sleepNSec', 2);
 
         // 10 ms so the daemon can pick up the job
@@ -106,6 +102,7 @@ class Tinebase_DaemonTest extends PHPUnit_Framework_TestCase
         $endTime = microtime(true);
         $totalTime = $endTime - $startTime;
 
+        clearstatcache();
         $this->assertTrue(is_file('/var/run/tine20/DummyController.txt'), 'could not find file /var/run/tine20/DummyController.txt');
         $this->assertEquals('success 2', file_get_contents('/var/run/tine20/DummyController.txt'));
         $this->assertTrue($totalTime > 1.9, 'shut down should take more than 1.9 sec: ' . $totalTime);
index 8633ba8..968a41c 100755 (executable)
@@ -52,27 +52,26 @@ class Tinebase_ActionQueue_Worker extends Console_Daemon
      */
     public function run()
     {
-        $actionQueueConfig = Tinebase_Core::getConfig()->getConfigFileSection(Tinebase_Config::ACTIONQUEUE);
-        if (!is_array($actionQueueConfig) || !isset($actionQueueConfig[Tinebase_Config::ACTIONQUEUE]) ||
-                !is_array($actionQueueConfig[Tinebase_Config::ACTIONQUEUE]) ||
-                !isset($actionQueueConfig[Tinebase_Config::ACTIONQUEUE][Tinebase_Config::ACTIONQUEUE_BACKEND]) ||
-                'redis' !== strtolower(
-                    $actionQueueConfig[Tinebase_Config::ACTIONQUEUE][Tinebase_Config::ACTIONQUEUE_BACKEND])) {
-            $this->_getLogger()->crit(__METHOD__ . '::' . __LINE__ . ' Tinebase_ActionQueue_Backend_Direct used. There is nothing to do for the worker! Configure Redis backend for example if you want to make use of the worker.');
+        $actionQueue = Tinebase_ActionQueue::getInstance();
+        if ($actionQueue->getBackendType() !== 'Tinebase_ActionQueue_Backend_Redis') {
+            $this->_getLogger()->crit(__METHOD__ . '::' . __LINE__ . ' not Tinebase_ActionQueue_Backend_Redis used. There is nothing to do for the worker! Configure Redis backend if you want to make use of the worker.');
             exit(1);
         }
 
         $maxChildren = $this->_getConfig()->tine20->maxChildren;
+        $lastMaxChildren = 0;
 
         while (!$this->_stopped) {
 
             // manage the number of children
             if (count ($this->_children) >=  $maxChildren) {
 
-                // TODO this will log A LOT, add a timeout between logs...
-
-                $this->_getLogger()->crit(__METHOD__ . '::' . __LINE__ .    " reached max children limit: " . $this->_getConfig()->tine20->maxChildren);
-                $this->_getLogger()->info(__METHOD__ . '::' . __LINE__ .    " number of pending jobs:" . Tinebase_ActionQueue::getInstance()->getQueueSize());
+                // log only every minute
+                if (time() - $lastMaxChildren > 60) {
+                    $this->_getLogger()->crit(__METHOD__ . '::' . __LINE__ . " reached max children limit: " . $maxChildren);
+                    $this->_getLogger()->info(__METHOD__ . '::' . __LINE__ . " number of pending jobs:" . $actionQueue->getQueueSize());
+                    $lastMaxChildren = time();
+                }
                 usleep(1000); // save some trees
                 pcntl_signal_dispatch();
                 continue;
@@ -80,7 +79,7 @@ class Tinebase_ActionQueue_Worker extends Console_Daemon
             
             $this->_getLogger()->debug(__METHOD__ . '::' . __LINE__ .    " trying to fetch a job from queue " . microtime(true));
 
-            $jobId = Tinebase_ActionQueue::getInstance()->waitForJob();
+            $jobId = $actionQueue->waitForJob();
 
             $this->_getLogger()->debug(__METHOD__ . '::' . __LINE__ .    " signal dispatch " . microtime(true));
 
@@ -92,14 +91,14 @@ class Tinebase_ActionQueue_Worker extends Console_Daemon
             }
             
             try {
-                $job = Tinebase_ActionQueue::getInstance()->receive($jobId);
+                $job = $actionQueue->receive($jobId);
             } catch (RuntimeException $re) {
                 $this->_getLogger()->crit(__METHOD__ . '::' . __LINE__ . " failed to receive message: " . $re->getMessage());
                 
                 // we are unable to process the job
                 // probably the retry count is exceeded
                 // TODO push message to dead letter queue
-                Tinebase_ActionQueue::getInstance()->delete($jobId);
+                $actionQueue->delete($jobId);
                 
                 continue;
             }
@@ -239,7 +238,7 @@ class Tinebase_ActionQueue_Worker extends Console_Daemon
 
         // execute in same process
         } else { */
-            //Zend_Registry::_unsetInstance();
+            Zend_Registry::_unsetInstance();
 
             Tinebase_Core::initFramework();