Пример #1
0
 public function addServer($host, $port)
 {
     $start = microtime(true);
     $result = $this->client->addServer($host, $port);
     $this->logCall($start, 'addServer', array($host, $port), $result);
     return $result;
 }
Пример #2
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');
     }
 }
 private function init()
 {
     if (!$this->gc) {
         $this->gc = new \GearmanClient();
         $this->gc->addServer();
         $this->gc->setCompleteCallback([$this, 'onTaskComplete']);
     }
 }
Пример #4
0
 /**
  * Ustawienie gearman klienta
  */
 protected function _setupGearmanClient()
 {
     $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
     foreach ($config->gearman->client->server as $serverAddress) {
         try {
             self::$_client->addServer($serverAddress);
         } catch (Exception $exc) {
             throw new Exception("Gearman server on {$serverAddress} is not working!");
         }
     }
 }
Пример #5
0
 public function put($queueName, $workload)
 {
     if (null === $this->client) {
         $this->client = new \GearmanClient();
         foreach ($this->servers as $server) {
             $this->client->addServer($server);
         }
     }
     $workload = serialize($workload);
     $this->client->doBackground($queueName, $workload);
     if ($this->client->returnCode() != GEARMAN_SUCCESS) {
         throw new \RuntimeException($this->client->error());
     }
 }
 /**
  * Get the GearmanClient instance
  * 
  * @return GearmanClient
  */
 public function getClientConnection()
 {
     //we have a stored connection
     if (!$this->_gmClient) {
         //Get the config, start the client object
         $_config = $this->_getConfiguration();
         $this->_gmClient = new GearmanClient();
         //add the servers to the client
         foreach ($_config as $_server) {
             $this->_gmClient->addServer($_server['host'], $_server['port']);
         }
     }
     return $this->_gmClient;
 }
Пример #7
0
function CreateNewClient()
{
    $_client = new GearmanClient();
    $_client->addServer("192.168.201.12");
    $_client->setCompleteCallback("jobEcho");
    return $_client;
}
Пример #8
0
 /**
  * 
  * @return \Api_Component_JobServer
  */
 static function createJobServer()
 {
     $gearmanHost = new GearmanHost('localhost');
     $gearmanClient = new GearmanClient();
     $gearmanClient->addServer();
     return new Api_Component_JobServer($gearmanHost, $gearmanClient);
 }
Пример #9
0
 /**
  * Smpp search all
  * @param $startPage Start page number
  */
 public function actionStart($startPage = 1)
 {
     $gmanClient = new \GearmanClient();
     $gmanClient->addServer($this->module->gman_server);
     $httpClient = new \GuzzleHttp\Client(['base_uri' => 'http://www.smpp.go.kr']);
     $res = $httpClient->request('POST', '/cop/registcorp/selectRegistCorpListVw.do', ['form_params' => ['pageIndex' => $startPage, 'pageUnit' => '100']]);
     $body = $res->getBody();
     $html = (string) $body;
     $p = '#<a.*btnMove last.*fn_getList\\((?<lastpage>\\d+)\\);#';
     if (!preg_match($p, $html, $m)) {
         return;
     }
     $lastPage = $m['lastpage'];
     echo "총 페이지수 : {$lastPage}", PHP_EOL;
     for ($i = $startPage; $i <= $lastPage; $i++) {
         if ($i > $startPage) {
             $res = $httpClient->request('POST', '/cop/registcorp/selectRegistCorpListVw.do', ['form_params' => ['pageIndex' => $i, 'pageUnit' => '100']]);
             $body = $res->getBody();
             $html = (string) $body;
         }
         $this->parseList($html, function ($data) use($gmanClient, $i) {
             echo "page({$i}) >> " . join(',', $data), PHP_EOL;
             $gmanClient->doNormal('smpp_corp_get', Json::encode(['bizno' => $data['bizno']]));
         });
         sleep(1);
     }
 }
Пример #10
0
 public function client()
 {
     $config = $this->config->item('base_config');
     $host = $config['gearman']['host'];
     $port = $config['gearman']['port'];
     $client = new GearmanClient();
     $client->addServer($host, $port);
     $data = array('method' => 'get', 'url' => 'http://master.500mi.com/main/gearman/test', 'params' => array('wd' => '哈哈'));
     $job_handle = $client->doBackground("send_request", json_encode($data));
     if ($client->returnCode() != GEARMAN_SUCCESS) {
         echo "bad return code\n";
         exit;
     }
     $done = false;
     do {
         sleep(1);
         $stat = $client->jobStatus($job_handle);
         var_dump($stat);
         if (!$stat[0]) {
             $done = true;
         }
         echo "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n";
     } while (!$done);
     echo "done!\n";
 }
Пример #11
0
 public static function start()
 {
     $gmc = new \GearmanClient();
     $gmc->addServer("127.0.0.1", 4730);
     $data = array();
     $task = $gmc->doNormal("hunt_postman", "foo");
 }
Пример #12
0
 /**
  * 实现单例模式
  *
  * @param   array   $config
  * @return  GearmanClient
  */
 public static function getClient($config)
 {
     $worker = new GearmanClient();
     foreach ($config as $serverInfo) {
         $worker->addServer($serverInfo['host'], $serverInfo['port']);
     }
     return $worker;
 }
Пример #13
0
 public function Run()
 {
     /* create our object */
     $gmclient = new \GearmanClient();
     /* add the default server */
     $gmclient->addServer();
     /* run reverse client */
     $job_handle = $gmclient->doBackground("reverse", "this is a test");
 }
Пример #14
0
 public static function addJob($id, $function, $data)
 {
     $client = new \GearmanClient();
     $client->addServer(gearman_server, gearman_port);
     $job = new Job();
     $job->setId($id)->setExpireTime(time() + 172800)->setReference(array('CLIController', $function, $data));
     $job_handle = $client->doBackground(app_name . 'handle', serialize($job));
     Log::write(__METHOD__ . ' invoked gearman job (' . $function . ') for id ' . $id . ' ' . app_name . 'handle' . ' ' . $client->returnCode());
 }
Пример #15
0
/**
 * This function will take 3 arguments and pass it to gearman worker to store in database
*/
function queueAssignment($name, $email, $phone)
{
    $detailsArray = array('name' => $name, 'email' => $email, 'phone' => $phone);
    $detailsStr = json_encode($detailsArray);
    writeFile($detailsArray);
    // client code
    $client = new GearmanClient();
    $client->addServer();
    $store = $client->do("saveRecord", $detailsStr);
}
Пример #16
0
 /**
  * This method will instantiate the object, configure it and return it
  *
  * @return Zend_Cache_Manager
  */
 public static function getInstance()
 {
     $config = App_DI_Container::get('ConfigObject');
     $gearmanClient = new GearmanClient();
     if (!empty($config->gearman->servers)) {
         $gearmanClient->addServers($config->gearman->servers->toArray());
     } else {
         $gearmanClient->addServer();
     }
     return $gearmanClient;
 }
Пример #17
0
 /**
  * enqueue
  *
  * @param string $value
  * @return boolean
  */
 public function enqueue($value)
 {
     $gm = new \GearmanClient();
     $gm->addServer($this->host, $this->port);
     $gm->queue_name = $this->queue_name;
     if ($gm->ping('ping')) {
         $job_handle = $gmclient->doBackground($this->queue_name, json_encode($value), md5($value));
         return $this->gmclient->returnCode() != GEARMAN_SUCCESS ? false : true;
     } else {
         return false;
     }
 }
Пример #18
0
 public static function singleton()
 {
     if (is_null(self::$_instance)) {
         register_shutdown_function(array("\\Pool\\Client", shutdown));
         $client = new \GearmanClient();
         foreach (\PoolConf::$SERVERS as $server) {
             $client->addServer($server[0], $server[1]);
         }
         self::$_instance = new Client($client);
     }
     return self::$_instance;
 }
 public function createBackgroundProcess($functionName, $workload)
 {
     // client
     $client = new \GearmanClient();
     $client->addServer('127.0.0.1', 4730);
     $result = $client->doBackground($functionName, $workload);
     $this->isWorkerExist($functionName);
     // worker
     $this->worker = new \GearmanWorker();
     $this->worker->addServer('127.0.0.1', 4730);
     $this->worker->setTimeout(240000);
     return $this;
 }
Пример #20
0
 public static function send_gearman($toAddresses, $subject, $content)
 {
     if (class_exists('GearmanClient', false)) {
         $conf = (require 'conf.php');
         $confGearman = $conf['gearman'];
         $client = new GearmanClient();
         $client->addServer($confGearman['host'], $confGearman['port']);
         $data = array('toAddresses' => $toAddresses, 'subject' => $subject, 'content' => $content);
         return $client->doBackground("send_mail", serialize($data));
     } else {
         return Mail::send($toAddresses, $subject, $content);
     }
 }
Пример #21
0
	/**
	 * Divides the passed workload and handles the division of workload between
	 * multiple instances
	 */
	public function before()
	{
		// If original request and above the workable limit then we
		//  look to parallelise the work

		$m_target = $this->request->post($this->_key_name);

		if
		(
			$this->request->is_initial()
			AND isset($m_target)
			AND is_array($m_target)
			AND count($m_target) >= $this->_workable_limit
		)
		{
			// Instantiate gearman client
			$obj_gearman = new GearmanClient;
			$obj_gearman->addServer();

			// Divide the work into $this->_worker_count chunks for processing
			$int_chunk_size = round( count($m_target) / $this->_worker_count );

			$arr_chunks = array_chunk( $m_target, $int_chunk_size );

			// Reverse the route..
			$str_route = $this->request->uri();

			// Update the controller action to our own nullifier
			$this->request->action('nullifier');

			// Schedule each of the requests
			$c = 0;
			foreach ($arr_chunks as $chunk) {

				// Format the string to be passed to the worker by formatting the post
				$arr_d = $_POST;
				$arr_d[$this->_key_name] = $arr_chunks[$c];

				$str_data = $str_route . "#" . http_build_query($arr_d);

				$obj_gearman->addTask('make_request', $str_data);
				$c++;
			}

			// Set the complete requests callback
			$obj_gearman->setCompleteCallback(array($this,"complete"));

			// Execute the requests
			$obj_gearman->runTasks();
		}
	}
Пример #22
0
function new_fork($type, $number_operations, $data, $account_code)
{
    global $mysqli;
    $fork_encrypt_key = md5('huls0fjhslsshskslgjbtqcwijnbxhl2391');
    $token = substr(str_shuffle(md5(time()) . rand() . str_shuffle('qwertyuiopasdfghjjklmnbvcxzQWERTYUIOPKJHGFDSAZXCVBNM1234567890')), 0, 64);
    $sql = sprintf("insert into `Fork Dimension`  (`Fork Process Data`,`Fork Token`,`Fork Type`,`Fork Operations Total Operations`) values (%s,%s,%s,%d)  ", prepare_mysql(json_encode($data)), prepare_mysql($token), prepare_mysql($type), $number_operations);
    $salt = md5(rand());
    $mysqli->query($sql);
    $fork_key = $mysqli->insert_id;
    $fork_metadata = base64_encode(AESEncryptCtr(json_encode(array('code' => addslashes($account_code), 'token' => $token, 'fork_key' => $fork_key, 'salt' => $salt)), $fork_encrypt_key, 256));
    $client = new GearmanClient();
    $client->addServer('127.0.0.1');
    $msg = $client->doBackground($type, $fork_metadata);
    return array($fork_key, $msg);
}
Пример #23
0
 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');
 }
Пример #24
0
 function queueSMS($msgdata)
 {
     if (isset($msgdata['from'])) {
         $this->from = $msgdata['from'];
     }
     $workload['msgdata']['from'] = $this->from;
     $workload['msgdata']['to'] = $msgdata['to'];
     $workload['msgdata']['msg'] = $msgdata['msg'];
     $workload['provider'] = get_class($this);
     $workload['api_key'] = $this->api_key;
     $workload['api_secret'] = $this->api_secret;
     if ($this->extra != null) {
         $workload['extra'] = $this->extra;
     }
     $client = new \GearmanClient();
     $client->addServer();
     $job_handle = $client->doBackground("sendSMS", json_encode($workload));
     return $job_handle;
 }
Пример #25
0
 function save($currpass, $newpass)
 {
     if (extension_loaded('gearman')) {
         $rcmail = rcmail::get_instance();
         $user = $_SESSION['username'];
         $payload = array('username' => $user, 'oldPassword' => $currpass, 'newPassword' => $newpass);
         $gmc = new GearmanClient();
         $gmc->addServer($rcmail->config->get('password_gearman_host'));
         $result = $gmc->doNormal('setPassword', json_encode($payload));
         $success = json_decode($result);
         if ($success && $success->result == 1) {
             return PASSWORD_SUCCESS;
         } else {
             rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Password plugin: Gearman authentication failed for user {$user}: {$error}"), true, false);
         }
     } else {
         rcube::raise_error(array('code' => 600, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Password plugin: PECL Gearman module not loaded"), true, false);
     }
     return PASSWORD_ERROR;
 }
Пример #26
0
function Action3()
{
    $client = new GearmanClient();
    $client->addServer("115.28.54.237", 4730);
    $i = 0;
    global $db;
    global $sites;
    foreach ($sites as $siteid => $site) {
        $dbbooks = $db->get_books($siteid);
        print_r("[{$siteid}] db-book count: " . count($dbbooks) . "\n");
        foreach ($dbbooks as $bookid => $dbbook) {
            $dbchapters = $db->get_chapters($siteid, $bookid);
            print_r("[{$siteid}] db-chapter count: " . count($dbchapters) . "\n");
            foreach ($dbchapters as $chapterid => $dbchapter) {
                print_r("Add task({$bookid}, {$chapterid})\n");
                $workload = sprintf("%s,%d", $bookid, $chapterid);
                $client->doBackground('comic-bengou', $workload, $workload);
            }
        }
    }
}
Пример #27
0
 public static function getFiles()
 {
     if (!empty($_FILES)) {
         foreach ($_FILES["file"]["error"] as $key => $error) {
             if ($error == UPLOAD_ERR_OK) {
                 if (!file_exists(self::$uploads_dir)) {
                     mkdir(self::$uploads_dir);
                 }
                 $tmp_name = $_FILES["file"]["tmp_name"][$key];
                 $name = $_FILES["file"]["name"][$key];
                 move_uploaded_file($tmp_name, self::$uploads_dir . $name);
                 $excel = new SimpleExcel('csv');
                 $excel->parser->loadFile(self::$uploads_dir . $name);
                 $data = $excel->parser->getField();
                 unset($data[0]);
                 $client = new GearmanClient();
                 $client->addServer();
                 $client->doBackground('q' . $key, serialize($data));
             }
         }
     }
 }
Пример #28
0
 /**
  * @group concurrency
  */
 public function testConcurrency()
 {
     if (!class_exists('GearmanClient', false)) {
         $this->markTestSkipped('pecl/gearman is required for this test to run.');
     }
     $client = new \GearmanClient();
     $client->addServer();
     $workerIds = [];
     $poppedItems = [];
     $client->setCompleteCallback(function (\GearmanTask $task) use(&$workerIds, &$poppedItems) {
         $data = explode(':', $task->data(), 2);
         if (!is_array($data) || 2 !== count($data)) {
             return;
         }
         list($workerId, $item) = $data;
         $workerIds[$workerId] = true;
         if (!isset($poppedItems[$item])) {
             $poppedItems[$item] = true;
         }
     });
     $queueSize = $this->getConcurrencyQueueSize();
     $this->assertGreaterThan(10, $queueSize, 'Queue size is too small to test concurrency.');
     $workload = serialize(self::getHandler());
     for ($i = 1; $i <= $queueSize; $i++) {
         $this->queue->push($i);
         $client->addTask('pop', $workload);
     }
     try {
         // run the tasks in parallel (assuming multiple workers)
         $result = $client->runTasks();
     } catch (\GearmanException $e) {
         $result = false;
     }
     if (!$result) {
         $this->markTestSkipped('Unable to run gearman tasks. Check if gearman server is running.');
     }
     $this->assertEquals($queueSize, count($poppedItems));
     $this->assertGreaterThan(1, count($workerIds), 'Not enough workers to test concurrency.');
 }
Пример #29
0
function Action3()
{
    $client = new GearmanClient();
    $client->addServer("115.28.54.237", 4730);
    $i = 0;
    global $db;
    $dbbooks = $db->get_books(CYSTS8::$siteid);
    foreach ($dbbooks as $bookid => $dbbook) {
        $dbchapters = $db->get_chapters(CYSTS8::$siteid, $bookid);
        foreach ($dbchapters as $chapterid => $dbchapter) {
            $uri = $dbchapter["uri"];
            $uri2 = $dbchapter["uri2"];
            if (strlen($uri) > 1 || strlen($uri2) > 1) {
                continue;
            }
            ++$i;
            print_r("[{$i}]Add task({$bookid}, {$chapterid})\n");
            $workload = sprintf("%s,%d", $bookid, $chapterid);
            $client->doBackground('DownloadYsts8', $workload, $workload);
        }
    }
}
Пример #30
0
function Action3($siteid)
{
    global $db;
    $client = new GearmanClient();
    $client->addServer("115.28.54.237", 4730);
    $i = 0;
    $dbbooks = $db->get_books($siteid);
    foreach ($dbbooks as $bookid => $dbbook) {
        $dbchapters = $db->get_chapters($siteid, $bookid);
        foreach ($dbchapters as $chapterid => $dbchapter) {
            $uri = $dbchapter["uri"];
            $uri2 = $dbchapter["uri2"];
            if (strlen($uri2) > 1 || 0 == strncmp("175.195.249.184", $uri, 10)) {
                //if(strlen($uri) > 1 || strlen($uri2) > 1)
                continue;
            }
            ++$i;
            print_r("[{$i}]Add task({$bookid}, {$chapterid})\n");
            $workload = sprintf("%s,%d", $bookid, $chapterid);
            $client->doBackground('DownloadPingshu8', $workload, $workload);
        }
    }
}