Пример #1
0
 /**
  * Decide how to process given operation
  *
  * @param RequestDescriptor $requestDescriptor Operation to decide
  * @param int               $totalItems Total amount of pending requestDescriptors
  *
  * @return int One of LimitationSolverInterface::DECISION_* consts
  */
 private function decide(RequestDescriptor $requestDescriptor, $totalItems)
 {
     $meta = $requestDescriptor->getMetadata();
     if ($requestDescriptor->isRunning()) {
         return LimitationSolverInterface::DECISION_SKIP_CURRENT;
     }
     $isSkippingThis = $meta[RequestExecutorInterface::META_CONNECTION_START_TIME] !== null;
     if ($isSkippingThis) {
         return LimitationSolverInterface::DECISION_SKIP_CURRENT;
     }
     $decision = $this->decider->decide($this->executor, $requestDescriptor->getSocket(), $totalItems);
     if ($decision !== LimitationSolverInterface::DECISION_OK) {
         return $decision;
     }
     return LimitationSolverInterface::DECISION_OK;
 }
 /** {@inheritdoc} */
 public function executeRequest()
 {
     if ($this->isExecuting()) {
         throw new \BadMethodCallException('Request is already in progress');
     }
     $this->isRequestStopped = false;
     $this->solver = $this->solver ?: new NoLimitationSolver();
     $this->isExecuting = true;
     $eventCaller = new EventCaller($this);
     try {
         foreach ($this->eventHandlers as $handler) {
             $eventCaller->addHandler($handler);
         }
         if ($this->solver instanceof EventHandlerInterface) {
             $eventCaller->addHandler($this->solver);
         }
         $this->initializeRequest($eventCaller);
         $eventCaller->addHandler($this);
         $this->solver->initialize($this);
         $this->doExecuteRequest($eventCaller);
         $this->solver->finalize($this);
         $this->terminateRequest();
     } catch (StopRequestExecuteException $e) {
         $this->isRequestStopInProgress = true;
         $this->disconnectItems($this->socketBag->getItems());
     } catch (SocketException $e) {
         foreach ($this->socketBag->getItems() as $item) {
             $eventCaller->setCurrentOperation($item);
             $eventCaller->callExceptionSubscribers($item, $e);
         }
         $this->disconnectItems($this->socketBag->getItems());
     } catch (\Exception $e) {
         $this->isExecuting = false;
         $this->emergencyShutdown();
         $this->solver->finalize($this);
         $this->terminateRequest();
         throw $e;
     }
     $this->solver->finalize($this);
     $this->terminateRequest();
     $this->isExecuting = false;
 }