Ejemplo n.º 1
0
 public function init()
 {
     // Only use gearman implementation when WP_GEARS is defined and true
     if (!defined('WP_GEARS') || !WP_GEARS) {
         return false;
     }
     global $gearman_servers;
     if (!class_exists('GearmanClient') || !class_exists('GearmanWorker')) {
         return false;
     }
     if (defined('DOING_ASYNC') && DOING_ASYNC) {
         $this->_worker = new GearmanWorker();
         $this->_client = new GearmanClient();
         if (empty($gearman_servers)) {
             $this->_client->addServer();
             return $this->_worker->addServer();
         } else {
             $this->_client->addServers(implode(',', $gearman_servers));
             return $this->_worker->addServers(implode(',', $gearman_servers));
         }
     } else {
         $this->_client = new GearmanClient();
         if (empty($gearman_servers)) {
             $this->_client->addServer();
         } else {
             $this->_client->addServers(implode(',', $gearman_servers));
         }
         // Supressing errors, because this will return true or false, depending on if we could connect & communicate
         return @$this->_client->ping('test');
     }
 }
Ejemplo n.º 2
0
 /**
  * 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;
 }
Ejemplo n.º 3
0
 public function init()
 {
     $this->client = new \GearmanClient();
     $this->worker = new \GearmanWorker();
     if (empty($this->servers)) {
         $this->servers = ['127.0.0.1:4730'];
     }
     $this->client->addServers(implode(',', $this->servers));
     $this->worker->addServers(implode(',', $this->servers));
     if (!empty($this->clientOptions)) {
         $this->client->setOptions(implode(' | ', $this->clientOptions));
     }
     if (!empty($this->workerOptions)) {
         $this->worker->setOptions(implode(' | ', $this->workerOptions));
     }
 }
Ejemplo n.º 4
0
 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()) {
     }
 }
 /**
  * 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));
 }
Ejemplo n.º 6
0
 public function actionSuc()
 {
     $worker = new \GearmanWorker();
     $worker->addServers($this->module->gman_server);
     $worker->addFunction('ebidlh_suc_work', [SucWorker::className(), 'work']);
     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;
 }
Ejemplo n.º 8
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()) {
     }
 }
 /**
  * Starts a worker for the PECL library
  *
  * @param   array   $worker_list    List of worker functions to add
  * @param   array   $timeouts       list of worker timeouts to pass to server
  * @return  void
  *
  */
 protected function start_lib_worker($worker_list, $timeouts = array())
 {
     $thisWorker = new GearmanWorker();
     $thisWorker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
     $thisWorker->setTimeout(5000);
     foreach ($this->servers as $s) {
         $this->log("Adding server {$s}", GearmanManager::LOG_LEVEL_WORKER_INFO);
         // see: https://bugs.php.net/bug.php?id=63041
         try {
             $thisWorker->addServers($s);
         } catch (\GearmanException $e) {
             if ($e->getMessage() !== 'Failed to set exception option') {
                 throw $e;
             }
         }
     }
     foreach ($worker_list as $w) {
         $timeout = isset($timeouts[$w]) ? $timeouts[$w] : null;
         $this->log("Adding job {$w} ; timeout: " . $timeout, GearmanManager::LOG_LEVEL_WORKER_INFO);
         $thisWorker->addFunction($w, array($this, "do_job"), $this, $timeout);
     }
     $start = time();
     while (!$this->stop_work) {
         if (@$thisWorker->work() || $thisWorker->returnCode() == GEARMAN_IO_WAIT || $thisWorker->returnCode() == GEARMAN_NO_JOBS) {
             if ($thisWorker->returnCode() == GEARMAN_SUCCESS) {
                 continue;
             }
             if (!@$thisWorker->wait()) {
                 if ($thisWorker->returnCode() == GEARMAN_NO_ACTIVE_FDS) {
                     sleep(5);
                 }
             }
         }
         /**
          * Check the running time of the current child. If it has
          * been too long, stop working.
          */
         if ($this->max_run_time > 0 && time() - $start > $this->max_run_time) {
             $this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
             $this->stop_work = true;
         }
         if (!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) {
             $this->log("Ran {$this->job_execution_count} jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
             $this->stop_work = true;
         }
     }
     $thisWorker->unregisterAll();
 }
Ejemplo n.º 10
0
 /**
  * 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()) {
     }
 }
Ejemplo n.º 11
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()) {
     }
 }
Ejemplo n.º 12
0
 /**
  * Starts a worker for the PECL library
  *
  * @param   array   $worker_list    List of worker functions to add
  * @return  void
  *
  */
 protected function start_lib_worker($worker_list)
 {
     $thisWorker = new GearmanWorker();
     $thisWorker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
     $thisWorker->setTimeout(5000);
     foreach ($this->servers as $s) {
         $this->log("Adding server {$s}", GearmanManager::LOG_LEVEL_WORKER_INFO);
         $thisWorker->addServers($s);
     }
     foreach ($worker_list as $w) {
         $this->log("Adding job {$w}", GearmanManager::LOG_LEVEL_WORKER_INFO);
         $thisWorker->addFunction($w, array($this, "do_job"), $this);
     }
     $start = time();
     while (!$this->stop_work) {
         if (@$thisWorker->work() || $thisWorker->returnCode() == GEARMAN_IO_WAIT || $thisWorker->returnCode() == GEARMAN_NO_JOBS) {
             if ($thisWorker->returnCode() == GEARMAN_SUCCESS) {
                 continue;
             }
             if (!@$thisWorker->wait()) {
                 if ($thisWorker->returnCode() == GEARMAN_NO_ACTIVE_FDS) {
                     sleep(5);
                 }
             }
         }
         /**
          * Check the running time of the current child. If it has
          * been too long, stop working.
          */
         if ($this->max_run_time > 0 && time() - $start > $this->max_run_time) {
             $this->log("Been running too long, exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
             $this->stop_work = true;
         }
         if (!empty($this->config["max_runs_per_worker"]) && $this->job_execution_count >= $this->config["max_runs_per_worker"]) {
             $this->log("Ran {$this->job_execution_count} jobs which is over the maximum({$this->config['max_runs_per_worker']}), exiting", GearmanManager::LOG_LEVEL_WORKER_INFO);
             $this->stop_work = true;
         }
     }
     $thisWorker->unregisterAll();
 }
Ejemplo n.º 13
0
 public function run()
 {
     $w = new \GearmanWorker();
     $w->addServers($this->module->gman_server);
     //-------------------
     //기초금액
     //-------------------
     $w->addFunction('kepco_work_basic', function ($job) {
         try {
             $workload = Json::decode($job->workload());
             $this->stdout("%4한전기초금액> {$workload['no']} {$workload['revision']} {$workload['name']}%n\n");
             $cookie = $this->module->redis_get('kepco.cookie');
             $token = $this->module->redis_get('kepco.token');
             if ($workload['purchaseType'] === 'Product') {
                 $bid = new BidWorkerPur(['id' => $workload['id'], 'cookie' => $cookie, 'token' => $token]);
             } else {
                 $bid = new BidWorker(['id' => $workload['id'], 'cookie' => $cookie, 'token' => $token]);
             }
             $data = $bid->run();
             list($noti, $revision) = explode('-', $data['notinum']);
             if (preg_match('/^\\d{10}$/', $noti, $m)) {
                 $old_noti = substr($noti, 0, 4) . '-' . substr($noti, 4);
             } else {
                 $old_noti = substr($noti, 0, 3) . '-' . substr($noti, 3, 2) . '-' . substr($noti, 5, 5);
             }
             if ($data !== null and $data['basic'] > 0) {
                 $query = BidKey::find()->where(['whereis' => '03'])->andWhere("notinum like '{$noti}%' or notinum like '{$old_noti}%'");
                 $bidkey = $query->orderBy('bidid desc')->limit(1)->one();
                 if ($bidkey !== null) {
                     if (empty($bidkey->basic) or $bidkey->basic == 0) {
                         $this->stdout(" 한전기초금액 : {$data['basic']}\n");
                         $this->module->gman_do('i2_auto_basic', ['bidid' => $bidkey->bidid, 'basic' => $data['basic']]);
                     }
                 }
             }
         } catch (\Exception $e) {
             $this->stdout("%r{$e}%n\n");
             \Yii::error($e, 'kepco');
         }
         $this->module->db->close();
     });
     //------------------
     //입찰공고
     //-----------------
     $w->addFunction('kepco_work_bid', function ($job) {
         try {
             $workload = Json::decode($job->workload());
             $this->stdout("한전입찰> [worker] {$workload['no']} {$workload['revision']} {$workload['name']} ({$workload['noticeType']})\n");
             $cookie = $this->module->redis_get('kepco.cookie');
             $token = $this->module->redis_get('kepco.token');
             if ($workload['purchaseType'] === 'Product') {
                 $bid = new BidWorkerPur(['id' => $workload['id'], 'cookie' => $cookie, 'token' => $token]);
                 $data = $bid->run();
             } else {
                 $bid = new BidWorker(['id' => $workload['id'], 'cookie' => $cookie, 'token' => $token]);
                 $data = $bid->run();
             }
             $this->stdout("%r > location code : {$data['location']}%n\n");
             print_r($data['bid_local']);
             //exit;
             if ($data !== null and $data['currencyCode'] == 'KRW') {
                 switch ($workload['noticeType']) {
                     case 'Cancel':
                         $this->bid_c($data);
                         break;
                     case 'New':
                     case 'OnceMore':
                         if ($workload['revision'] > 1) {
                             $this->bid_m($data);
                         } else {
                             if ($workload['resultState'] == 'Cancel') {
                                 $this->bid_c($data);
                             } else {
                                 $this->bid_b($data);
                             }
                         }
                         break;
                     case 'Correct':
                         $this->bid_m($data, $workload);
                         break;
                     case 'ReBidding':
                         $this->bid_r($data);
                         break;
                     default:
                         $this->stdout("%r > unknown noticeType : {$workload['noticeType']}%n\n");
                 }
                 $this->module->publish('kepco-login', 'ok');
             }
         } 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()) {
     }
 }
Ejemplo n.º 14
0
#!/usr/local/momo_album/php/bin/php 
<?php 
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set("Asia/Shanghai");
define('SCRIPT_BASE', dirname(__FILE__) . DIRECTORY_SEPARATOR);
require SCRIPT_BASE . '../global.php';
$worker = new GearmanWorker();
$worker->addServers(Core::config('job_servers'));
$thumb = new Thumb();
$worker->addFunction("thumbnail", array($thumb, 'resize'));
$worker->addFunction("rotate", array($thumb, 'rotate'));
echo "Waiting for job...\n";
while ($worker->work()) {
    if ($worker->returnCode() != GEARMAN_SUCCESS) {
        echo "return_code: " . $worker->returnCode() . "\n";
        Core::debug('gmworkerlog', "return_code: " . $worker->returnCode());
        break;
    }
}
Ejemplo n.º 15
0
 /**
  * 
  * @return \GearmanWorker
  */
 public function worker()
 {
     $gmWorker = new \GearmanWorker();
     $gmWorker->addServers(GEARMAN_SERVERS);
     return $gmWorker;
 }
Ejemplo n.º 16
0
 protected function __property_worker()
 {
     $return = new \GearmanWorker();
     $return->addServers($this->server);
     return $return;
 }
Ejemplo n.º 17
0
 static function createWorker($servers = null)
 {
     $worker = new \GearmanWorker();
     $worker->addServers($servers);
     return new Worker($worker);
 }
Ejemplo n.º 18
0
 /**
  * @inheritdoc
  */
 public function receive(callable $callback, $limit = -1)
 {
     $worker = new \GearmanWorker();
     $worker->addServers($this->dns);
     $worker->addFunction($this->id, $callback);
     /* Loop receiving and echoing back */
     $count = 0;
     while ($limit == -1 || $count < $limit) {
         if (!$worker->work()) {
             break;
         }
         $count++;
     }
 }
Ejemplo n.º 19
0
 public function actionSuc()
 {
     $w = new \GearmanWorker();
     $w->addServers($this->module->gman_server);
     $w->addFunction($this->module->i2_auto_suc, [$this, 'suc_work']);
     while ($w->work()) {
     }
 }