Skip to content

Commit

Permalink
Made sure not to start tasks that are already running
Browse files Browse the repository at this point in the history
Signed-off-by: Emil Kilhage <emilkilhage@gmail.com>
  • Loading branch information
kilhage committed Mar 15, 2017
1 parent f3167a8 commit 682c423
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/Entity/QueuedTaskRepository.php
Expand Up @@ -3,6 +3,7 @@
namespace Glooby\TaskBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;
use Glooby\TaskBundle\Model\QueuedTaskInterface;

/**
Expand Down Expand Up @@ -61,4 +62,27 @@ public function findQueued($limit)
->useQueryCache(true)
->getResult();
}

/**
* @param string $name
* @return bool
*/
public function isRunning($name)
{
try {
$this->getEntityManager()
->createQuery('SELECT r
FROM GloobyTaskBundle:QueuedTask r
WHERE r.name = :name AND r.status = :status')
->setParameter('name', $name)
->setParameter('status', QueuedTaskInterface::STATUS_RUNNING)
->useQueryCache(true)
->setMaxResults(1)
->getSingleResult();

return true;
} catch (NoResultException $e) {
return false;
}
}
}
4 changes: 3 additions & 1 deletion src/Queue/QueueProcessor.php
Expand Up @@ -78,7 +78,9 @@ public function process()
->getRepository('GloobyTaskBundle:QueuedTask');

foreach ($queueRepo->findQueued($this->limit) as $queuedTask) {
$this->start($queuedTask);
if (!$queueRepo->isRunning($queuedTask->getName())) {
$this->start($queuedTask);
}
}

$this->wait();
Expand Down

0 comments on commit 682c423

Please sign in to comment.