本文整理汇总了PHP中GearmanWorker::work方法的典型用法代码示例。如果您正苦于以下问题:PHP GearmanWorker::work方法的具体用法?PHP GearmanWorker::work怎么用?PHP GearmanWorker::work使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GearmanWorker
的用法示例。
在下文中一共展示了GearmanWorker::work方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: work
public function work()
{
$this->_worker->addFunction($this->gearman_function(), array($this, 'do_job'));
$this->_worker->work();
// Killing after one job, so we don't run into unexpected behaviors or memory issues. Supervisord will respawn the php processes
die;
}
示例2: __construct
/**
* Constructor
* Checks for the required gearman extension,
* fetches the bootstrap and loads in the gearman worker
*
* @return Gearman_Worker
*/
public function __construct($bootstrap)
{
if (!extension_loaded('gearman')) {
throw new RuntimeException('The PECL::gearman extension is required.');
}
$this->_worker = $bootstrap->getWorker();
if (empty($this->_registerFunction)) {
throw new InvalidArgumentException(get_class($this) . ' must implement a registerFunction');
}
// allow for a small memory gap:
$memoryLimit = ($this->_memory + 128) * 1024 * 1024;
ini_set('memory_limit', $memoryLimit);
$this->_worker->addFunction($this->_registerFunction, array(&$this, 'work'));
$this->_worker->setTimeout($this->_timeout);
$this->init();
while ($this->_worker->work() || $this->_worker->returnCode() == GEARMAN_TIMEOUT) {
if ($this->_worker->returnCode() == GEARMAN_TIMEOUT) {
$this->timeout();
continue;
}
if ($this->_worker->returnCode() != GEARMAN_SUCCESS) {
$this->setError($this->_worker->returnCode() . ': ' . $this->_worker->getErrno() . ': ' . $this->_worker->error());
break;
}
}
$this->shutdown();
}
示例3: produce
/**
* Connects to the ZMQ inbound queue from the deferred queue server.
* Starts the gearman worker and processes the workload by calling
* the registered callback functions.
*/
public function produce()
{
$this->client->connect();
while ($this->active) {
$this->gearman->work();
}
}
示例4: work
public function work()
{
$config = $this->config->item('base_config');
$host = $config['gearman']['host'];
$port = $config['gearman']['port'];
$worker = new GearmanWorker();
$worker->addServer($host, $port);
function send_request($job)
{
var_dump("scorpio");
include_once APPPATH . 'third_party/snoopy.php';
$snoopy = new Snoopy();
$data = json_decode($job->workload());
$method = $data->method;
$url = $data->url;
$params = $data->params;
${$method} = array();
foreach ($params as $key => $value) {
${$method}[$key] = $value;
}
$snoopy->submit($url, ${$method});
$result = $snoopy->results;
return $result;
}
$worker->addFunction("send_request", "send_request");
while ($worker->work()) {
}
}
示例5: actionIndex
public function actionIndex()
{
$w = new \GearmanWorker();
$w->addServers($this->module->gman_server);
$w->addFunction('kepco_file_download', function ($job) {
$workload = Json::decode($job->workload());
$bidid = $workload['bidid'];
$attchd_lnk = $workload['attchd_lnk'];
$this->stdout("한전파일> {$bidid} \n", Console::FG_GREEN);
try {
$saveDir = "/home/info21c/data/kepco/" . substr($bidid, 0, 4) . "/{$bidid}";
@mkdir($saveDir, 0777, true);
$cookie = $this->module->redis_get('kepco.cookie');
$token = $this->module->redis_get('kepco.token');
$downinfo = explode('|', $attchd_lnk);
foreach ($downinfo as $info) {
$this->stdout(" > {$info}\n");
list($name, $url) = explode('#', $info);
$savePath = $saveDir . '/' . $name;
$cmd = "wget -q -T 30 --header 'Cookie: {$cookie}' --header \"X-CSRF-TOKEN: {$token}\" --header 'Accept-Encoding: gzip' -O - '{$url}' | gunzip > \"{$savePath}\"";
//echo $cmd,PHP_EOL;
$res = exec($cmd);
}
$this->gman_fileconv->doBackground('fileconv', $bidid);
} catch (\Exception $e) {
$this->stdout("{$e}\n", Console::FG_RED);
\Yii::error($e, 'kepco');
}
$this->stdout(sprintf("[%s] Peak memory usage: %s Mb\n", date('Y-m-d H:i:s'), memory_get_peak_usage(true) / 1024 / 1024), Console::FG_GREY);
sleep(1);
});
while ($w->work()) {
}
}
示例6: _work
/**
* Set up the GearmanWorker to run in non-blocking mode
*
* This allows you to do work "in between" jobs when its idle
*
* Events emitted:
* - Gearman.beforeWork: Called before GearmanWorker::work()
* - Gearman.afterWork: Called after GearmanWorker::work() actually processed a job
* - Gearman.beforeWait: Called before Gearman::wait() is called
* - Gearman.afterWait: Called after Gearman::wait() is called
*
* N.B: Gearman.beforeWork will be called even if there is no jobs to be processed,
* it's meant as a simple wrapper around GearmanWorker::work() and GearmanWorker::wait()
*
* All events can abort the infinite loop by calling $event->stopPropagation();
*
* @return void
*/
protected function _work()
{
$this->_worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
// Infinite loop of doom until we die!
while (true) {
if (!$this->_triggerEvent('Gearman.beforeWork')) {
break;
}
$this->_worker->work();
if (!$this->_triggerEvent('Gearman.afterWork')) {
break;
}
// If we processed a job, don't bother to wait
if ($this->_worker->returnCode() == GEARMAN_SUCCESS) {
continue;
}
if (!$this->_triggerEvent('Gearman.beforeWait')) {
break;
}
$this->_worker->wait();
if (!$this->_triggerEvent('Gearman.afterWait')) {
break;
}
}
}
示例7: __construct
/**
* Constructor
* Checks for the required gearman extension,
* fetches the bootstrap and loads in the gearman worker
*
* @param Zend_Application_Bootstrap_BootstrapAbstract $bootstrap
* @return Zend_Gearman_Worker
*/
public function __construct(Zend_Application_Bootstrap_BootstrapAbstract $bootstrap)
{
if (!extension_loaded('gearman')) {
throw new RuntimeException('The PECL::gearman extension is required.');
}
$this->_bootstrap = $bootstrap;
$this->_worker = $this->_bootstrap->bootstrap('gearmanworker')->getResource('gearmanworker');
if (empty($this->_registerFunction)) {
throw new InvalidArgumentException(get_class($this) . ' must implement a registerFunction');
}
// allow for a small memory gap:
$memoryLimit = ($this->_memory + 128) * 1024 * 1024;
ini_set('memory_limit', $memoryLimit);
$this->_worker->addFunction($this->_registerFunction, array(&$this, 'work'));
$this->_worker->setTimeout($this->_timeout);
$this->init();
$check = 10;
$c = 0;
while (@$this->_worker->work() || $this->_worker->returnCode() == GEARMAN_TIMEOUT) {
$c++;
if ($this->_worker->returnCode() == GEARMAN_TIMEOUT) {
$this->timeout();
continue;
}
if ($this->_worker->returnCode() != GEARMAN_SUCCESS) {
$this->setError($this->_worker->returnCode() . ': ' . $this->_worker->getErrno() . ': ' . $this->_worker->error());
break;
}
if ($c % $check === 0 && $this->isMemoryOverflow()) {
break;
// we've consumed our memory and the worker needs to be restarted
}
}
$this->shutdown();
}
示例8: actionSuc
public function actionSuc()
{
$worker = new \GearmanWorker();
$worker->addServers($this->module->gman_server);
$worker->addFunction('ebidlh_suc_work', [SucWorker::className(), 'work']);
while ($worker->work()) {
}
}
示例9: indexAction
public function indexAction()
{
$config = App::instance()->config;
$worker = new GearmanWorker();
$worker->addServer($config['gearman']['host'], $config['gearman']['port']);
$worker->addFunction('delete_keys', array($this, 'deleteKeys'));
$worker->addFunction('move_keys', array($this, 'moveKeys'));
while ($worker->work()) {
}
}
示例10: listen
function listen()
{
foreach ($this->getConfiguration()->getFunctions() as $functionName => $callable) {
$this->worker->addFunction($functionName, $this->wrap($callable, $functionName));
}
$this->suppressListen();
$started = time();
while ($this->worker->work()) {
if ($this->worker->returnCode() != GEARMAN_SUCCESS) {
$this->getLogger()->error('Gearman success fail with code:' . $this->worker->returnCode());
$this->terminate(SIGTERM);
}
$this->suppressListen();
$this->checkMatchedConditions();
if (time() - $started < 1) {
sleep(1);
}
}
}
示例11: GearmanWorker
function gearman_worker()
{
$gm = new GearmanWorker();
$gm->addServer();
$gm->addFunction('do_blacklist_checking', 'Monitor::do_blacklist_checking');
$gm->addFunction('process_pending_monitor', 'Monitor::process_pending_monitor');
while ($gm->work()) {
echo $gm->returnCode();
}
exit;
}
示例12: executeBgTasks
public static function executeBgTasks()
{
$files = scandir(self::$uploads_dir);
unset($files[0]);
unset($files[1]);
$jobs = array_values($files);
foreach ($jobs as $key => $singleJob) {
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('q' . $key, array(new MyClass(new GearmanJob()), 'csvHandler'));
$worker->work();
}
}
示例13: actionIndex
public function actionIndex()
{
$worker = new GearmanWorker();
$worker->addServer("219.232.243.98");
$worker->addFunction("sendMessage", function (GearmanJob $job) {
$workload = json_decode($job->workload(), true);
$phones = $workload['phones'];
$content = $workload['content'];
Yii::app()->smsClient->sendMessage($phones, $content);
return true;
});
while ($worker->work()) {
}
}
示例14: run
public static function run()
{
$lockAgent = new LockAgentWorker();
$worker = new \GearmanWorker();
$worker->addServer();
$worker->addFunction("findWithLock", array($lockAgent, "findWithLock"));
$worker->addFunction("dqlWithLock", array($lockAgent, "dqlWithLock"));
$worker->addFunction('lock', array($lockAgent, 'lock'));
while ($worker->work()) {
if ($worker->returnCode() != GEARMAN_SUCCESS) {
echo "return_code: " . $worker->returnCode() . "\n";
break;
}
}
}
示例15: run
public function run()
{
try {
$gserver = $this->_configuration->gearman->server;
$gport = $this->_configuration->gearman->port;
$worker = new GearmanWorker();
$worker->addServer($gserver, $gport);
$jobs = $this->_configuration->job;
$worker->addFunction($jobs->sync_search, array($this, "syncSearch"));
while ($worker->work()) {
}
} catch (Exception $ex) {
Log::dumpLog("Uncaught exception: " . $ex->getMessage(), Log::ERR);
}
}