/** * {@inheritdoc} */ public function run($thread = 0) { //run params $startRunTime = time(); $maxRunTime = $startRunTime + $this->maxExecutionTime; $countExecutedJobs = 0; //params for get item of queue $threadRun = $thread ?: $this->thread; $queue = new Job_queue(); do { //set timeout for next iteration usleep(1000000 * $this->runTimeout); $countExecutedJobs++; try { //getting item $item = $queue->getItem($this->failRetryTime, $threadRun); if (!$item->exists()) { throw new Exception("No actually items for run at " . date($this->dateFormat, time())); } //get params for execute $command = $item->command; $argsStr = $item->args; $args = json_decode($argsStr, true); if (empty($command)) { $item->state = $item::STATUS_FAILED; $item->save(); throw new Exception("Can't run item - data is missing!"); } //start executing $startExecute = new DateTime(); $item->state = $item::STATUS_RUNNING; $item->started_at = $startExecute->format($this->dateFormat); if (!$item->save()) { throw new Exception($item->error->string); } } catch (Exception $e) { continue; } try { $this->launcher->execute($command, $args); $item->state = $item::STATUS_FINISHED; } catch (Exception $e) { log_message('TASK_ERROR', __FUNCTION__ . $e->getMessage()); $item->state = $item->retries <= $item->max_retries ? $item::STATUS_INCOMPLETE : $item::STATUS_FAILED; } //end executing $endExecute = new DateTime(); $item->retries++; $item->closed_at = $endExecute->format($this->dateFormat); $item->runtime = $startExecute->diff($endExecute)->format('%s'); $item->memory_usage = memory_get_usage(false); $item->memory_usage_real = memory_get_usage(true); if (!$item->save()) { log_message('TASK_ERROR', 'Item not saved' . $item->error->string); } } while ($countExecutedJobs < $this->maxJobs && time() < $maxRunTime); }