Skip to content

Commit e979166

Browse files
committed
Merge pull request #6 from webdevvie/feature/identifier-and-status-request
Status retrieval for task with id and return task entity id on adding the task to the queue
2 parents c740c3b + a52d2ae commit e979166

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

Entity/Task.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,41 @@
1414
*/
1515
class Task
1616
{
17+
/**
18+
* The task is awaiting start
19+
*/
1720
const STATUS_NEW = 'new';
21+
22+
/**
23+
* The task is in progress.. awaiting completion (or failure)
24+
*/
1825
const STATUS_WORKING = 'working';
26+
27+
/**
28+
* Unused at this time
29+
*/
1930
const STATUS_STALLED = 'stalled';
31+
32+
/**
33+
* The task failed executing ( the command returned an exitcode !=0)
34+
*/
2035
const STATUS_FAILED = 'failed';
36+
37+
/**
38+
* The task was manually restarted
39+
*/
2140
const STATUS_RESTARTED = 'restarted';
41+
42+
/**
43+
* The task was completed successfully
44+
*/
2245
const STATUS_DONE = 'done';
2346

47+
/**
48+
* The Task entity was not found when searching for it.
49+
*/
50+
const STATUS_GONE = 'gone';
51+
2452
/**
2553
* @var integer
2654
*

Service/TaskQueueService.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,29 @@ public function getDefaultTube()
7676
{
7777
return $this->defaultTube;
7878
}
79+
80+
/**
81+
* Returns the current status of a task
82+
*
83+
* @param integer $taskId
84+
* @return string
85+
*/
86+
public function getStatusOfTaskWithId($taskId)
87+
{
88+
$task = $this->taskRepo->find($taskId);
89+
if(!($task instanceof Task))
90+
{
91+
//the task was not found
92+
return Task::STATUS_GONE;
93+
}
94+
return $task->getStatus();
95+
}
96+
7997
/**
8098
* @param TaskDescriptionInterface $task
8199
* @param string $tube (optional) the tube to use
82100
* @throws TaskQueueServiceException
83-
* @return void
101+
* @return integer
84102
*/
85103
public function queueTask(TaskDescriptionInterface $task, $tube = null)
86104
{
@@ -97,6 +115,8 @@ public function queueTask(TaskDescriptionInterface $task, $tube = null)
97115
$this->beanstalk
98116
->useTube($tube)
99117
->put($stringVersion);
118+
119+
return $taskEntity->getId();
100120
}
101121

102122
/**

Tests/Service/TaskQueueServiceTest.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,49 @@ public function setUp()
5858
$this->setupEntityManagerMock();
5959
}
6060

61+
/**
62+
* Test if the proper status is returned in case a task is not found for the Id
63+
*
64+
* @return void
65+
*/
66+
public function testIfGettingTaskStatusWorksIfThereIsNoSuchTask()
67+
{
68+
$this->taskRepository->shouldReceive('find')->andReturn(null);
69+
$service = new TaskQueueService(
70+
$this->entityManager,
71+
$this->pheanStalkProxy,
72+
$this->serializer,
73+
$this->params
74+
);
75+
$response = $service->getStatusOfTaskWithId(1);
76+
$this->assertEquals(Task::STATUS_GONE, $response, 'Expected "gone" Response!');
77+
}
78+
79+
/**
80+
* Test if the proper status is returned in case a task is found
81+
*
82+
* @return void
83+
*/
84+
public function testIfGettingTaskStatusWorksIfThereIsATask()
85+
{
86+
$exampleTask = new ExampleTaskDescription();
87+
$stringVersion = get_class($exampleTask) .
88+
"::" .
89+
$this->serializer->serialize($exampleTask, 'json');
90+
$taskEntity = new Task($exampleTask, $stringVersion, 'gtldtube');
91+
$taskEntity->setStatus(Task::STATUS_DONE);
92+
93+
$this->taskRepository->shouldReceive('find')->andReturn($taskEntity);
94+
$service = new TaskQueueService(
95+
$this->entityManager,
96+
$this->pheanStalkProxy,
97+
$this->serializer,
98+
$this->params
99+
);
100+
$response = $service->getStatusOfTaskWithId(1);
101+
$this->assertEquals(Task::STATUS_DONE, $response, 'Expected "done" Response!');
102+
}
103+
61104
/**
62105
* Test the queueing of tasks
63106
*
@@ -105,6 +148,7 @@ public function testMarkDone()
105148
);
106149
$service->markDone($workPackage);
107150
}
151+
108152
/**
109153
* tests the status update call in the service
110154
*
@@ -142,7 +186,6 @@ public function testRegenerate()
142186
$id = 1;
143187

144188

145-
146189
$exampleTask = new ExampleTaskDescription();
147190

148191
$stringVersion = get_class($exampleTask) . "::" . $this->serializer->serialize($exampleTask, 'json');
@@ -221,7 +264,6 @@ public function testReserveFilled()
221264
}
222265

223266

224-
225267
/**
226268
* Sets up the example task into the (mocked) queue and in the (mocked) database
227269
*

0 commit comments

Comments
 (0)