Tinebase ActionQueue - create a new process to execute job in
authorPaul Mehrer <p.mehrer@metaways.de>
Mon, 3 Jul 2017 09:27:37 +0000 (11:27 +0200)
committerPhilipp Schüle <p.schuele@metaways.de>
Mon, 3 Jul 2017 14:41:44 +0000 (16:41 +0200)
the forked child process makes trouble (db connections etc. due to copied
memory from parent). so we better start a completly new process to execute
our job in.

Change-Id: Ic2f81838c10b518c5b9f0b2838011f345777d5a6
Reviewed-on: http://gerrit.tine20.com/customers/4991
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tine20/Tinebase/ActionQueue/Worker.php
tine20/Tinebase/DummyController.php [new file with mode: 0644]
tine20/Tinebase/Frontend/Cli.php

index 968a41c..99845dd 100755 (executable)
@@ -230,14 +230,17 @@ class Tinebase_ActionQueue_Worker extends Console_Daemon
         $this->_getLogger()->debug(__METHOD__ . '::' . __LINE__ . " with isChild: " . var_export($this->_isChild, true));
 
         // execute in subprocess
-        /*if ($this->_getConfig()->tine20->executionMethod === self::EXECUTION_METHOD_EXEC_CLI) {
-            $output = system('php $paths ./../../tine20.php --method Tinebase.executeQueueJob message=' . escapeshellarg($job), $exitCode );
+        //if ($this->_getConfig()->tine20->executionMethod === self::EXECUTION_METHOD_EXEC_CLI) {
+        chdir(__DIR__);
+            exec('php -d include_path=' . escapeshellarg(get_include_path()) .
+                ' ./../../tine20.php --method Tinebase.executeQueueJob message=' .
+                escapeshellarg(json_encode($job)), $output, $exitCode);
             if ($exitCode != 0) {
-                throw new Exception('Problem during execution with shell: ' . $output);
+                throw new Exception('Problem during execution with shell: ' . join(PHP_EOL, $output));
             }
 
         // execute in same process
-        } else { */
+        /*} else {
             Zend_Registry::_unsetInstance();
 
             Tinebase_Core::initFramework();
@@ -246,9 +249,10 @@ class Tinebase_ActionQueue_Worker extends Console_Daemon
 
         if (true !== ($result = Tinebase_ActionQueue::getInstance()->executeAction($job))) {
             throw new Tinebase_Exception_UnexpectedValue('action queue job execution did not return true: ' . var_export($result, true));
-        }
+        }*/
         //}
 
-        $this->_getLogger()->debug(__METHOD__ . '::' . __LINE__ . " result: " . var_export($result, true));
+        //$this->_getLogger()->debug(__METHOD__ . '::' . __LINE__ . " result: " . var_export($result, true));
+        $this->_getLogger()->debug(__METHOD__ . '::' . __LINE__ . " output: " . var_export($output, true));
     }
 }
diff --git a/tine20/Tinebase/DummyController.php b/tine20/Tinebase/DummyController.php
new file mode 100644 (file)
index 0000000..1255dfb
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Tine 2.0 - http://www.tine20.org
+ *
+ * @package     Tinebase
+ * @license     http://www.gnu.org/licenses/agpl.html
+ * @copyright   Copyright (c) 2017-2017 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @author      Paul Mehrer <p.mehrer@metaways.de>
+ */
+
+
+/**
+ * Dummy Controller, to be used for example by queue with action like 'Tinebase_FOO_DummyController.someMethod'
+ */
+class Tinebase_DummyController
+{
+    public static function getInstance()
+    {
+        return new self();
+    }
+
+    public function sleepNSec($n)
+    {
+        /*if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+            . ' start sleeping...');*/
+
+        sleep($n);
+        file_put_contents('/var/run/tine20/DummyController.txt', 'success ' . $n);
+
+        /*if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
+            . ' done');*/
+
+        return true;
+    }
+}
\ No newline at end of file
index 7c411e5..23c49cc 100644 (file)
@@ -348,8 +348,6 @@ class Tinebase_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
      * process given queue job
      *  --message json encoded task
      *
-     * @TODO rework user management, jobs should be executed as the right user in future
-     * 
      * @param Zend_Console_Getopt $_opts
      * @return boolean success
      */
@@ -369,8 +367,16 @@ class Tinebase_Frontend_Cli extends Tinebase_Frontend_Cli_Abstract
         if (! $message) {
             throw new Tinebase_Exception_InvalidArgument('mandatory parameter "message" is missing');
         }
+
+        if (null === ($job = json_decode($message, true))) {
+            throw new Tinebase_Exception_InvalidArgument('parameter "message" can not be json decoded');
+        }
+
+        if (isset($job['account_id'])) {
+            Tinebase_Core::set(Tinebase_Core::USER, Tinebase_User::getInstance()->getFullUserById($job['account_id']));
+        }
         
-        Tinebase_ActionQueue::getInstance()->executeAction($message);
+        Tinebase_ActionQueue::getInstance()->executeAction($job);
         
         return TRUE;
     }