/**
  * Handler for all queued events
  *
  * @param null|integer $queueId - id "очереди"
  *
  * @return mixed
  *
  * @throws \yii\base\InvalidParamException
  */
 public function actionHandle($queueId = null)
 {
     /* Время начала работы скрипта */
     $start = microtime(true);
     if ($queueId === null) {
         // Check if the instance of current command has been already started and alive
         if ($this->isLocked()) {
             return;
         }
         // Infinite cicle
         do {
             $queues = QmQueues::findQueues();
             foreach ($queues as $queue) {
                 $queuePid = $this->getPidFromLock($queue->tag, $this->offset);
                 if ($queuePid === null || !posix_kill($queuePid, 0)) {
                     if ($queue->scheduler && file_exists(Yii::getAlias($queue->scheduler))) {
                         $command = Yii::getAlias($queue->scheduler) . ' queue/queue/handle';
                     } else {
                         $command = Yii::$app->request->scriptFile . ' queue/queue/handle';
                     }
                     $prepare = 'nice -n 19 ' . $command . ' ' . (string) $queue->id;
                     /* Задачь за 1 выполнеие */
                     if ($this->taskPerShoot !== null) {
                         $prepare .= ' --taskPerShoot=' . $this->taskPerShoot;
                     }
                     /* Отступ */
                     if ($this->offset !== 0) {
                         $prepare .= ' --offset=' . $this->offset;
                     }
                     shell_exec($prepare . ' 2>&1 &');
                 }
             }
             sleep($this->sleep);
         } while ($this->isLockAlive());
     } else {
         /* @var null|QmQueues $queue - очередь */
         $queue = QmQueues::findOne(['id' => $queueId]);
         if ($queue === null) {
             return false;
         }
         $queuePid = $this->getPidFromLock($queue->tag, $this->offset);
         if ($queuePid === null || !posix_kill($queuePid, 0)) {
             $queuePid = getmypid();
             if ($this->setPidFromLock($queue->tag, $this->offset, $queuePid)) {
                 /* Устанавливаем альтернативное количество обрабатываемых строк за "выстрел" */
                 if ($this->taskPerShoot !== null) {
                     $queue->tasks_per_shot = $this->taskPerShoot;
                 }
                 if ($this->offset !== 0) {
                     $queue->offset = $this->offset;
                 }
                 try {
                     /* Обработка задач пока разница составляет менее 55 секунд (скрипт выполняется 55 секунд) */
                     while (microtime(true) - $start < 55) {
                         $queue->handleShot();
                         sleep($this->sleep);
                     }
                     $this->setPidFromLock($queue->tag, $this->offset, null);
                 } catch (\Exception $e) {
                     $this->setPidFromLock($queue->tag, $this->offset, null);
                 }
             }
         }
     }
     return true;
 }
 /**
  * Relation с очередью
  *
  * @return \yii\db\ActiveQuery
  */
 public function getQueue()
 {
     return $this->hasOne(QmQueues::className(), ['id' => 'queue_id']);
 }
 /**
  * Finds the QmQueues model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return QmQueues the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = QmQueues::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function getQueues()
 {
     return QmQueues::findQueues();
 }