예제 #1
0
 /**
  * @inheritdoc
  */
 public function persistJobState(Job &$job)
 {
     if (!array_key_exists(spl_object_hash($job), $this->jobs)) {
         throw new ReflectionException(sprintf('Untracked job reported'));
     }
     $original = $this->jobs[spl_object_hash($job)];
     $version = JobReflector::getVersion($job);
     $qb = $this->connection->createQueryBuilder();
     $qb->update($this->getQueueTableName())->set($this->columns[JobReflector::PROPERTY_VERSION], ':newVersion')->where($qb->expr()->eq($this->columns[JobReflector::PROPERTY_ID], ':id'))->andWhere($qb->expr()->eq($this->columns[JobReflector::PROPERTY_VERSION], ':version'));
     $qb->setParameters(['id' => $job->getId(), 'version' => $version, 'newVersion' => $version + 1]);
     foreach ($this->columns as $property => $column) {
         if ($property == '__CLASS__') {
             continue;
         }
         $old = JobReflector::getProperty($original, $property);
         $new = JobReflector::getProperty($job, $property);
         if ($old != $new) {
             if ($new instanceof DateTimeImmutable) {
                 $new = $new->format('Y-m-d H:i:s');
             } elseif (is_array($new)) {
                 $new = json_encode($new);
             }
             $qb->set($column, $qb->createNamedParameter($new));
         }
     }
     $result = $qb->execute();
     if ($result === 1) {
         JobReflector::setVersion($job, $version + 1);
         $this->jobs[spl_object_hash($job)] = clone $job;
         return $job;
     } else {
         throw new Exception\LockException(sprintf('Could not lock job #%d', $job->getId()));
     }
 }