Skip to content

Commit 91b0c04

Browse files
changed remove execution to abort execution
1 parent 1f80722 commit 91b0c04

File tree

5 files changed

+129
-24
lines changed

5 files changed

+129
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
],
1212
"require": {
1313
"php": "~5.5 || ~7.0",
14-
"php-task/php-task": "^1.0",
14+
"php-task/php-task": "dev-feature/aborted-execution",
1515
"symfony/http-kernel": "^2.6 || ^3.0",
1616
"symfony/dependency-injection": "^2.6 || ^3.0",
1717
"symfony/config": "^2.6 || ^3.0",

src/Builder/TaskBuilder.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@
2222
*/
2323
class TaskBuilder extends BaseTaskBuilder
2424
{
25-
/**
26-
* @var TaskInterface
27-
*/
28-
private $task;
29-
30-
/**
31-
* @param TaskInterface $task
32-
* @param TaskSchedulerInterface $taskScheduler
33-
*/
34-
public function __construct(TaskInterface $task, TaskSchedulerInterface $taskScheduler)
35-
{
36-
parent::__construct($task, $taskScheduler);
37-
38-
$this->task = $task;
39-
}
40-
4125
/**
4226
* Set system-key.
4327
*

src/Command/ScheduleSystemTasksCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Task\TaskBundle\Builder\TaskBuilder;
1212
use Task\TaskBundle\Entity\TaskRepository;
1313
use Task\TaskInterface;
14+
use Task\TaskStatus;
1415

1516
/**
1617
* Schedules configured system-tasks.
@@ -151,7 +152,7 @@ private function disableTask($systemKey)
151152
}
152153

153154
$task->setInterval($task->getInterval(), $task->getFirstExecution(), new \DateTime());
154-
$this->removePending($task);
155+
$this->abortPending($task);
155156
}
156157

157158
/**
@@ -177,21 +178,22 @@ private function updateTask($systemKey, array $systemTask, TaskInterface $task)
177178

178179
$task->setInterval(CronExpression::factory($systemTask['cron_expression']), $task->getFirstExecution());
179180

180-
$this->removePending($task);
181+
$this->abortPending($task);
181182
$this->scheduler->scheduleTasks();
182183
}
183184

184185
/**
185-
* Remove pending execution for given task.
186+
* Abort pending execution for given task.
186187
*
187188
* @param TaskInterface $task
188189
*/
189-
private function removePending(TaskInterface $task)
190+
private function abortPending(TaskInterface $task)
190191
{
191192
if (!$execution = $this->taskExecutionRepository->findPending($task)) {
192193
return;
193194
}
194195

195-
$this->taskExecutionRepository->remove($execution);
196+
$execution->setStatus(TaskStatus::ABORTED);
197+
$this->taskExecutionRepository->save($execution);
196198
}
197199
}

tests/Functional/Command/ScheduleSystemTasksCommandTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77

88
class ScheduleSystemTasksCommandTest extends BaseCommandTestCase
99
{
10-
public function testExecute()
10+
public function setUp()
1111
{
12+
self::bootKernel();
1213
if (self::$kernel->getContainer()->getParameter('kernel.storage') !== 'doctrine') {
1314
return $this->markTestSkipped('This testcase will only be called for doctrine storage.');
1415
}
16+
}
1517

18+
public function testExecute()
19+
{
1620
$this->commandTester->execute(
1721
[
1822
'command' => $this->command->getName(),

tests/Unit/Command/ScheduleSystemTasksCommandTest.php

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22

33
namespace Task\TaskBundle\Unit\Command;
44

5+
use Cron\CronExpression;
6+
use Prophecy\Argument;
57
use Symfony\Component\Console\Input\InputInterface;
68
use Symfony\Component\Console\Output\OutputInterface;
9+
use Task\Execution\TaskExecutionInterface;
710
use Task\Scheduler\TaskSchedulerInterface;
811
use Task\Storage\TaskExecutionRepositoryInterface;
912
use Task\TaskBundle\Builder\TaskBuilder;
1013
use Task\TaskBundle\Command\ScheduleSystemTasksCommand;
1114
use Task\TaskBundle\Entity\TaskRepository;
1215
use Task\TaskBundle\Tests\Functional\TestHandler;
16+
use Task\TaskInterface;
17+
use Task\TaskStatus;
1318

1419
class ScheduleSystemTasksCommandTest extends \PHPUnit_Framework_TestCase
1520
{
@@ -119,5 +124,115 @@ public function testExecuteMultiple()
119124
);
120125
}
121126

122-
// TODO Tests for update and disable.
127+
public function testExecuteDisable()
128+
{
129+
$command = $this->createCommand(
130+
[
131+
'testing' => [
132+
'enabled' => false,
133+
'handler_class' => TestHandler::class,
134+
'workload' => 'test',
135+
'cron_expression' => '* * * * *',
136+
],
137+
]
138+
);
139+
140+
$task = $this->prophesize(TaskInterface::class);
141+
$task->getInterval()->willReturn(CronExpression::factory('* * * * *'));
142+
$task->getFirstExecution()->willReturn(new \DateTime());
143+
144+
$task->setInterval(
145+
$task->reveal()->getInterval(),
146+
$task->reveal()->getFirstExecution(),
147+
Argument::that(
148+
function ($date) {
149+
return $date <= new \DateTime('+1 Minute');
150+
}
151+
)
152+
)->shouldBeCalled();
153+
154+
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
155+
156+
$execution = $this->prophesize(TaskExecutionInterface::class);
157+
$execution->setStatus(TaskStatus::ABORTED);
158+
159+
$this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal());
160+
$this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled();
161+
162+
$command->run(
163+
$this->prophesize(InputInterface::class)->reveal(),
164+
$this->prophesize(OutputInterface::class)->reveal()
165+
);
166+
}
167+
168+
public function testExecuteUpdate()
169+
{
170+
$command = $this->createCommand(
171+
[
172+
'testing' => [
173+
'enabled' => true,
174+
'handler_class' => TestHandler::class,
175+
'workload' => 'test',
176+
'cron_expression' => '* * * * *',
177+
],
178+
]
179+
);
180+
181+
$task = $this->prophesize(TaskInterface::class);
182+
$task->getHandlerClass()->willReturn(TestHandler::class);
183+
$task->getWorkload()->willReturn('test');
184+
$task->getInterval()->willReturn(CronExpression::factory('@daily'));
185+
$task->getFirstExecution()->willReturn(new \DateTime());
186+
187+
$task->setInterval(CronExpression::factory('* * * * *'), $task->reveal()->getFirstExecution())
188+
->shouldBeCalled();
189+
190+
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
191+
192+
$execution = $this->prophesize(TaskExecutionInterface::class);
193+
$execution->setStatus(TaskStatus::ABORTED);
194+
195+
$this->taskExecutionRepository->findPending($task->reveal())->willReturn($execution->reveal());
196+
$this->taskExecutionRepository->save($execution->reveal())->shouldBeCalled();
197+
198+
$this->scheduler->scheduleTasks()->shouldBeCalled();
199+
200+
$command->run(
201+
$this->prophesize(InputInterface::class)->reveal(),
202+
$this->prophesize(OutputInterface::class)->reveal()
203+
);
204+
}
205+
206+
public function testExecuteUpdateNotSupported()
207+
{
208+
$command = $this->createCommand(
209+
[
210+
'testing' => [
211+
'enabled' => true,
212+
'handler_class' => TestHandler::class,
213+
'workload' => 'test',
214+
'cron_expression' => '* * * * *',
215+
],
216+
]
217+
);
218+
219+
$task = $this->prophesize(TaskInterface::class);
220+
$task->getHandlerClass()->willReturn('not-existing');
221+
$task->getWorkload()->willReturn('new-workload');
222+
$task->getInterval()->willReturn(CronExpression::factory('@daily'));
223+
$task->getFirstExecution()->willReturn(new \DateTime());
224+
225+
$task->setInterval(Argument::cetera())->shouldNotBeCalled();
226+
227+
$this->taskRepository->findBySystemKey('testing')->willReturn($task->reveal());
228+
229+
$this->taskExecutionRepository->save(Argument::cetera())->shouldNotBeCalled();
230+
231+
$this->scheduler->scheduleTasks()->shouldNotBeCalled();
232+
233+
$command->run(
234+
$this->prophesize(InputInterface::class)->reveal(),
235+
$this->prophesize(OutputInterface::class)->reveal()
236+
);
237+
}
123238
}

0 commit comments

Comments
 (0)