I have a PHP daemon running:
while (true) {
$tasks = $this->tasksFinder->findDueTasks();
foreach ($tasks as $task) {
try {
$handlerClass = $task->getHandlerClass();
/** @var AbstractTaskHandler $handler */
$handler = new $handlerClass(...$task->getArguments());
$handler->setEntityManager($this->getEm());
$handler->handle();
$this->getEm()->remove($task);
} catch (Exception $e) {
$this->logger->error("{$e->getMessage()}
{$e->getTraceAsString()}");
$task->setDisabled(true);
$this->getEm()->persist($task);
}
}
$this->getEm()->flush();
sleep(1);
}
Inside of the handler classes (which will be fetched from the daemon) will done some debug work. For example:
$this->getEm()->transactional(function (EntityManager $em) {
$repository = $em->getRepository(UserUnit::class);
/** @var UserUnit $entity */
$entity = $repository->findOneBy(['user' => 1, 'type' => Enum::ORC]);
$em->lock($entity, LockMode::PESSIMISTIC_WRITE);
$entity->increase(1);
});
So the entry orc
for player 1 will be increased by one. Imagine we have already 10 orcs. Creating a task and get handled by the daemon results to 11. Everything is fine, but if I set the entry to 0 manually and create a task without restarting the daemon it will be 11 as well. So it seems the daemon is working with a cache!? Am I right? How to solve that? Clearing the cache?
Call $this->getEm()->clear();
after $this->getEm()->flush();
solved the problem. After calling clear
the objects will be loaded from the database again instead be served by the identity map of Doctrine.