Example #1
0
 /**
  * @param  PromiseInterface $promise
  * @return bool
  */
 public function reject(PromiseInterface $promise)
 {
     if ($this->isSettled($promise)) {
         throw new \LogicException("Settled promises can't be rejected");
     }
     $this->connection->execute('UPDATE ' . self::PROMISES_TABLE_NAME . ' SET `settlement` = ?, `settled_at` = UTC_TIMESTAMP() WHERE `signature` = ?', PromiseInterface::REJECTED, $promise->getSignature());
     if ($this->connection->affectedRows()) {
         if ($this->log) {
             $this->log->info('Promise {promise_id} rejected', ['promise_id' => $promise->getSignature()]);
         }
     } else {
         if ($this->log) {
             $this->log->error('Promise {promise_id} not found', ['promise_id' => $promise->getSignature()]);
         }
         throw new InvalidArgumentException("Promise '{$promise->getSignature()}' not found");
     }
 }
Example #2
0
 /**
  * Reserve next job ID.
  *
  * @param  array|null $from_channels
  * @return int|null
  */
 public function reserveNextJob(array $from_channels = null)
 {
     $timestamp = date('Y-m-d H:i:s');
     $channel_conditions = empty($from_channels) ? '' : $this->connection->prepareConditions(['`channel` IN ? AND ', $from_channels]);
     if ($job_ids = $this->connection->executeFirstColumn('SELECT `id` FROM `' . self::JOBS_TABLE_NAME . "` WHERE {$channel_conditions}`reserved_at` IS NULL AND `available_at` <= ? ORDER BY `priority` DESC, `id` LIMIT 0, 100", $timestamp)) {
         foreach ($job_ids as $job_id) {
             $reservation_key = $this->prepareNewReservationKey();
             if ($this->on_reservation_key_ready) {
                 call_user_func($this->on_reservation_key_ready, $job_id, $reservation_key);
             }
             $this->connection->execute('UPDATE `' . self::JOBS_TABLE_NAME . '` SET `reservation_key` = ?, `reserved_at` = ? WHERE `id` = ? AND `reservation_key` IS NULL', $reservation_key, $timestamp, $job_id);
             if ($this->connection->affectedRows() === 1) {
                 return $job_id;
             }
         }
     }
     return null;
 }