Пример #1
0
 /**
  * @param \Venne\Queue\Worker $worker
  */
 public function pauseWorker(Worker $worker)
 {
     $this->configManager->lock();
     $data = $this->configManager->loadConfigFile();
     $data['worker'][$worker->getId()]['state'] = self::STATE_PAUSED;
     $this->configManager->saveConfigFile($data);
     $worker->log('Worker has been paused');
     $this->configManager->unlock();
 }
Пример #2
0
 /**
  * @param \Venne\Queue\Worker $worker
  * @return bool
  * @internal
  */
 public function doNextWork(Worker $worker)
 {
     $this->configManager->lock();
     $jobEntity = $this->jobRepository->createQueryBuilder('a')->addOrderBy('a.priority', 'DESC')->addOrderBy('a.date', 'ASC')->andWhere('a.date <= :now')->setParameter('now', new \DateTime())->andWhere('a.state = :state')->setParameter('state', Job::STATE_SCHEDULED)->setMaxResults(1)->getQuery()->getOneOrNullResult();
     $data = $this->configManager->loadConfigFile();
     $data['worker'][$worker->getId()]['lastCheck'] = (new \DateTime())->format('Y-m-d H:i:s');
     $this->configManager->saveConfigFile($data);
     if ($jobEntity) {
         $jobEntity->state = $jobEntity::STATE_IN_PROGRESS;
         $this->entityManager->flush($jobEntity);
         $this->configManager->unlock();
         try {
             $this->doJob($jobEntity, self::PRIORITY_DEFAULT);
         } catch (JobFailedException $e) {
             $failed = true;
             $jobEntity->state = $jobEntity::STATE_FAILED;
             $this->entityManager->flush($jobEntity);
             $worker->log('Error: ' . $e->getPrevious()->getMessage());
         }
         if (!isset($failed)) {
             if ($jobEntity->dateInterval && $jobEntity->round !== 0) {
                 $zero = new \DateTime('00:00');
                 $diff = $zero->diff($jobEntity->dateInterval);
                 $jobEntity->round--;
                 $jobEntity->date = $jobEntity->date->add($diff);
                 $jobEntity->state = $jobEntity::STATE_SCHEDULED;
                 $this->entityManager->persist($jobEntity);
             }
         }
         $this->configManager->lock();
         $data = $this->configManager->loadConfigFile();
         $data['worker'][$worker->getId()]['lastJob'] = (new \DateTime())->format('Y-m-d H:i:s');
         $this->configManager->saveConfigFile($data);
         $this->configManager->unlock();
         return true;
     } else {
         $this->configManager->unlock();
     }
     return false;
 }