Skip to content

Commit dbcc770

Browse files
added task-execution listener to clear entity-manager
1 parent 963f20a commit dbcc770

File tree

5 files changed

+84
-1
lines changed

5 files changed

+84
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "~5.5 || ~7.0",
14-
"php-task/php-task": "dev-master",
14+
"php-task/php-task": "dev-bugfix/event-after",
1515
"symfony/http-kernel": "^2.6 || ^3.0",
1616
"symfony/dependency-injection": "^2.6 || ^3.0",
1717
"symfony/config": "^2.6 || ^3.0",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-task library.
5+
*
6+
* (c) php-task
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Task\TaskBundle\EventListener;
13+
14+
use Doctrine\ORM\EntityManagerInterface;
15+
use Task\Event\TaskExecutionEvent;
16+
17+
/**
18+
* Listens on task-execution events.
19+
*/
20+
class TaskExecutionListener
21+
{
22+
/**
23+
* @var EntityManagerInterface
24+
*/
25+
private $entityManager;
26+
27+
/**
28+
* @param EntityManagerInterface $entityManager
29+
*/
30+
public function __construct(EntityManagerInterface $entityManager)
31+
{
32+
$this->entityManager = $entityManager;
33+
}
34+
35+
/**
36+
* This method clears the entity-manager after each task to ensure clean state before next task.
37+
*
38+
* @param TaskExecutionEvent $event
39+
*/
40+
public function clearEntityManagerAfterTask(TaskExecutionEvent $event)
41+
{
42+
$this->entityManager->clear();
43+
}
44+
}

src/Resources/config/storage/doctrine.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
<argument type="string">TaskBundle:TaskExecution</argument>
1717
</service>
1818
<service id="task.storage.task_execution" alias="task.repository.task_execution"/>
19+
20+
<service id="task.event_listener.execution" class="Task\TaskBundle\EventListener\TaskExecutionListener">
21+
<argument type="service" id="doctrine.orm.entity_manager"/>
22+
23+
<tag name="%task.events.after%" method="clearEntityManagerAfterTask"/>
24+
</service>
1925
</services>
2026
</container>

src/Resources/config/task_event_listener.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<parameter key="task.events.create" type="constant">Task\Event\Events::TASK_CREATE</parameter>
88
<parameter key="task.events.create_execution" type="constant">Task\Event\Events::TASK_EXECUTION_CREATE</parameter>
99
<parameter key="task.events.before" type="constant">Task\Event\Events::TASK_BEFORE</parameter>
10+
<parameter key="task.events.after" type="constant">Task\Event\Events::TASK_AFTER</parameter>
1011
<parameter key="task.events.finished" type="constant">Task\Event\Events::TASK_FINISHED</parameter>
1112
<parameter key="task.events.passed" type="constant">Task\Event\Events::TASK_PASSED</parameter>
1213
<parameter key="task.events.failed" type="constant">Task\Event\Events::TASK_FAILED</parameter>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-task library.
5+
*
6+
* (c) php-task
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Unit\EventListener;
13+
14+
use Doctrine\ORM\EntityManagerInterface;
15+
use Task\Event\TaskExecutionEvent;
16+
use Task\TaskBundle\EventListener\TaskExecutionListener;
17+
18+
/**
19+
* Tests for class TaskExecutionListener.
20+
*/
21+
class TaskExecutionListenerTest extends \PHPUnit_Framework_TestCase
22+
{
23+
public function testClearEntityManagerAfterTask()
24+
{
25+
$entityManager = $this->prophesize(EntityManagerInterface::class);
26+
27+
$listener = new TaskExecutionListener($entityManager->reveal());
28+
$listener->clearEntityManagerAfterTask($this->prophesize(TaskExecutionEvent::class)->reveal());
29+
30+
$entityManager->clear()->shouldBeCalled();
31+
}
32+
}

0 commit comments

Comments
 (0)