Skip to content

Commit ee1defd

Browse files
removed flush of repositories (#27)
1 parent e2c3260 commit ee1defd

File tree

8 files changed

+50
-31
lines changed

8 files changed

+50
-31
lines changed

src/Command/RunCommand.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Task\TaskBundle\Command;
1313

14+
use Doctrine\ORM\EntityManagerInterface;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
@@ -32,17 +33,28 @@ class RunCommand extends Command
3233
*/
3334
private $scheduler;
3435

36+
/**
37+
* @var EntityManagerInterface
38+
*/
39+
private $entityManager;
40+
3541
/**
3642
* @param string $name
3743
* @param TaskRunnerInterface $runner
3844
* @param TaskSchedulerInterface $scheduler
45+
* @param EntityManagerInterface $entityManager
3946
*/
40-
public function __construct($name, TaskRunnerInterface $runner, TaskSchedulerInterface $scheduler)
41-
{
47+
public function __construct(
48+
$name,
49+
TaskRunnerInterface $runner,
50+
TaskSchedulerInterface $scheduler,
51+
EntityManagerInterface $entityManager = null
52+
) {
4253
parent::__construct($name);
4354

4455
$this->runner = $runner;
4556
$this->scheduler = $scheduler;
57+
$this->entityManager = $entityManager;
4658
}
4759

4860
/**
@@ -60,5 +72,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
6072
{
6173
$this->runner->runTasks();
6274
$this->scheduler->scheduleTasks();
75+
76+
if ($this->entityManager) {
77+
$this->entityManager->flush();
78+
}
6379
}
6480
}

src/Command/ScheduleTaskCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Task\TaskBundle\Command;
1313

14+
use Doctrine\ORM\EntityManagerInterface;
1415
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
@@ -28,15 +29,22 @@ class ScheduleTaskCommand extends Command
2829
*/
2930
private $scheduler;
3031

32+
/**
33+
* @var EntityManagerInterface
34+
*/
35+
private $entityManager;
36+
3137
/**
3238
* @param string $name
3339
* @param TaskSchedulerInterface $runner
40+
* @param EntityManagerInterface $entityManager
3441
*/
35-
public function __construct($name, TaskSchedulerInterface $runner)
42+
public function __construct($name, TaskSchedulerInterface $runner, EntityManagerInterface $entityManager = null)
3643
{
3744
parent::__construct($name);
3845

3946
$this->scheduler = $runner;
47+
$this->entityManager = $entityManager;
4048
}
4149

4250
/**
@@ -84,5 +92,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
8492
}
8593

8694
$taskBuilder->schedule();
95+
96+
if ($this->entityManager) {
97+
$this->entityManager->flush();
98+
}
8799
}
88100
}

src/DependencyInjection/TaskExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Loader;
17+
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1819

1920
/**
@@ -38,5 +39,13 @@ public function load(array $configs, ContainerBuilder $container)
3839
if ($config['run']['mode'] === 'listener') {
3940
$loader->load('listener.xml');
4041
}
42+
43+
if ('doctrine' === $config['storage']) {
44+
// FIXME move to compiler pass
45+
$container->getDefinition('task.command.schedule_task')
46+
->addArgument(new Reference('doctrine.orm.entity_manager'));
47+
$container->getDefinition('task.command.run')
48+
->addArgument(new Reference('doctrine.orm.entity_manager'));
49+
}
4150
}
4251
}

src/Entity/TaskExecutionRepository.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function create(TaskInterface $task, \DateTime $scheduleTime)
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function persist(TaskExecutionInterface $execution)
37+
public function save(TaskExecutionInterface $execution)
3838
{
3939
$this->_em->persist($execution);
4040

@@ -51,16 +51,6 @@ public function remove(TaskExecutionInterface $execution)
5151
return $this;
5252
}
5353

54-
/**
55-
* {@inheritdoc}
56-
*/
57-
public function flush()
58-
{
59-
$this->_em->flush();
60-
61-
return $this;
62-
}
63-
6454
/**
6555
* {@inheritdoc}
6656
*/

src/Entity/TaskRepository.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function findByUuid($uuid)
3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function persist(TaskInterface $task)
42+
public function save(TaskInterface $task)
4343
{
4444
$this->_em->persist($task);
4545

@@ -56,16 +56,6 @@ public function remove(TaskInterface $task)
5656
return $this;
5757
}
5858

59-
/**
60-
* {@inheritdoc}
61-
*/
62-
public function flush()
63-
{
64-
$this->_em->flush();
65-
66-
return $this;
67-
}
68-
6959
/**
7060
* {@inheritdoc}
7161
*/

src/Resources/config/command.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<argument type="string">task:run</argument>
88
<argument type="service" id="task.runner"/>
99
<argument type="service" id="task.scheduler"/>
10+
<!-- add entity_manager if doctrine storage is enabled -->
1011

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

2527
<tag name="console.command"/>
2628
</service>

tests/Functional/BaseCommandTestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ protected function createTask($workload, CronExpression $cronExpression = null,
104104
if ($cronExpression) {
105105
$task->setInterval($cronExpression, new \DateTime(), new \DateTime('+1 year'));
106106
}
107-
$this->taskRepository->persist($task);
108-
$this->taskRepository->flush();
107+
$this->taskRepository->save($task);
109108

110109
return $task;
111110
}
@@ -123,8 +122,9 @@ protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleT
123122
{
124123
$execution = $this->taskExecutionRepository->create($task, $scheduleTime);
125124
$execution->setStatus($status);
126-
$this->taskExecutionRepository->persist($execution);
127-
$this->taskExecutionRepository->flush();
125+
$this->taskExecutionRepository->save($execution);
126+
127+
$this->getEntityManager()->flush();
128128

129129
return $execution;
130130
}

tests/Functional/Command/DebugTasksCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testExecute()
3535
$executions[1]->setResult(strrev($executions[1]->getWorkload()));
3636
$executions[1]->setDuration(0.0001);
3737

38-
$this->taskExecutionRepository->flush();
38+
$this->getEntityManager()->flush();
3939

4040
$this->commandTester->execute(
4141
[
@@ -62,7 +62,7 @@ public function testExecutePaginated()
6262
$this->createTaskExecution($task, new \DateTime('+1 hour')),
6363
];
6464

65-
$this->taskExecutionRepository->flush();
65+
$this->getEntityManager()->flush();
6666

6767
$this->commandTester->execute(
6868
[

0 commit comments

Comments
 (0)