-
Notifications
You must be signed in to change notification settings - Fork 8
Clear entity-manager after each task to ensure clean environment #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
963f20a
266b2b5
ca59767
31eaf6e
28de4c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,22 @@ public function findPending(TaskInterface $task) | |
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function findByUuid($uuid) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same opinion as @wachterjohannes. This way it will be easier to optimize if there come up performance problem at some time. |
||
{ | ||
try { | ||
return $this->createQueryBuilder('e') | ||
->where('e.uuid = :uuid') | ||
->setParameter('uuid', $uuid) | ||
->getQuery() | ||
->getSingleResult(); | ||
} catch (NoResultException $e) { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-task library. | ||
* | ||
* (c) php-task | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Task\TaskBundle\EventListener; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Task\Event\TaskExecutionEvent; | ||
|
||
/** | ||
* Listens on task-execution events. | ||
*/ | ||
class DoctrineTaskExecutionListener | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to mention that again, but I missed it the last time... Wouldn't it be better to make it a subscriber? Then it could handle all the doctrine specific settings, which might be added later. Otherwise this would have to be renamed to something to include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also like this we could handle everything for doctrine - i dont see the difference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not if you have to do some doctrine specific tasks on different events. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thats configuration you can use multiple tags for different events. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, that's true, but I think it is a little bit hidden in the configuration... And since this class is already bound to an event I would also not fear implementing the |
||
{ | ||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $entityManager; | ||
|
||
/** | ||
* @param EntityManagerInterface $entityManager | ||
*/ | ||
public function __construct(EntityManagerInterface $entityManager = null) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
/** | ||
* This method clears the entity-manager after each task to ensure clean state before next task. | ||
* | ||
* @param TaskExecutionEvent $event | ||
*/ | ||
public function clearEntityManagerAfterTask(TaskExecutionEvent $event) | ||
{ | ||
if (!$this->entityManager) { | ||
return; | ||
} | ||
|
||
$this->entityManager->clear(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-task library. | ||
* | ||
* (c) php-task | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Task\TaskBundle\Tests\Unit\EventListener; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Task\Event\TaskExecutionEvent; | ||
use Task\TaskBundle\EventListener\DoctrineTaskExecutionListener; | ||
|
||
/** | ||
* Tests for class TaskExecutionListener. | ||
*/ | ||
class TaskExecutionListenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testClearEntityManagerAfterTask() | ||
{ | ||
$entityManager = $this->prophesize(EntityManagerInterface::class); | ||
|
||
$listener = new DoctrineTaskExecutionListener($entityManager->reveal()); | ||
$listener->clearEntityManagerAfterTask($this->prophesize(TaskExecutionEvent::class)->reveal()); | ||
|
||
$entityManager->clear()->shouldBeCalled(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couldnt you use the doctrine internal
findOneByUuid
function?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh i dont like this "magic" internal methods.