@@ -37,6 +37,20 @@ class TaskQueueService
3737 */
3838 private $ defaultTube ;
3939
40+ /**
41+ * Log the worker output on success
42+ *
43+ * @var boolean
44+ */
45+ private $ logWorkerOutputOnSuccess ;
46+
47+ /**
48+ * Log the worker output on failure
49+ *
50+ * @var boolean
51+ */
52+ private $ logWorkerOutputOnFailure ;
53+
4054 /**
4155 * @var \Doctrine\ORM\EntityManager
4256 */
@@ -65,6 +79,8 @@ public function __construct(
6579 $ this ->entityManager = $ entityManager ;
6680 $ this ->taskRepo = $ this ->entityManager ->getRepository ('WebdevviePheanstalkTaskQueueBundle:Task ' );
6781 $ this ->defaultTube = $ params ['default_tube ' ];
82+ $ this ->logWorkerOutputOnSuccess = $ params ['log_worker_output_on_success ' ];
83+ $ this ->logWorkerOutputOnFailure = $ params ['log_worker_output_on_failure ' ];
6884 }
6985
7086 /**
@@ -86,8 +102,7 @@ public function getDefaultTube()
86102 public function getStatusOfTaskWithId ($ taskId )
87103 {
88104 $ task = $ this ->taskRepo ->find ($ taskId );
89- if (!($ task instanceof Task))
90- {
105+ if (!($ task instanceof Task)) {
91106 //the task was not found
92107 return Task::STATUS_GONE ;
93108 }
@@ -169,7 +184,7 @@ public function cleanUpTasks($timePeriod)
169184 t.created <= :older '
170185 );
171186 $ query ->setParameter ('status ' , array (Task::STATUS_DONE ));
172- $ query ->setParameter ('older ' , date ("Y-m-d H:i:s " , time ()- $ timePeriod ));
187+ $ query ->setParameter ('older ' , date ("Y-m-d H:i:s " , time () - $ timePeriod ));
173188 $ query ->execute ();
174189 }
175190
@@ -206,7 +221,9 @@ public function reserveTask($tube = null)
206221 throw new TaskQueueServiceException ("Invalid format in TaskQueue {$ tube } " );
207222 } catch (\ReflectionException $ exception ) {
208223 $ this ->beanstalk ->delete ($ inTask );
209- throw new TaskQueueServiceException ("Invalid format in TaskQueue {$ tube } class " .$ parts [0 ].' is unknown ' );
224+ throw new TaskQueueServiceException (
225+ "Invalid format in TaskQueue {$ tube } class " . $ parts [0 ] . ' is unknown '
226+ );
210227 }
211228 if (!($ taskObject instanceof TaskDescriptionInterface)) {
212229 $ this ->beanstalk ->delete ($ inTask );
@@ -231,11 +248,15 @@ public function reserveTask($tube = null)
231248 * Deletes a task from the queue
232249 *
233250 * @param WorkPackage $task
251+ * @param string $log
234252 * @throws TaskQueueServiceException
235253 * @return void
236254 */
237- public function markDone (WorkPackage $ task )
255+ public function markDone (WorkPackage $ task, $ log )
238256 {
257+ if ($ this ->logWorkerOutputOnSuccess ) {
258+ $ this ->updateTaskLog ($ task , $ log );
259+ }
239260 $ this ->updateTaskStatus ($ task , Task::STATUS_DONE );
240261 $ this ->beanstalk ->delete ($ task ->getPheanstalkJob ());
241262 }
@@ -244,16 +265,42 @@ public function markDone(WorkPackage $task)
244265 * Marks a job as failed and deletes it from the beanstalk tube
245266 *
246267 * @param WorkPackage $task
268+ * @param string $log
247269 * @throws TaskQueueServiceException
248270 * @return void
249271 */
250- public function markFailed (WorkPackage $ task )
272+ public function markFailed (WorkPackage $ task, $ log )
251273 {
274+ if ($ this ->logWorkerOutputOnFailure ) {
275+ $ this ->updateTaskLog ($ task , $ log );
276+ }
252277 $ this ->updateTaskStatus ($ task , Task::STATUS_FAILED );
253278 $ this ->beanstalk ->delete ($ task ->getPheanstalkJob ());
254279 }
255280
256281 /**
282+ * Writes the log to the Task entity
283+ *
284+ * @param WorkPackage $task
285+ * @param string $log
286+ * @return void
287+ * @throws TaskQueueServiceException
288+ */
289+ private function updateTaskLog (WorkPackage $ task , $ log )
290+ {
291+ $ taskEntity = $ task ->getTaskEntity ();
292+ if ($ taskEntity instanceof Task) {
293+ $ taskEntity ->setLog ($ log );
294+ //make sure it is stored...
295+ $ this ->entityManager ->flush ($ taskEntity );
296+ } else {
297+ throw new TaskQueueServiceException ("Entity is not of type Task " );
298+ }
299+ }
300+
301+ /**
302+ * Updates the task status
303+ *
257304 * @param WorkPackage $task
258305 * @param string $status
259306 * @return void
0 commit comments