示例#1
0
 protected function generateTicketId()
 {
     $this->logger->debug("Request zohocrm for ticket id...");
     $ch = curl_init();
     $query = array("servicename" => "ZohoCRM", "FROM_AGENT" => "true", "LOGIN_ID" => $this->username, "PASSWORD" => $this->password);
     curl_setopt_array($ch, array(CURLOPT_URL => "https://accounts.zoho.com/login?" . http_build_query($query), CURLOPT_RETURNTRANSFER => 1, CURLOPT_HEADER => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_SSL_VERIFYHOST => 0));
     $httpBody = curl_exec($ch);
     $httpStatus = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
     // Output example:
     /*		
     #
     #Mon Nov 02 03:49:58 PST 2009
     GETUSERNAME=null
     WARNING=null
     PASS_EXPIRY=-1
     TICKET=3da08fd10867a6114f70090982878cbc
     RESULT=TRUE
     */
     if ($httpStatus == 200) {
         $response = Scalr_Util_Compat::parseIniString($httpBody);
         if ($response) {
             if ($response["RESULT"] == "1") {
                 $this->logger->debug(sprintf("Ticket '%s' was generated", $response["TICKET"]));
                 return $response["TICKET"];
             } else {
                 throw new Scalr_Service_ZohoCrm_Exception(sprintf("Cannot generate ticket. ZohoCRM warn: %s", $response["WARNING"]));
             }
         } else {
             throw new Scalr_Service_ZohoCrm_Exception(sprintf("Cannot parse response. ZohoCRM response: '%s'", $httpBody));
         }
     } else {
         throw new Scalr_Service_ZohoCrm_Exception(sprintf("ZohoCRM service failed (code: %d, body: %s)", $httpStatus, $httpBody));
     }
 }
示例#2
0
 protected function init($options = null)
 {
     $this->logger = Logger::getLogger(__CLASS__);
     // Merge configurations. this config, ini config
     // Get configuration filename from ENV, CLI options, own static config, default: "dcron.ini"
     $configFileName = $_ENV[self::ENV_CONFIG_FILE_PROPERTY];
     if (!$configFileName) {
         if ($options && $options["getopt"]) {
             $configFileName = $options["getopt"]->getOption(self::GETOPT_CONFIG_FILE);
         }
     }
     if (!$configFileName) {
         if ($this->config["iniFile"]) {
             $configFileName = $this->config["iniFile"];
         }
     }
     if (!$configFileName) {
         $configFileName = "dcron.ini";
     }
     // Load configuration
     $configString = @file_get_contents($configFileName);
     if (!$configString) {
         throw new Scalr_System_Cronjob_Exception(sprintf("Cannot load configuration file '%s'", $configFileName));
     }
     $iniConfig = Scalr_Util_Compat::parseIniString($configString, true);
     if (!$iniConfig) {
         throw new Scalr_System_Cronjob_Exception(sprintf("Cannot parse configuration file '%s'", $configFileName));
     }
     // XXX Temporary hack
     if ($iniConfig["remoteConfigUrl"]) {
         $this->logger->debug(sprintf("Fetch configuration from '%s'", $iniConfig["remoteConfigUrl"]));
         $configString = @file_get_contents($iniConfig["remoteConfigUrl"]);
         if (!$configString) {
             throw new Scalr_System_Cronjob_Exception(sprintf("Cannot load configuration file '%s'", $iniConfig["remoteConfigUrl"]));
         }
         $iniConfig = Scalr_Util_Compat::parseIniString($configString, true);
         if (!$iniConfig) {
             throw new Scalr_System_Cronjob_Exception(sprintf("Cannot parse configuration file '%s'", $iniConfig["remoteConfigUrl"]));
         }
     }
     // Apply configuration. Worker configuration is already applied
     $this->config = Scalr_Util_Arrays::mergeReplaceRecursive($iniConfig, $this->config);
     foreach ($this->config as $k => $v) {
         if (property_exists($this, $k)) {
             $this->{$k} = $v;
         }
     }
     // Get nodeName from ENV, CLI options, UNIX hostname command output
     $nodeName = $_ENV[self::ENV_NONE_NAME_PROPERTY];
     if (!$nodeName) {
         if ($options && $options["getopt"]) {
             $nodeName = $options["getopt"]->getOption(self::GETOPT_NONE_NAME);
         }
     }
     if (!$nodeName) {
         $nodeName = php_uname("n");
     }
     if (!$nodeName) {
         throw new Scalr_System_Cronjob_Exception('Cannot detect current nodeName. ' . 'Use $_ENV or CLI options to setup nodeName');
     }
     $this->nodeName = $nodeName;
     $this->logger->info(sprintf("Initialize distributed cronjob (nodeName: %s, quorum: %d, distributedConfig: %s)", $this->nodeName, $this->config["quorum"], $configFileName));
     // Create elector
     $electorCls = $this->config["electorCls"];
     if (!$electorCls) {
         $electorCls = "Scalr_System_Cronjob_Distributed_DefaultElector";
     }
     $this->logger->info("Set elector: {$electorCls}");
     $this->elector = new $electorCls($this->nodeName, $this->config);
     // ZOO
     $this->jobZPath = "{$this->config["jobsZPath"]}/{$this->jobName}";
     $this->zookeeper = new Scalr_Service_Zookeeper($this->config["zookeeper"]);
     $this->nodeRegistry = new Scalr_System_Cronjob_Distributed_NodeRegistry(array("zookeeper" => $this->zookeeper, "path" => "{$this->jobZPath}/nodes", "node" => $this->nodeName));
     $this->leaderElection = new Scalr_Service_Zookeeper_Election(array("zookeeper" => $this->zookeeper, "path" => "{$this->jobZPath}/election", "timeout" => $this->electionTimeout, "quorum" => $this->quorum));
     $this->returnedNodesQueue = new Scalr_Service_Zookeeper_Queue(array("zookeeper" => $this->zookeeper, "path" => "{$this->jobZPath}/returned-queue"));
     // Work queue
     $this->globalWorkQueue = new Scalr_Service_Zookeeper_Queue(array("zookeeper" => $this->zookeeper, "path" => $this->jobZPath . "/work-queue"));
     // Local queue
     $this->config["processPool"]["workQueue"] = new Scalr_System_Ipc_ShmQueue(array("name" => "scalr.system.cronjob.multiprocess.workQueue-" . posix_getpid(), "blocking" => true, "autoInit" => true));
     // Call parent initialization
     parent::init($options);
 }