Permalink
Browse files

#6530: allow to trigger redis workers

- added redis queue + test
- added abstract console daemon
- added abstract redis worker + test
- added redisWorker start script

https://forge.tine20.org/mantisbt/view.php?id=6530

Change-Id: I529e22d74b4a39a3532fd4cb8d67c1cfe9126adc
Reviewed-on: https://gerrit.tine20.org/tine20/707
Tested-by: jenkins user
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
  • Loading branch information...
1 parent b6a6415 commit 8c85cf9f036ede6b94b1b070de3ab6cc384cb084 @pschuele pschuele committed Jun 27, 2012
View
@@ -0,0 +1,49 @@
+#!/usr/bin/env php
+<?php
+/**
+ * redis worker run script
+ *
+ * @package Cli
+ * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author Philipp Schüle <p.schuele@metaways.de>
+ * @copyright Copyright (c) 2012 Metaways Infosystems GmbH (http://www.metaways.de)
+ *
+ * you need to adjust some paths (tine + daemon)
+ *
+ * you need an config.ini file that should look like this:
+
+[redis]
+host = localhost
+port = 6379
+
+ */
+
+// TODO adjust paths
+$tine20path = dirname(dirname(__FILE__)). '/tine20';
+$workerPath = dirname(dirname(__FILE__)) . '/tests/tine20/Tinebase/Redis/RedisWorker.php';
+
+if (php_sapi_name() != 'cli') {
+ die('Not allowed: wrong sapi name!');
+}
+
+set_time_limit(0);
+ob_implicit_flush();
+declare(ticks = 1);
+
+$paths = array(
+ $tine20path,
+ $tine20path . '/library',
+ get_include_path()
+);
+set_include_path(implode(PATH_SEPARATOR, $paths));
+
+require_once 'Zend/Loader/Autoloader.php';
+$autoloader = Zend_Loader_Autoloader::getInstance();
+$autoloader->setFallbackAutoloader(true);
+Tinebase_Autoloader::initialize($autoloader);
+
+// NOTE: you need to include your worker class here
+require_once $workerPath;
+$worker = new RedisWorker();
+
+$worker->run();
@@ -4,7 +4,7 @@
*
* @package Tinebase
* @license http://www.gnu.org/licenses/agpl.html
- * @copyright Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright Copyright (c) 2007-2012 Metaways Infosystems GmbH (http://www.metaways.de)
* @author Matthias Greiling <m.greiling@metaways.de>
*/
@@ -60,6 +60,7 @@ public static function suite()
$suite->addTestSuite('Tinebase_Model_Filter_TextTest');
$suite->addTestSuite('Tinebase_TagsTest');
$suite->addTestSuite('Tinebase_Log_AllTests');
+ $suite->addTestSuite('Tinebase_Redis_QueueTest');
$suite->addTest(Tinebase_User_AllTests::suite());
$suite->addTest(Tinebase_Group_AllTests::suite());
@@ -0,0 +1,59 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package CustomEventHooks
+ * @subpackage Redis
+ * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author Philipp Schüle <p.schuele@metaways.de>
+ * @copyright Copyright (c) 2012 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+
+/**
+ * custom event handlers for redis
+ * NOTE: can be used as template for custom jobs pushed to redis queue
+ *
+ * @package CustomEventHooks
+ * @subpackage Redis
+ */
+class CustomEventHooks
+{
+ /**
+ * implement logic for each controller in this function
+ *
+ * @param Tinebase_Event_Abstract $_eventObject
+ */
+ public static function handleEvent(Tinebase_Event_Abstract $_eventObject)
+ {
+ if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' (' . __LINE__ . ') handle event of type ' . get_class($_eventObject));
+
+ switch(get_class($_eventObject)) {
+ case 'Admin_Event_AddAccount':
+ $data = array(
+ 'user' => $_eventObject->account->toArray(),
+ 'action' => 'create',
+ );
+ break;
+/*
+ case 'Admin_Event_DeleteAccount':
+ break;
+ case 'Admin_Event_UpdateGroup':
+ break;
+ case 'Admin_Event_AddGroupMember':
+ break;
+ case 'Admin_Event_RemoveGroupMember':
+ break;
+ case 'Tinebase_Event_Container_BeforeCreate':
+ break;
+*/
+ default:
+ // do nothing
+ return;
+ }
+
+ if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . ' (' . __LINE__ . ') Push to Redis queue: ' . print_r($data, TRUE));
+
+ $redisQueue = new Tinebase_Redis_Queue();
+ $redisQueue->push($data);
+ }
+}
@@ -0,0 +1,139 @@
+<?php
+/**
+ * Tine 2.0 - http://www.tine20.org
+ *
+ * @package Tinebase
+ * @subpackage Redis
+ * @license http://www.gnu.org/licenses/agpl.html
+ * @copyright Copyright (c) 2012 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @author Philipp Schüle <p.schuele@metaways.de>
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
+
+/**
+ * event hooks
+ */
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'CustomEventHooks.php';
+
+/**
+ * redis worker
+ */
+require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'RedisWorker.php';
+
+/**
+ * Test class for Tinebase_Redis_Queue
+ */
+class Tinebase_Redis_QueueTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Runs the test methods of this class.
+ *
+ * @access public
+ * @static
+ */
+ public static function main()
+ {
+ $suite = new PHPUnit_Framework_TestSuite('Tinebase_Redis_QueueTest');
+ PHPUnit_TextUI_TestRunner::run($suite);
+ }
+
+ /**
+ * Redis queue
+ *
+ * @var Tinebase_Redis_Queue
+ */
+ protected $_redis = NULL;
+
+ /**
+ * redis config
+ *
+ * @var array
+ */
+ protected $_redisConfig = NULL;
+
+ /**
+ * Sets up the fixture.
+ * This method is called before a test is executed.
+ *
+ * @access protected
+ */
+ protected function setUp()
+ {
+ if (! extension_loaded('redis')) {
+ $this->markTestSkipped('redis extension not found');
+ }
+
+ $config = Tinebase_Config::getInstance()->get('redis', NULL);
+ if ($config === NULL) {
+ $this->markTestSkipped('redis not configured');
+ }
+ $this->_redisConfig = $config->toArray();
+
+ Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
+ $this->_redis = new Tinebase_Redis_Queue($this->_redisConfig);
+ }
+
+ /**
+ * Tears down the fixture
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ */
+ protected function tearDown()
+ {
+ Tinebase_TransactionManager::getInstance()->rollBack();
+ }
+
+ /**
+ * testAddUserEvent
+ */
+ public function testAddUserEvent()
+ {
+ $queueSizeBefore = $this->_redis->getQueueSize();
+ $user = $this->_createUser();
+ $queueSizeAfter = $this->_redis->getQueueSize();
+ $this->assertEquals($queueSizeBefore + 1, $queueSizeAfter);
+
+ $event = $this->_redis->pop();
+ $this->assertEquals('create', $event->action);
+ $this->assertTrue(isset($event->user));
+ $this->assertEquals($user->accountLoginName, $event->user->accountLoginName);
+ }
+
+ /**
+ * create user
+ *
+ * @return Tinebase_Model_FullUser
+ */
+ protected function _createUser()
+ {
+ return Admin_Controller_User::getInstance()->create(new Tinebase_Model_FullUser(array(
+ 'accountLoginName' => 'redissepp',
+ 'accountPrimaryGroup' => Tinebase_Group::getInstance()->getDefaultGroup()->getId(),
+ 'accountLastName' => 'redis',
+ 'accountFullName' => 'sepp',
+ )), '', '');
+ }
+
+ /**
+ * test redis worker
+ */
+ public function testWorker()
+ {
+ $user = $this->_createUser();
+
+ $worker = new RedisWorker(new Zend_Config(array(
+ 'redis' => $this->_redisConfig
+ )));
+
+ ob_start();
+ $worker->run();
+ $out = ob_get_clean();
+
+ $this->assertEquals('handled create job.', $out);
+ }
+}
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Tine 2.0
+ *
+ * @package RedisWorker
+ * @subpackage Redis
+ * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
+ * @author Philipp Schüle <p.schuele@metaways.de>
+ * @copyright Copyright (c) 2012 Metaways Infosystems GmbH (http://www.metaways.de)
+ */
+
+/**
+ * redis worker
+ *
+ * @package RedisWorker
+ * @subpackage Redis
+ */
+class RedisWorker extends Tinebase_Redis_Worker_Abstract
+{
+ protected function _runCondition()
+ {
+ return ($this->_jobsHandled === 0);
+ }
+
+ public function doJob($job)
+ {
+ echo 'handled ' . $job->action . " job.";
+ }
+}
@@ -44,7 +44,7 @@ public function handleEvent(Tinebase_Event_Abstract $_eventObject)
/**
* implement logic for each controller in this function
-
+ *
* @param Tinebase_Event_Abstract $_eventObject
*/
protected function _handleEvent(Tinebase_Event_Abstract $_eventObject)
Oops, something went wrong.

0 comments on commit 8c85cf9

Please sign in to comment.