Cleanup events and stuff before processing job in worker

This commit is contained in:
Frederic Guillot 2016-07-31 11:28:33 -04:00
parent 92ac133d23
commit 92a5a0f860
No known key found for this signature in database
GPG Key ID: 92D77191BA7FBC99
5 changed files with 71 additions and 7 deletions

View File

@ -23,7 +23,6 @@ class WorkerCommand extends BaseCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->dispatcher->dispatch('app.bootstrap');
$this->queueManager->listen();
}
}

View File

@ -139,4 +139,20 @@ class ActionManager extends Base
return $this;
}
/**
* Remove all listeners for automated actions
*
* @access public
*/
public function removeEvents()
{
foreach ($this->dispatcher->getListeners() as $eventName => $listeners) {
foreach ($listeners as $listener) {
if (is_array($listener) && $listener[0] instanceof ActionBase) {
$this->dispatcher->removeListener($eventName, $listener);
}
}
}
}
}

View File

@ -43,8 +43,8 @@ class JobHandler extends Base
try {
$className = $payload['class'];
$this->memoryCache->flush();
$this->prepareJobSession($payload['user_id']);
$this->prepareJobEnvironment();
if (DEBUG) {
$this->logger->debug(__METHOD__.' Received job => '.$className.' ('.getmypid().')');
@ -75,4 +75,16 @@ class JobHandler extends Base
$this->userSession->initialize($user);
}
}
/**
* Flush in-memory caching and specific events
*
* @access protected
*/
protected function prepareJobEnvironment()
{
$this->memoryCache->flush();
$this->actionManager->removeEvents();
$this->dispatcher->dispatch('app.bootstrap');
}
}

View File

@ -16,6 +16,11 @@ abstract class Base extends PHPUnit_Framework_TestCase
{
protected $container;
/**
* @var EventDispatcher
*/
protected $dispatcher;
public function setUp()
{
date_default_timezone_set('UTC');
@ -49,6 +54,8 @@ abstract class Base extends PHPUnit_Framework_TestCase
new Stopwatch
);
$this->dispatcher = $this->container['dispatcher'];
$this->container['db']->getStatementHandler()->withLogging();
$this->container['logger'] = new Logger();

View File

@ -96,7 +96,7 @@ class ActionManagerTest extends Base
$actions = $actionManager->getAvailableActions();
$actionManager->attachEvents();
$this->assertEmpty($this->container['dispatcher']->getListeners());
$this->assertEmpty($this->dispatcher->getListeners());
$this->assertEquals(1, $projectModel->create(array('name' =>'test')));
$this->assertEquals(1, $actionModel->create(array(
@ -107,7 +107,7 @@ class ActionManagerTest extends Base
)));
$actionManager->attachEvents();
$listeners = $this->container['dispatcher']->getListeners(TaskModel::EVENT_CREATE);
$listeners = $this->dispatcher->getListeners(TaskModel::EVENT_CREATE);
$this->assertCount(1, $listeners);
$this->assertInstanceOf(get_class($actionTaskAssignColorColumn), $listeners[0][0]);
@ -148,7 +148,7 @@ class ActionManagerTest extends Base
$actionManager->attachEvents();
$listeners = $this->container['dispatcher']->getListeners(TaskModel::EVENT_MOVE_COLUMN);
$listeners = $this->dispatcher->getListeners(TaskModel::EVENT_MOVE_COLUMN);
$this->assertCount(1, $listeners);
$this->assertInstanceOf(get_class($actionTaskAssignColorColumn), $listeners[0][0]);
@ -158,7 +158,6 @@ class ActionManagerTest extends Base
public function testThatEachListenerAreDifferentInstance()
{
$projectModel = new ProjectModel($this->container);
$projectUserRoleModel = new ProjectUserRoleModel($this->container);
$actionModel = new ActionModel($this->container);
$actionTaskAssignColorColumn = new TaskAssignColorColumn($this->container);
$actionManager = new ActionManager($this->container);
@ -183,7 +182,7 @@ class ActionManagerTest extends Base
$actionManager->attachEvents();
$listeners = $this->container['dispatcher']->getListeners(TaskModel::EVENT_MOVE_COLUMN);
$listeners = $this->dispatcher->getListeners(TaskModel::EVENT_MOVE_COLUMN);
$this->assertCount(2, $listeners);
$this->assertFalse($listeners[0][0] === $listeners[1][0]);
@ -193,4 +192,35 @@ class ActionManagerTest extends Base
$this->assertEquals(1, $listeners[1][0]->getParam('column_id'));
$this->assertEquals('red', $listeners[1][0]->getParam('color_id'));
}
public function testRemoveEvents()
{
$projectModel = new ProjectModel($this->container);
$actionModel = new ActionModel($this->container);
$actionTaskAssignColorColumn = new TaskAssignColorColumn($this->container);
$actionManager = new ActionManager($this->container);
$actionManager->register($actionTaskAssignColorColumn);
$actions = $actionManager->getAvailableActions();
$this->assertEquals(1, $projectModel->create(array('name' =>'test')));
$this->assertEquals(1, $actionModel->create(array(
'project_id' => 1,
'event_name' => TaskModel::EVENT_CREATE,
'action_name' => key($actions),
'params' => array('column_id' => 1, 'color_id' => 'red'),
)));
$actionManager->attachEvents();
$this->dispatcher->addListener(TaskModel::EVENT_CREATE, function () {});
$listeners = $this->dispatcher->getListeners(TaskModel::EVENT_CREATE);
$this->assertCount(2, $listeners);
$actionManager->removeEvents();
$listeners = $this->dispatcher->getListeners(TaskModel::EVENT_CREATE);
$this->assertCount(1, $listeners);
$this->assertNotInstanceOf(get_class($actionTaskAssignColorColumn), $listeners[0]);
}
}