Esempio n. 1
0
 /**
  * Gets the current queue from the Memcache server.
  *
  * @param boolean $exclusive If set to true, this will lock all processes from querying the queue.
  * @return \SplQueue
  */
 public function getQueue($exclusive = false)
 {
     if ($this->locked) {
         return $this->queue;
     }
     //If the 'add' operation fails, it means the lock is already active.
     if ($this->memcache->add('queue.manager.lock', true)) {
         $queue = $this->memcache->get('queue.manager');
         if ($queue) {
             $this->queue = unserialize($queue);
         }
         if (!$queue) {
             $this->queue = new \SplQueue();
         }
         $this->locked = true;
     } else {
         $interval = $this->memcache->get('queue.process.interval');
         $this->eventDispatcher->dispatch('queue.manager.retrieved', new ManagerRetrievedEvent(ManagerRetrievedEvent::RETRIEVAL_STATUS_FAILED_LOCKED));
         //Wait for the lock to be released.
         while ($this->memcache->get('queue.manager.lock')) {
             usleep($interval * 1000 * 1000);
         }
         $this->queue = unserialize($this->memcache->get('queue.manager'));
         $this->locked = true;
     }
     if (!$exclusive) {
         $this->memcache->delete('queue.manager.lock');
         $this->locked = false;
     }
     return $this->queue;
 }
 /**
  * @param      $sObjectType
  * @param      $id
  * @param bool $refresh
  *
  * @return array|bool|mixed|\Phpforce\SoapClient\Result\SObject[]|\Traversable|void
  */
 public function get($sObjectType, $id, $refresh = false)
 {
     $fields = false;
     $object = false;
     if (!$refresh) {
         $object = $this->memcached->get('salesforce.object.' . $id);
     }
     if (!$object) {
         // Get fields from memcache
         if (!$refresh) {
             $fields = $this->memcached->get('salesforce.objectFields.' . $sObjectType);
         }
         try {
             // If no data, ask Salesforce and save to memcached.
             if (!$fields) {
                 $obj = $this->soapClient->call('describeSObject', array('sObjectType' => $sObjectType));
                 $fields = array();
                 foreach ($obj->getFields() as $field) {
                     $fields[] = $field->getName();
                 }
                 $this->memcached->set('salesforce.objectFields.' . $sObjectType, $fields, $this->memcached_ttl);
             }
             // TODO: Log Salesforce Query and Result
             $object = $this->soapClient->retrieve($fields, array($id), $sObjectType);
             $object = $object[0];
             // Set Memcached
             $this->memcached->set('salesforce.object.' . $id, $object, $this->memcached_ttl);
         } catch (\Exception $e) {
         }
     }
     return $object;
 }
Esempio n. 3
0
 /**
  * Checks if the Queue Manager is already running in a different process.
  *
  * @TODO: This is not using the ->add() atomic operation, so it will fail the concurrency checks. Fix this.
  * @return bool If the manager is running in another process.
  */
 public function checkServer()
 {
     if (!$this->memcache->add('queue.lock', getmypid())) {
         $pid = $this->memcache->get('queue.lock');
         if ($this->isQueueRunning($pid)) {
             return true;
         } else {
             $this->memcache->delete('queue.lock');
             return $this->checkServer();
         }
     }
     return false;
 }