/** * Get GearmanClient * * @return \GearmanClient */ public function getGearmanClient() { if (!$this->client) { $this->client = new \GearmanClient(); } if ($this->timeout !== null) { $this->client->setTimeout($this->timeout); } if ($this->context !== null) { $this->client->setContext($this->context); } return $this->client; }
public function check() { if (class_exists('\\GearmanClient')) { $client = new \GearmanClient(); $client->setTimeout($this->timeout); $client->addServer($this->host, $this->port); $mtime = microtime(true); $result = $client->doNormal($this->functionName, json_encode(array('monitor' => 'uptize'))); if ($client->returnCode() == \GEARMAN_SUCCESS) { $mtime = microtime(true) - $mtime; return new Result(true, array('time' => $mtime)); } return new Result(false, array(), $client->error()); } return new Result(false, array(), 'Class GearmanClient not found'); }
/** * Configuration application default DI * * @return FactoryDefault| CLI */ public function getDI() { if ($this->di) { return $this->di; } if ($this->appMode == 'cli') { $di = new FactoryDefault\CLI(); } else { $di = new FactoryDefault(); } //PHP5.3 not support $this in closure $self = $this; /********************************** * DI initialize for MVC core ***********************************/ //$di->set('application', $this); //call loadmodules will overwrite this $di->set('moduleManager', function () use($di) { $moduleManager = new ModuleManager(); $moduleManager->setEventsManager($di->getEventsManager()); return $moduleManager; }, true); //System global events manager $di->set('eventsManager', function () use($di) { $eventsManager = new EventsManager(); $eventsManager->enablePriorities(true); // dispatch caching event handler $eventsManager->attach("dispatch", new DispatchInterceptor(), -1); $eventsManager->enablePriorities(true); return $eventsManager; }, true); $di->set('config', function () use($self) { return $self->diConfig(); }, true); $di->set('router', function () use($self) { return $self->diRouter(); }, true); $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($di->getEventsManager()); return $dispatcher; }, true); $di->set('modelsMetadata', function () use($self) { return $self->diModelsMetadata(); }, true); $di->set('modelsManager', function () use($di) { $config = $di->getConfig(); ModelManager::setDefaultPrefix($config->dbAdapter->prefix); //for solving db master/slave under static find method $modelsManager = new ModelManager(); return $modelsManager; }); $di->set('view', function () use($di) { $view = new View(); $view->setViewsDir(__DIR__ . '/views/'); $view->setEventsManager($di->getEventsManager()); return $view; }); $di->set('session', function () use($self) { return $self->diSession(); }); $di->set('tokenStorage', function () use($self) { return $self->diTokenStorage(); }, true); /********************************** * DI initialize for database ***********************************/ $di->set('dbMaster', function () use($self) { return $self->diDbMaster(); }, true); $di->set('dbSlave', function () use($self) { return $self->diDbSlave(); }, true); $di->set('transactions', function () use($di) { $transactions = new \Phalcon\Mvc\Model\Transaction\Manager(); $transactions->setDbService('dbMaster'); return $transactions; }, true); /********************************** * DI initialize for cache ***********************************/ $di->set('globalCache', function () use($self) { return $self->diGlobalCache(); }, true); $di->set('viewCache', function () use($self) { return $self->diViewCache(); }, true); $di->set('modelsCache', function () use($self) { return $self->diModelsCache(); }, true); $di->set('apiCache', function () use($self) { return $self->diApiCache(); }, true); $di->set('fastCache', function () use($self) { return $self->diFastCache(); }, true); /********************************** * DI initialize for queue ***********************************/ $di->set('queue', function () use($di) { $config = $di->getConfig(); $client = new \GearmanClient(); $client->setTimeout(1000); foreach ($config->queue->servers as $key => $server) { $client->addServer($server->host, $server->port); } return $client; }, true); $di->set('worker', function () use($di) { $config = $di->getConfig(); $worker = new \GearmanWorker(); foreach ($config->queue->servers as $key => $server) { $worker->addServer($server->host, $server->port); } return $worker; }, true); /********************************** * DI initialize for email ***********************************/ $di->set('mailer', function () use($self) { return $self->diMailer(); }, true); $di->set('mailMessage', 'Eva\\EvaEngine\\MailMessage'); $di->set('smsSender', function () use($self) { return $self->diSmsSender(); }, true); /********************************** * DI initialize for helpers ***********************************/ $di->set('url', function () use($di) { $config = $di->getConfig(); $url = new UrlResolver(); $url->setVersionFile($config->staticBaseUriVersionFile); $url->setBaseUri($config->baseUri); $url->setStaticBaseUri($config->staticBaseUri); return $url; }, true); $di->set('escaper', 'Phalcon\\Escaper'); $di->set('tag', function () use($di, $self) { Tag::setDi($di); $self->registerViewHelpers(); return new Tag(); }); $di->set('flash', 'Phalcon\\Flash\\Session'); $di->set('placeholder', 'Eva\\EvaEngine\\View\\Helper\\Placeholder'); $di->set('cookies', function () { $cookies = new \Phalcon\Http\Response\Cookies(); $cookies->useEncryption(false); return $cookies; }); $di->set('translate', function () use($self) { return $self->diTranslate(); }); $di->set('fileSystem', function () use($self) { return $self->diFileSystem(); }); $di->set('logException', function () use($di) { $config = $di->getConfig(); return new FileLogger($config->logger->path . 'error.log'); }); if ($this->appMode == 'cli') { $this->cliDI($di); } IoC::setDI($di); return $this->di = $di; }
/** * Given a GearmanClient, set timeout * * @param \GearmanClient $gearmanClient Object to include servers * * @return GearmanClient Returns self object */ protected function assignTimeout(\GearmanClient $gearmanClient) { if ($this->timeout) { $gearmanClient->setTimeout($this->timeout); } return $this; }
<?php if (empty($_REQUEST['city'])) { error('city is a required parameter'); exit; } $gearman = new \GearmanClient(); $gearman->addServer("gearmand", "4730"); $gearman->setTimeout(1 * 500000); $temp = $gearman->doNormal('getTemp', json_encode(array('city' => $_REQUEST['city']))); if (!$temp) { error($gearman->error()); } else { jsonify(array('temp' => $temp)); } function error($message) { jsonify(array('error' => $message), 500); } function jsonify($params, $status = 200) { header(' ', true, $status); header('Content-Type: application/json'); print json_encode($params); }
public function sendMessage($phones, $content) { $client = new GearmanClient(); $client->addServer("219.232.243.98"); $client->setTimeout(2000); if ($client->timeout() > 2000) { return false; } $result = $client->doBackground("sendMessage", json_encode(array('content' => $content, 'phones' => $phones))); }
public function sendPasswordEmailGearman($id, $token, $email) { $client = new GearmanClient(); $client->addServer(); $client->setTimeout(2000); if ($client->timeout() > 2000) { return false; } $result = $client->doBackground("sendPasswordEmail", json_encode(array('id' => $id, 'email' => $email, 'token' => $token))); }
/** * * 图片旋转接口 * GET rotate/:id.json?direction={方向}&size={尺寸} */ public function rotate($pid) { $direction = intval($_GET['direction']); $size = intval($_GET['size']); $size = in_array($size, Core::config('photo_standard_type')) ? $size : 130; if ($direction != -1) { $direction = 1; } if (!$pid) { $this->response(ResponseType::ERROR_LACKPARAMS); } $client = new GearmanClient(); $client->addServers(Core::config('job_servers')); $client->setTimeout(3000); $result = @$client->doHigh("rotate", serialize(array($pid, $direction))); $result && ($sid = @unserialize($result)); if (!$sid) { $thumb = new Thumb(); if ($thumb->rotate(NULL, $pid, $direction)) { $sid = TRUE; } } if ($sid) { $photoModel = new Models\Photo(); $imgurls = $photoModel->geturi($pid, $size); $return['src'] = $imgurls[0]; $this->response(ResponseType::PHOTO_ROTATE_OK, '', $return); } else { $this->response(ResponseType::PHOTO_ROTATE_ERROR); } }