Skip to content

Commit 5696ca2

Browse files
added remove system-task
1 parent 08c10c7 commit 5696ca2

File tree

4 files changed

+114
-12
lines changed

4 files changed

+114
-12
lines changed

src/Command/ScheduleSystemTasksCommand.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
9898
}
9999
}
100100

101+
foreach ($this->taskRepository->findSystemTasks() as $task) {
102+
if (!in_array($task->getSystemKey(), array_keys($this->systemTasks))) {
103+
$this->disableTask($task);
104+
}
105+
}
106+
101107
$output->writeln('');
102108
$output->writeln('System-tasks successfully scheduled');
103109
}
@@ -112,7 +118,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
112118
private function processSystemTask($systemKey, array $systemTask, OutputInterface $output)
113119
{
114120
if (!$systemTask['enabled']) {
115-
$this->disableTask($systemKey);
121+
$this->disableSystemTask($systemKey);
116122

117123
$output->writeln(sprintf(' * System-task "%s" was <info>disabled</info>.', $systemKey));
118124

@@ -144,12 +150,22 @@ private function processSystemTask($systemKey, array $systemTask, OutputInterfac
144150
*
145151
* @param string $systemKey
146152
*/
147-
private function disableTask($systemKey)
153+
private function disableSystemTask($systemKey)
148154
{
149155
if (!$task = $this->taskRepository->findBySystemKey($systemKey)) {
150156
return;
151157
}
152158

159+
$this->disableTask($task);
160+
}
161+
162+
/**
163+
* Disable given task identified.
164+
*
165+
* @param TaskInterface $task
166+
*/
167+
public function disableTask(TaskInterface $task)
168+
{
153169
$task->setInterval($task->getInterval(), $task->getFirstExecution(), new \DateTime());
154170
$this->abortPending($task);
155171
}

src/Entity/TaskRepository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,17 @@ public function findBySystemKey($systemKey)
118118
return;
119119
}
120120
}
121+
122+
/**
123+
* Returns all system-task.
124+
*
125+
* @return TaskInterface[]
126+
*/
127+
public function findSystemTasks()
128+
{
129+
return $this->createQueryBuilder('t')
130+
->where('t.systemKey IS NOT NULL')
131+
->getQuery()
132+
->getResult();
133+
}
121134
}

tests/Functional/Entity/TaskRepositoryTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function setUp()
1919
$this->taskRepository = self::$kernel->getContainer()->get('task.storage.task');
2020
}
2121

22-
public function testFinBySystemKey()
22+
public function testFindBySystemKey()
2323
{
2424
if (self::$kernel->getContainer()->getParameter('kernel.storage') !== 'doctrine') {
2525
return $this->markTestSkipped('This testcase will only be called for doctrine storage.');
@@ -34,7 +34,7 @@ public function testFinBySystemKey()
3434
$this->assertEquals($task->getUuid(), $result->getUuid());
3535
}
3636

37-
public function testFinBySystemKeyNotFound()
37+
public function testFindBySystemKeyNotFound()
3838
{
3939
if (self::$kernel->getContainer()->getParameter('kernel.storage') !== 'doctrine') {
4040
return $this->markTestSkipped('This testcase will only be called for doctrine storage.');
@@ -45,4 +45,22 @@ public function testFinBySystemKeyNotFound()
4545

4646
$this->assertNull($this->taskRepository->findBySystemKey('test'));
4747
}
48+
49+
public function testFindSystemTasks()
50+
{
51+
if (self::$kernel->getContainer()->getParameter('kernel.storage') !== 'doctrine') {
52+
return $this->markTestSkipped('This testcase will only be called for doctrine storage.');
53+
}
54+
55+
$task1 = $this->createTask();
56+
$task1->setSystemKey('test');
57+
$this->taskRepository->save($task1);
58+
59+
$task2 = $this->createTask();
60+
$this->taskRepository->save($task2);
61+
62+
$result = $this->taskRepository->findSystemTasks();
63+
$this->assertCount(1, $result);
64+
$this->assertEquals($task1->getUuid(), $result[0]->getUuid());
65+
}
4866
}

tests/Unit/Command/ScheduleSystemTasksCommandTest.php

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Task\Storage\TaskExecutionRepositoryInterface;
1212
use Task\TaskBundle\Builder\TaskBuilder;
1313
use Task\TaskBundle\Command\ScheduleSystemTasksCommand;
14+
use Task\TaskBundle\Entity\Task;
1415
use Task\TaskBundle\Entity\TaskRepository;
1516
use Task\TaskBundle\Tests\Functional\TestHandler;
16-
use Task\TaskInterface;
1717
use Task\TaskStatus;
1818

1919
class ScheduleSystemTasksCommandTest extends \PHPUnit_Framework_TestCase
@@ -69,9 +69,14 @@ public function testExecute()
6969
]
7070
);
7171

72+
$task = $this->prophesize(Task::class);
73+
$task->getSystemKey()->willReturn('testing');
74+
7275
$taskBuilder = $this->prophesize(TaskBuilder::class);
7376

7477
$this->taskRepository->findBySystemKey('testing')->willReturn(null);
78+
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
79+
7580
$this->scheduler->createTask(TestHandler::class, 'test')->shouldBeCalled()->willReturn($taskBuilder->reveal());
7681

7782
$taskBuilder->setSystemKey('testing')->shouldBeCalled();
@@ -103,17 +108,27 @@ public function testExecuteMultiple()
103108
]
104109
);
105110

111+
$task1 = $this->prophesize(Task::class);
112+
$task1->getSystemKey()->willReturn('testing-1');
113+
$task2 = $this->prophesize(Task::class);
114+
$task2->getSystemKey()->willReturn('testing-2');
115+
106116
$this->taskRepository->findBySystemKey('testing-1')->willReturn(null);
107117
$this->taskRepository->findBySystemKey('testing-2')->willReturn(null);
118+
$this->taskRepository->findSystemTasks()->willReturn([$task1->reveal(), $task2->reveal()]);
108119

109120
$taskBuilder1 = $this->prophesize(TaskBuilder::class);
110-
$this->scheduler->createTask(TestHandler::class, 'test-1')->shouldBeCalled()->willReturn($taskBuilder1->reveal());
121+
$this->scheduler->createTask(TestHandler::class, 'test-1')->shouldBeCalled()->willReturn(
122+
$taskBuilder1->reveal()
123+
);
111124
$taskBuilder1->setSystemKey('testing-1')->shouldBeCalled();
112125
$taskBuilder1->cron('* * * * *')->shouldBeCalled();
113126
$taskBuilder1->schedule()->shouldBeCalled();
114127

115128
$taskBuilder2 = $this->prophesize(TaskBuilder::class);
116-
$this->scheduler->createTask(TestHandler::class, 'test-2')->shouldBeCalled()->willReturn($taskBuilder2->reveal());
129+
$this->scheduler->createTask(TestHandler::class, 'test-2')->shouldBeCalled()->willReturn(
130+
$taskBuilder2->reveal()
131+
);
117132
$taskBuilder2->setSystemKey('testing-2')->shouldBeCalled();
118133
$taskBuilder2->cron('* * * * *')->shouldBeCalled();
119134
$taskBuilder2->schedule()->shouldBeCalled();
@@ -137,9 +152,10 @@ public function testExecuteDisable()
137152
]
138153
);
139154

140-
$task = $this->prophesize(TaskInterface::class);
155+
$task = $this->prophesize(Task::class);
141156
$task->getInterval()->willReturn(CronExpression::factory('* * * * *'));
142157
$task->getFirstExecution()->willReturn(new \DateTime());
158+
$task->getSystemKey()->willReturn('testing');
143159

144160
$task->setInterval(
145161
$task->reveal()->getInterval(),
@@ -152,6 +168,7 @@ function ($date) {
152168
)->shouldBeCalled();
153169

154170
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
171+
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
155172

156173
$execution = $this->prophesize(TaskExecutionInterface::class);
157174
$execution->setStatus(TaskStatus::ABORTED);
@@ -178,16 +195,18 @@ public function testExecuteUpdate()
178195
]
179196
);
180197

181-
$task = $this->prophesize(TaskInterface::class);
198+
$task = $this->prophesize(Task::class);
199+
$task->getSystemKey()->willReturn('testing');
182200
$task->getHandlerClass()->willReturn(TestHandler::class);
183201
$task->getWorkload()->willReturn('test');
184202
$task->getInterval()->willReturn(CronExpression::factory('@daily'));
185203
$task->getFirstExecution()->willReturn(new \DateTime());
186204

187-
$task->setInterval(CronExpression::factory('* * * * *'), $task->reveal()->getFirstExecution())
188-
->shouldBeCalled();
205+
$task->setInterval(CronExpression::factory('* * * * *'), $task->reveal()->getFirstExecution())->shouldBeCalled(
206+
);
189207

190208
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
209+
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
191210

192211
$execution = $this->prophesize(TaskExecutionInterface::class);
193212
$execution->setStatus(TaskStatus::ABORTED);
@@ -216,7 +235,8 @@ public function testExecuteUpdateNotSupported()
216235
]
217236
);
218237

219-
$task = $this->prophesize(TaskInterface::class);
238+
$task = $this->prophesize(Task::class);
239+
$task->getSystemKey()->willReturn('testing');
220240
$task->getHandlerClass()->willReturn('not-existing');
221241
$task->getWorkload()->willReturn('new-workload');
222242
$task->getInterval()->willReturn(CronExpression::factory('@daily'));
@@ -225,6 +245,7 @@ public function testExecuteUpdateNotSupported()
225245
$task->setInterval(Argument::cetera())->shouldNotBeCalled();
226246

227247
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
248+
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
228249

229250
$this->taskExecutionRepository->save(Argument::cetera())->shouldNotBeCalled();
230251

@@ -235,4 +256,38 @@ public function testExecuteUpdateNotSupported()
235256
$this->prophesize(OutputInterface::class)->reveal()
236257
);
237258
}
259+
260+
public function testExecuteRemove()
261+
{
262+
$command = $this->createCommand([]);
263+
264+
$task = $this->prophesize(Task::class);
265+
$task->getInterval()->willReturn(CronExpression::factory('* * * * *'));
266+
$task->getFirstExecution()->willReturn(new \DateTime());
267+
$task->getSystemKey()->willReturn('testing');
268+
269+
$task->setInterval(
270+
$task->reveal()->getInterval(),
271+
$task->reveal()->getFirstExecution(),
272+
Argument::that(
273+
function ($date) {
274+
return $date <= new \DateTime('+1 Minute');
275+
}
276+
)
277+
)->shouldBeCalled();
278+
279+
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
280+
$this->taskRepository->findSystemTasks()->willReturn([$task->reveal()]);
281+
282+
$execution = $this->prophesize(TaskExecutionInterface::class);
283+
$execution->setStatus(TaskStatus::ABORTED);
284+
285+
$this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal());
286+
$this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled();
287+
288+
$command->run(
289+
$this->prophesize(InputInterface::class)->reveal(),
290+
$this->prophesize(OutputInterface::class)->reveal()
291+
);
292+
}
238293
}

0 commit comments

Comments
 (0)