Skip to content

Commit

Permalink
Merge pull request #367 from tienvx/fix-cant-finish-reduce-bug
Browse files Browse the repository at this point in the history
Fix can't finish reduce bug
  • Loading branch information
tienvx committed Aug 20, 2019
2 parents f0fa085 + 3d174f5 commit 51ce4e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/Command/ReduceBugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Tienvx\Bundle\MbtBundle\Command;

use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Registry;
use Throwable;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Helper\WorkflowHelper;
Expand All @@ -24,7 +26,7 @@ class ReduceBugCommand extends AbstractCommand
private $reducerManager;

/**
* @var EntityManagerInterface
* @var EntityManager
*/
private $entityManager;

Expand Down Expand Up @@ -68,6 +70,7 @@ protected function configure()
* @param OutputInterface $output
*
* @throws Exception
* @throws Throwable
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -93,11 +96,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$messagesCount = $reducer->dispatch($bug);
if (0 === $messagesCount && 0 === $bug->getMessagesCount()) {
$this->messageBus->dispatch(new FinishReduceBugMessage($bug->getId()));
} else {
} elseif ($messagesCount > 0) {
$callback = function () use ($bug, $messagesCount) {
$this->entityManager->lock($bug, LockMode::PESSIMISTIC_WRITE);
// Reload the bug for the newest messages count.
$bug = $this->entityManager->find(Bug::class, $bug->getId(), LockMode::PESSIMISTIC_WRITE);

$bug->setMessagesCount($bug->getMessagesCount() + $messagesCount);
if ($bug instanceof Bug) {
$bug->setMessagesCount($bug->getMessagesCount() + $messagesCount);
}
};

$this->entityManager->transactional($callback);
Expand Down
13 changes: 9 additions & 4 deletions src/Reducer/AbstractReducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tienvx\Bundle\MbtBundle\Reducer;

use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Psr\Cache\InvalidArgumentException;
Expand Down Expand Up @@ -42,7 +43,7 @@ abstract class AbstractReducer implements ReducerInterface
protected $graphBuilder;

/**
* @var EntityManagerInterface
* @var EntityManager
*/
private $entityManager;

Expand Down Expand Up @@ -109,10 +110,14 @@ public function handle(Bug $bug, Workflow $workflow, int $length, int $from, int
*/
protected function updateSteps(Bug $bug, Steps $newSteps)
{
$callback = function () use ($bug, $newSteps) {
$this->entityManager->lock($bug, LockMode::PESSIMISTIC_WRITE);
$length = $bug->getSteps()->getLength();
$callback = function () use ($bug, $newSteps, $length) {
// Reload the bug for the newest messages length.
$bug = $this->entityManager->find(Bug::class, $bug->getId(), LockMode::PESSIMISTIC_WRITE);

$bug->setSteps($newSteps);
if ($bug instanceof Bug && $length === $bug->getSteps()->getLength()) {
$bug->setSteps($newSteps);
}
};

$this->entityManager->transactional($callback);
Expand Down

0 comments on commit 51ce4e2

Please sign in to comment.