/**
  * 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();
 }
 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()) {
     }
 }
Example #3
0
 /**
  * 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();
 }
Example #4
0
 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()) {
     }
 }
 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;
 }
 public function actionSuc()
 {
     $worker = new \GearmanWorker();
     $worker->addServers($this->module->gman_server);
     $worker->addFunction('ebidlh_suc_work', [SucWorker::className(), 'work']);
     while ($worker->work()) {
     }
 }
Example #7
0
 /**
  * @return GearmanWorker
  */
 public function worker()
 {
     if (!$this->worker) {
         $this->worker = $this->setServers(new \GearmanWorker());
     }
     $this->worker->addOptions(GEARMAN_WORKER_GRAB_UNIQ);
     return $this->worker;
 }
Example #8
0
 /**
  * 实现单例模式
  *
  * @param   array   $config
  * @return  GearmanWorker
  */
 public static function getWorker($config)
 {
     $client = new GearmanWorker();
     foreach ($config as $serverInfo) {
         $client->addServer($serverInfo['host'], $serverInfo['port']);
     }
     return $client;
 }
 /**
  * Get the GearmanWorker object
  *
  * @return GearmanWorker
  */
 public function getWorker()
 {
     if (empty($this->_worker)) {
         $this->_worker = new GearmanWorker();
         $servers = Configure::read('Gearman.servers') ?: array('127.0.0.1:4730');
         $this->_worker->addServers(implode(',', (array) $servers));
     }
     return $this->_worker;
 }
Example #10
0
 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()) {
     }
 }
 /**
  * do driver instance init
  */
 public function setup()
 {
     $settings = $this->getSettings();
     if (empty($settings)) {
         throw new BoxRouteInstanceException('init driver instance failed: empty settings');
     }
     $curInst = new \GearmanWorker();
     $curInst->addServers($settings['gearmanHosts']);
     $this->instance = $curInst;
     $this->isAvailable = $this->instance ? true : false;
 }
 /**
  * Get GearmanWorker object.
  * @param string $queueName Queue name
  * @return \GearmanWorker
  */
 protected function getWorker($queueName)
 {
     if (!isset($this->workers[$queueName])) {
         $worker = new \GearmanWorker();
         foreach ($this->servers as $server) {
             $worker->addServer($server);
         }
         $worker->addFunction($queueName, array($this, 'work'));
         $this->workers[$queueName] = $worker;
     }
     return $this->workers[$queueName];
 }
Example #13
0
 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();
     }
 }
 /**
  * Get the GearmanWorker instance
  * 
  * @return GearmanWorker
  */
 public function getWorkerConnection()
 {
     //we have a stored connection
     if (!$this->_gmWorker) {
         //Get the config, start the client object
         $_config = $this->_getConfiguration();
         $this->_gmWorker = new GearmanWorker();
         //add the servers to the client
         foreach ($_config as $_server) {
             $this->_gmWorker->addServer($_server['host'], $_server['port']);
         }
     }
     return $this->_gmWorker;
 }
 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()) {
     }
 }
Example #16
0
 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);
     }
 }
Example #17
0
 /**
  * Get GearmanWorker
  *
  * @return \GearmanWorker
  */
 public function getGearmanWorker()
 {
     if (!$this->worker) {
         $this->worker = new \GearmanWorker();
     }
     if ($this->timeout !== null) {
         $this->worker->setTimeout($this->timeout);
     }
     if ($this->functions !== null) {
         foreach ($this->functions as $f) {
             $this->register($f, array($this, 'proxify'));
         }
     }
     return $this->worker;
 }
Example #18
0
 public function run()
 {
     $w = new \GearmanWorker();
     $w->addServers($this->module->gman_server);
     $w->addFunction('kepco_work_suc2', function ($job) {
         try {
             $workload = Json::decode($job->workload());
             $this->stdout("%2%kkepco> [낙찰2] {$workload['id']} {$workload['no']} {$workload['name']}%n\n");
             $cookie = $this->module->redis_get('kepco.cookie');
             $token = $this->module->redis_get('kepco.token');
             $suc = new SucWorker2(['id' => $workload['id'], 'cookie' => $cookie, 'token' => $token]);
             $data = $suc->run();
             //print_r($data);
             $this->stdout(" > 공고번호 : {$data['notinum']}\n");
             $this->stdout(" > 예정가격 : {$data['yega']}\n");
             //$this->stdout(" > 복수예가 : {$data['multispare']}\n");
             $this->stdout(" > 1순위 : {$data['officenm1']}\n");
             $this->stdout(" > 참여수 : {$data['innum']}\n");
             $this->stdout(" > 진행상태 : {$data['bidproc']}\n");
             if (strlen($data['notinum']) < 10) {
                 return;
             }
             list($notino, $revno) = explode('-', $data['notinum']);
             if (preg_match('/^\\d{10}$/', $notino, $m)) {
                 $old_noti = substr($notino, 0, 4) . '-' . substr($notino, 4);
             } else {
                 $old_noti = substr($notino, 0, 3) . '-' . substr($notino, 3, 2) . '-' . substr($notino, 5);
             }
             $query = BidKey::find()->where(['whereis' => '03']);
             $query->andWhere("notinum like '{$old_noti}%' or notinum like '{$notino}%'");
             $bidkey = $query->orderBy('bidid desc')->limit(1)->one();
             if ($bidkey === null) {
                 return;
             }
             $data['bidid'] = $bidkey->bidid;
             $this->stdout(" > 개찰정보 저장 : {$bidkey->notinum} {$bidkey->constnm} ({$bidkey->state} {$bidkey->bidproc})\n");
             $this->module->gman_do('i2_auto_suc', Json::encode($data));
         } catch (\Exception $e) {
             $this->stdout("%r{$e}%n\n");
             \Yii::error($e, 'kepco');
         }
         $this->module->db->close();
         $this->memory_usage();
         sleep(1);
     });
     while ($w->work()) {
     }
 }
 /**
  * Runs gearman worker for mkpur
  */
 public function actionIndex()
 {
     ini_set('memory_limit', '128M');
     echo '[database connection]', PHP_EOL;
     echo '  biddb  : ' . $this->module->biddb->dsn, PHP_EOL;
     echo '  infodb : ' . $this->module->infodb->dsn, PHP_EOL;
     echo '[gearman worker]', PHP_EOL;
     echo '  server   : ' . $this->module->gman_server, PHP_EOL;
     echo '  function : "mkpur_work"', PHP_EOL;
     echo 'start worker...', PHP_EOL;
     $worker = new \GearmanWorker();
     $worker->addServers($this->module->gman_server);
     $worker->addFunction('mkpur_work', [$this, 'mkpur_work']);
     while ($worker->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;
         }
     }
 }
 /**
  * Auxiliary function to set the servers for the worker to be connected to.
  *
  * @return void
  */
 protected function _setServers()
 {
     $servers = Configure::read('Gearman.servers');
     if (empty($servers)) {
         $servers = array('127.0.0.1');
     }
     $this->_worker->addServers(implode(',', $servers));
 }
Example #22
0
 public function actionSuc()
 {
     $this->i2_gman_func = 'i2_auto_suc';
     $w = new \GearmanWorker();
     $w->addServers($this->module->gman_server);
     $w->addFunction('kepco_work_suc', function ($job) {
         try {
             $workload = Json::decode($job->workload());
             $this->stdout2("kepco> %g[worker]%n {$workload['no']} {$workload['revision']} {$workload['name']} %g{$workload['id']}%n\n");
             $cookie = $this->module->redis_get('kepco.cookie');
             $token = $this->module->redis_get('kepco.token');
             if (preg_match('/^\\d{10}$/', $workload['no'], $m)) {
                 $old_noti = substr($workload['no'], 0, 4) . '-' . substr($workload['no'], 4);
             } else {
                 $old_noti = substr($workload['no'], 0, 3) . '-' . substr($workload['no'], 3, 2) . '-' . substr($workload['no'], 5);
             }
             $notinum = $workload['no'] . '-' . $workload['revision'];
             $worker = new SucWorker(['id' => $workload['id'], 'cookie' => $cookie, 'token' => $token]);
             $data = $worker->run();
             $notinum = $data['notinum'] . '-' . $data['revision'];
             list($noti, $revision) = explode('-', $notinum);
             $bidkey = BidKey::find()->where("notinum like '{$noti}%' or notinum like '{$old_noti}%'")->andWhere(['whereis' => '03'])->orderBy('bidid desc')->limit(1)->one();
             if ($bidkey === null) {
                 return;
             }
             if ($data['bidproc'] === null) {
                 return;
             }
             $this->stdout2(" %yNEW%n\n");
             $data['notinum'] = $notinum;
             $data['constnm'] = $bidkey['constnm'];
             $data['bidid'] = $bidkey['bidid'];
             $this->stdout2("%g > do {$this->i2_gman_func} {$data['bidid']} {$data['bidproc']}%n\n");
             $this->module->gman_do($this->i2_gman_func, Json::encode($data));
         } catch (\Exception $e) {
             $this->stdout2("%r{$e}%n\n");
             \Yii::error($e, 'kepco');
         }
         $this->module->db->close();
         $this->stdout2(sprintf("[%s] Peak memory usage: %sMb\n", date('Y-m-d H:i:s'), memory_get_peak_usage(true) / 1024 / 1024), Console::FG_GREY);
         sleep(1);
     });
     while ($w->work()) {
     }
 }
Example #23
0
 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);
         }
     }
 }
 public function work(\GearmanWorker $worker)
 {
     $worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
     $getWorkAttempts = 0;
     do {
         $workRes = $worker->work();
         $getWorkAttempts++;
         $this->logger->debug("Get work attempt res: " . serialize($workRes));
         $this->logger->debug("Attempts number: " . serialize($getWorkAttempts));
         sleep(1);
         if ($workRes) {
             $this->taskWasGot = true;
         }
     } while ($workRes === false && $getWorkAttempts < WorkersConstants::MAX_GET_TASK_ATTEMPTS);
     if ($worker->returnCode() != GEARMAN_SUCCESS) {
         $this->logger->error("Not correct Gearman worker execution:" . $worker->returnCode());
     }
     return null;
 }
Example #25
0
 public function __construct($function, Logger $logger = null)
 {
     $this->function = $function;
     $this->mongo = new \MongoClient(Config::$get->database->mongodb);
     if (!$logger) {
         $this->logger = new DefaultLogger('worker');
     } else {
         $this->logger = $logger;
     }
     parent::__construct();
 }
Example #26
0
 /**
  * The Gearman Worker processes requests passed to it from a Gearman
  * server. This class should be invoked as a daemon using the CLI
  * interface. Multiple instances can be created to handle multiple requests
  * asynchronously.
  * 
  *      // Initialize the Gearman Worker (in bootstrap)
  *      Request_Async_Gearman::worker();
  *      exit(0);
  * 
  * To create a daemon script, run the following command from your command
  * line.
  * 
  *      php /path/to/index.php
  *
  * @return  void
  */
 public static function worker()
 {
     $worker = new GearmanWorker();
     $worker->addServer();
     $worker->addFunction('request_async', array('Request_Async_Gearman', 'execute_request'), Request_Async_Gearman::$context);
     echo Request_Async_Gearman::$context . ': Starting worker.' . "\n";
     while ($worker->work() or $worker->returnCode() == GEARMAN_IO_WAIT or $worker->returnCode() == GEARMAN_NO_JOBS) {
         if ($worker->returnCode() == GEARMAN_SUCCESS) {
             continue;
         }
         echo Request_Async_Gearman::$context . ': Waiting for next job...' . "\n";
         if (!$worker->wait()) {
             if ($worker->returnCode() == GEARMAN_NO_ACTIVE_FDS) {
                 usleep(100);
                 continue;
             }
         }
         break;
     }
     echo Request_Async_Gearman::$context . ': Stopping worker.' . "\n";
     echo Request_Async_Gearman::$context . ': Worker error' . $worker->error() . "\n";
 }
 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;
         }
     }
 }
    public function actionSendPasswordEmail()
    {
        $worker = new GearmanWorker();
        $worker->addServer();
        $worker->addFunction("sendPasswordEmail", function (GearmanJob $job) {
            $workload = json_decode($job->workload(), true);
            $addresses = $workload['email'];
            $id = $workload['id'];
            $token = $workload['token'];
            Yii::log(json_encode(array('token' => $token)), 'info');
            $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'union.zhantai.com';
            $link = "http://{$host}/admin/login/setPassword/id/{$id}/token/{$token}";
            $message = <<<EOT
            <p><b>亲爱的用户您好!</b></p>
            <p>这是一封铭智华通广告联盟密码设置邮件,<a target="_blank" href="{$link}">点击设置密码</a></p>
            <p>如果上述文字点击无效,请把下面网页地址复制到浏览器地址栏中打开:</p>
            <p><a target="_blank" href="{$link}">{$link}</a></p>
EOT;
            $subject = '铭智广告联盟密码设置邮件';
            return Yii::app()->mailer->send($addresses, $subject, $message);
        });
        while ($worker->work()) {
        }
    }
 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;
 }
Example #30
0
	public function action_index()
	{
		// Purge and terminate ob
		while (ob_get_level()) ob_end_flush();

		# Create our worker object.
		$gearman_mworker= new GearmanWorker;

		# Add default server (localhost).
		$gearman_mworker->addServer();

		# Register function "reverse" with the server. Change the worker function to
		# "reverse_fn_fast" for a faster worker with no output.
		$gearman_mworker->addFunction("make_request", array($this, "worker"));

		while($gearman_mworker->work())
		{
			if ($gearman_mworker->returnCode() != GEARMAN_SUCCESS)
			{
				echo "return_code: " . $gearman_mworker->returnCode() . "\n";
				break;
			}
		}
	}