queue_name = $this->getName(); $dao->submit_time = CRM_Utils_Time::getTime('YmdHis'); $dao->data = serialize($data); $dao->weight = CRM_Utils_Array::value('weight', $options, 0); $dao->save(); } /** * Get the next item. * * @param int $lease_time * Seconds. * * @return object * With key 'data' that matches the inputted data. */ public function claimItem($lease_time = 3600) { $result = NULL; $dao = CRM_Core_DAO::executeQuery('LOCK TABLES civicrm_queue_item WRITE;'); $sql = "SELECT id, queue_name, submit_time, release_time, data FROM civicrm_queue_item WHERE queue_name = %1 AND (release_time IS NULL OR release_time < %2) ORDER BY weight ASC, id ASC LIMIT 1 "; $params = [ 1 => [$this->getName(), 'String'], 2 => [CRM_Utils_Time::getTime(), 'Timestamp'], ]; $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem'); if (is_a($dao, 'DB_Error')) { // FIXME - Adding code to allow tests to pass CRM_Core_Error::fatal(); } if ($dao->fetch()) { $nowEpoch = CRM_Utils_Time::getTimeRaw(); CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", [ '1' => [date('YmdHis', $nowEpoch + $lease_time), 'String'], '2' => [$dao->id, 'Integer'], ]); // (Comment by artfulrobot Sep 2019: Not sure what the below comment means, should be removed/clarified?) // work-around: inconsistent date-formatting causes unintentional breakage # $dao->submit_time = date('YmdHis', strtotime($dao->submit_time)); # $dao->release_time = date('YmdHis', $nowEpoch + $lease_time); # $dao->save(); $dao->data = unserialize($dao->data); $result = $dao; } $dao = CRM_Core_DAO::executeQuery('UNLOCK TABLES;'); return $result; } /** * Get the next item, even if there's an active lease * * @param int $lease_time * Seconds. * * @return object * With key 'data' that matches the inputted data. */ public function stealItem($lease_time = 3600) { $sql = " SELECT id, queue_name, submit_time, release_time, data FROM civicrm_queue_item WHERE queue_name = %1 ORDER BY weight ASC, id ASC LIMIT 1 "; $params = [ 1 => [$this->getName(), 'String'], ]; $dao = CRM_Core_DAO::executeQuery($sql, $params, TRUE, 'CRM_Queue_DAO_QueueItem'); if ($dao->fetch()) { $nowEpoch = CRM_Utils_Time::getTimeRaw(); CRM_Core_DAO::executeQuery("UPDATE civicrm_queue_item SET release_time = %1 WHERE id = %2", [ '1' => [date('YmdHis', $nowEpoch + $lease_time), 'String'], '2' => [$dao->id, 'Integer'], ]); $dao->data = unserialize($dao->data); return $dao; } } }