Skip to content

Removed flush of repositories #27

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

Merged
merged 1 commit into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Task\TaskBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -32,17 +33,28 @@ class RunCommand extends Command
*/
private $scheduler;

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

/**
* @param string $name
* @param TaskRunnerInterface $runner
* @param TaskSchedulerInterface $scheduler
* @param EntityManagerInterface $entityManager
*/
public function __construct($name, TaskRunnerInterface $runner, TaskSchedulerInterface $scheduler)
{
public function __construct(
$name,
TaskRunnerInterface $runner,
TaskSchedulerInterface $scheduler,
EntityManagerInterface $entityManager = null
) {
parent::__construct($name);

$this->runner = $runner;
$this->scheduler = $scheduler;
$this->entityManager = $entityManager;
}

/**
Expand All @@ -60,5 +72,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->runner->runTasks();
$this->scheduler->scheduleTasks();

if ($this->entityManager) {
$this->entityManager->flush();
}
}
}
14 changes: 13 additions & 1 deletion src/Command/ScheduleTaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Task\TaskBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -28,15 +29,22 @@ class ScheduleTaskCommand extends Command
*/
private $scheduler;

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

/**
* @param string $name
* @param TaskSchedulerInterface $runner
* @param EntityManagerInterface $entityManager
*/
public function __construct($name, TaskSchedulerInterface $runner)
public function __construct($name, TaskSchedulerInterface $runner, EntityManagerInterface $entityManager = null)
{
parent::__construct($name);

$this->scheduler = $runner;
$this->entityManager = $entityManager;
}

/**
Expand Down Expand Up @@ -84,5 +92,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$taskBuilder->schedule();

if ($this->entityManager) {
$this->entityManager->flush();
}
}
}
9 changes: 9 additions & 0 deletions src/DependencyInjection/TaskExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
Expand All @@ -38,5 +39,13 @@ public function load(array $configs, ContainerBuilder $container)
if ($config['run']['mode'] === 'listener') {
$loader->load('listener.xml');
}

if ('doctrine' === $config['storage']) {
// FIXME move to compiler pass
$container->getDefinition('task.command.schedule_task')
->addArgument(new Reference('doctrine.orm.entity_manager'));
$container->getDefinition('task.command.run')
->addArgument(new Reference('doctrine.orm.entity_manager'));
}
}
}
12 changes: 1 addition & 11 deletions src/Entity/TaskExecutionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function create(TaskInterface $task, \DateTime $scheduleTime)
/**
* {@inheritdoc}
*/
public function persist(TaskExecutionInterface $execution)
public function save(TaskExecutionInterface $execution)
{
$this->_em->persist($execution);

Expand All @@ -51,16 +51,6 @@ public function remove(TaskExecutionInterface $execution)
return $this;
}

/**
* {@inheritdoc}
*/
public function flush()
{
$this->_em->flush();

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
12 changes: 1 addition & 11 deletions src/Entity/TaskRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function findByUuid($uuid)
/**
* {@inheritdoc}
*/
public function persist(TaskInterface $task)
public function save(TaskInterface $task)
{
$this->_em->persist($task);

Expand All @@ -56,16 +56,6 @@ public function remove(TaskInterface $task)
return $this;
}

/**
* {@inheritdoc}
*/
public function flush()
{
$this->_em->flush();

return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/command.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<argument type="string">task:run</argument>
<argument type="service" id="task.runner"/>
<argument type="service" id="task.scheduler"/>
<!-- add entity_manager if doctrine storage is enabled -->

<tag name="console.command"/>
</service>
Expand All @@ -21,6 +22,7 @@
<service id="task.command.schedule_task" class="Task\TaskBundle\Command\ScheduleTaskCommand">
<argument type="string">task:schedule</argument>
<argument type="service" id="task.scheduler"/>
<!-- add entity_manager if doctrine storage is enabled -->

<tag name="console.command"/>
</service>
Expand Down
8 changes: 4 additions & 4 deletions tests/Functional/BaseCommandTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ protected function createTask($workload, CronExpression $cronExpression = null,
if ($cronExpression) {
$task->setInterval($cronExpression, new \DateTime(), new \DateTime('+1 year'));
}
$this->taskRepository->persist($task);
$this->taskRepository->flush();
$this->taskRepository->save($task);

return $task;
}
Expand All @@ -123,8 +122,9 @@ protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleT
{
$execution = $this->taskExecutionRepository->create($task, $scheduleTime);
$execution->setStatus($status);
$this->taskExecutionRepository->persist($execution);
$this->taskExecutionRepository->flush();
$this->taskExecutionRepository->save($execution);

$this->getEntityManager()->flush();

return $execution;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Command/DebugTasksCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testExecute()
$executions[1]->setResult(strrev($executions[1]->getWorkload()));
$executions[1]->setDuration(0.0001);

$this->taskExecutionRepository->flush();
$this->getEntityManager()->flush();

$this->commandTester->execute(
[
Expand All @@ -62,7 +62,7 @@ public function testExecutePaginated()
$this->createTaskExecution($task, new \DateTime('+1 hour')),
];

$this->taskExecutionRepository->flush();
$this->getEntityManager()->flush();

$this->commandTester->execute(
[
Expand Down