/** * Perform a quote on the remote Provider server * * @see OutsourceTo_AbstractProvider::performQuote * * @param array|null $volAnalysis */ public function performQuote($volAnalysis = null) { /** * cache this job info for 20 minutes ( session duration ) */ $cache_cart = Shop_Cart::getInstance('outsource_to_external_cache'); if ($volAnalysis == null) { //call matecat API for Project status and information $project_url_api = INIT::$HTTPHOST . INIT::$BASEURL . "api/status?id_project=" . $this->pid . "&project_pass="******"Project Not Found in Cache. Call API url for STATUS: " . $project_url_api); $options = array(CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0, CURLOPT_USERAGENT => INIT::MATECAT_USER_AGENT . INIT::$BUILD_NUMBER, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2); //prepare handlers for curl to quote service $mh = new MultiCurlHandler(); $resourceHash = $mh->createResource($project_url_api, $options); $mh->multiExec(); if ($mh->hasError($resourceHash)) { Log::doLog($mh->getError($resourceHash)); } $raw_volAnalysis = $mh->getSingleContent($resourceHash); $mh->multiCurlCloseAll(); //retrieve the project subject: pick the project's first job and get the subject $jobData = getJobData($this->jobList[0]['jid'], $this->jobList[0]['jpassword']); $subject = $jobData['subject']; $itemCart = new Shop_ItemHTSQuoteJob(); $itemCart['id'] = $project_url_api; $itemCart['show_info'] = $raw_volAnalysis; $itemCart['subject'] = $subject; $cache_cart->addItem($itemCart); } else { $tmp_project_cache = $cache_cart->getItem($project_url_api); $raw_volAnalysis = $tmp_project_cache['show_info']; $subject = $tmp_project_cache['subject']; } // Log::doLog( $raw_volAnalysis ); $volAnalysis = json_decode($raw_volAnalysis, true); } // Log::doLog( $volAnalysis ); $_jobLangs = array(); $options = array(CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => 0, CURLOPT_USERAGENT => INIT::MATECAT_USER_AGENT . INIT::$BUILD_NUMBER, CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2); //prepare handlers for curl to quote service $mh = new MultiCurlHandler(); foreach ($this->jobList as $job) { //trim decimals to int $job_payableWords = (int) $volAnalysis['data']['jobs'][$job['jid']]['totals'][$job['jpassword']]['TOTAL_PAYABLE'][0]; /* * //languages are in the form: * * "langpairs":{ * "5888-e94bd2f79afd":"en-GB|fr-FR", * "5889-c853a841dafd":"en-GB|de-DE", * "5890-e852ca45c66e":"en-GB|it-IT", * "5891-b43f2f067319":"en-GB|es-ES" * }, * */ $langPairs = $volAnalysis['jobs']['langpairs'][$job['jid'] . "-" . $job['jpassword']]; $_langPairs_array = explode("|", $langPairs); $source = $_langPairs_array[0]; $target = $_langPairs_array[1]; //save langpairs of the jobs $_jobLangs[$job['jid'] . "-" . $job['jpassword']]['source'] = $source; $_jobLangs[$job['jid'] . "-" . $job['jpassword']]['target'] = $target; $url = "https://www.translated.net/hts/?f=quote&cid=htsdemo&p=htsdemo5&s={$source}&t={$target}&pn=MATECAT_{$job['jid']}-{$job['jpassword']}&w={$job_payableWords}&df=matecat&matecat_pid=" . $this->pid . "&matecat_ppass="******"&matecat_pname=" . $volAnalysis['data']['summary']['NAME'] . "&subject=" . $subject; if (!$cache_cart->itemExists($job['jid'] . "-" . $job['jpassword'])) { Log::doLog("Not Found in Cache. Call url for Quote: " . $url); $tokenHash = $mh->createResource($url, $options, $job['jid'] . "-" . $job['jpassword']); } else { $cartElem = $cache_cart->getItem($job['jid'] . "-" . $job['jpassword']); $cartElem["currency"] = $this->currency; $cartElem["timezone"] = $this->timezone; $cache_cart->delItem($job['jid'] . "-" . $job['jpassword']); $cache_cart->addItem($cartElem); } } $mh->multiExec(); $res = $mh->getAllContents(); $failures = array(); //fetch contents and store in cache if there are foreach ($res as $jpid => $quote) { if ($mh->hasError($jpid)) { Log::doLog($mh->getError($jpid)); } /* * Quotes are plain text line feed separated fields in the form: * 1 * OK * 2014-04-16T09:30:00Z * 488 * 46.36 * 11140320 * 1 */ Log::doLog($quote); $result_quote = explode("\n", $quote); $itemCart = new Shop_ItemHTSQuoteJob(); $itemCart['id'] = $jpid; $itemCart['project_name'] = $volAnalysis['data']['summary']['NAME']; $itemCart['name'] = "MATECAT_{$jpid}"; $itemCart['delivery_date'] = $result_quote[2]; $itemCart['words'] = $result_quote[3]; $itemCart['price'] = $result_quote[4] ? $result_quote[4] : 0; $itemCart['currency'] = $this->currency; $itemCart['timezone'] = $this->timezone; $itemCart['quote_pid'] = $result_quote[5]; $itemCart['source'] = $_jobLangs[$jpid]['source']; //get the right language $itemCart['target'] = $_jobLangs[$jpid]['target']; //get the right language $itemCart['show_info'] = $result_quote[6]; $itemCart['subject'] = $subject; $cache_cart->addItem($itemCart); Log::doLog($itemCart); //Oops we got an error if ($itemCart['price'] == 0 && empty($itemCart['words'])) { $failures[$jpid] = $jpid; } } $shopping_cart = Shop_Cart::getInstance('outsource_to_external'); //now get the right contents foreach ($this->jobList as $job) { $shopping_cart->delItem($job['jid'] . "-" . $job['jpassword']); $shopping_cart->addItem($cache_cart->getItem($job['jid'] . "-" . $job['jpassword'])); $this->_quote_result = array($shopping_cart->getItem($job['jid'] . "-" . $job['jpassword'])); } //check for failures.. destroy the cache if (!empty($failures)) { foreach ($failures as $jpid) { $cache_cart->delItem($jpid); } } }
/** * Get the number of consumers for this queue * * @param null $queueName * * @return mixed * @throws Exception */ public function getConsumerCount($queueName = null) { if (!empty($queueName)) { $queue = $queueName; } elseif (!empty($this->queueName)) { $queue = $this->queueName; } else { $queue = INIT::$QUEUE_NAME; } $queue_inteface_url = INIT::$QUEUE_JMX_ADDRESS . "/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName={$queue}/ConsumerCount"; $mHandler = new MultiCurlHandler(); $options = array(CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_USERAGENT => INIT::MATECAT_USER_AGENT . INIT::$BUILD_NUMBER, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2, CURLOPT_HTTPHEADER => array('Authorization: Basic ' . base64_encode("admin:admin"))); $resource = $mHandler->createResource($queue_inteface_url, $options); $mHandler->multiExec(); $result = $mHandler->getSingleContent($resource); $mHandler->multiCurlCloseAll(); $result = json_decode($result, true); Utils::raiseJsonExceptionError(); return $result['value']; }
protected function _call($url, array $curl_options = array()) { $mh = new MultiCurlHandler(); $uniq_uid = uniqid('', true); /* * Append array elements from the second array * to the first array while not overwriting the elements from * the first array and not re-indexing * * Use the + array union operator */ $resourceHash = $mh->createResource($url, $this->curl_additional_params + $curl_options, $uniq_uid); $mh->multiExec(); if ($mh->hasError($resourceHash)) { $curl_error = $mh->getError($resourceHash); Log::doLog('Curl Error: (http status ' . $curl_error['http_code'] . ') ' . $curl_error['errno'] . " - " . $curl_error['error'] . " " . var_export(parse_url($url), true)); $rawValue = array('error' => array('code' => -$curl_error['errno'], 'message' => " {$curl_error['error']}. Server Not Available (http status " . $curl_error['http_code'] . ")")); //return negative number } else { $rawValue = $mh->getSingleContent($resourceHash); } $mh->multiCurlCloseAll(); if ($this->doLog) { Log::doLog($uniq_uid . " ... Received... " . var_export($rawValue, true)); } return $rawValue; }
/** * Retrieve data about the whole project (information common to all jobs): subject and volume analysis * These info are retrieved from API status and Database and cached in session, as if they were a real quote. * The workflow is as follow: * - if the data is not in a Shop_ItemHTSQuoteJob cached in session * retrieve them from API and DB and put them in session. * - retrieve project information from session and return them to caller * @see GUIDE->"PROCEDURE"->POINT 1 for details * * @param array|null $volAnalysis * @return array */ private function __getProjectData($volAnalysis) { // API Status url, used as session cache id too $project_url_api = INIT::$HTTPHOST . INIT::$BASEURL . "api/status?id_project=" . $this->pid . "&project_pass="******"Project Not Found in Cache. Call API url for STATUS: " . $project_url_api); /** ************************** GET VOLUME ANALYSIS FIRST ************************* */ // NOTE: by default timeout is set to 10s, but 5 seconds will be enough to call itself $curlOptionsForAnalysis = $this->_curlOptions; $curlOptionsForAnalysis[CURLOPT_CONNECTTIMEOUT] = 5; // prepare handlers for curl to quote service, and execute curl $mh = new MultiCurlHandler(); $resourceHash = $mh->createResource($project_url_api, $curlOptionsForAnalysis); $mh->multiExec(); // any error? Log it if ($mh->hasError($resourceHash)) { Log::doLog($mh->getError($resourceHash)); } // get API Status reply $volAnalysis = $mh->getSingleContent($resourceHash); // free resources $mh->multiCurlCloseAll(); /** *************************** GET SUBJECT ************************************** */ // subject is retrieved from database: get first job of the project and get its subject $jobData = getJobData($this->jobList[0]['jid'], $this->jobList[0]['jpassword']); // store all the data in a Shop_ItemHTSQuoteJob $projectItemCart = new Shop_ItemHTSQuoteJob(); $projectItemCart['id'] = $project_url_api; $projectItemCart['subject'] = $jobData['subject']; $projectItemCart['show_info'] = $volAnalysis; // store the Shop_ItemHTSQuoteJob itself in a Shop_Cart cached in session Shop_Cart::getInstance('outsource_to_external_cache')->addItem($projectItemCart); } // Now we are sure the data is in cache, so retrieve it $projectItemCart = Shop_Cart::getInstance('outsource_to_external_cache')->getItem($project_url_api); // in case volume analysis has been passed as parameter, update the cart, and the cache content consecutively if ($volAnalysis != null) { $projectItemCart['show_info'] = $volAnalysis; Shop_Cart::getInstance('outsource_to_external_cache')->delItem($project_url_api); Shop_Cart::getInstance('outsource_to_external_cache')->addItem($projectItemCart); } return array($projectItemCart['subject'], json_decode($projectItemCart['show_info'], true)); }