private function addTasks() { static $unique = 0; $tasksToAdd = $this->maxTasks - count($this->tasks); for ($i = 0; $i < $tasksToAdd; $i++) { $workload = call_user_func($this->workloadCallback); if ($workload === false) { return false; } $task = $this->gc->addTask($this->functionName, (string) $workload, null, $unique); $this->tasks[$unique] = $task; $unique++; } return $tasksToAdd; }
/** * 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(); } }
/** * @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.'); }
/* set a few callbacks */ $gmc->setCreatedCallback("thumb_created"); $gmc->setCompleteCallback("thumb_complete"); $gmc->setFailCallback("thumb_fail"); for ($x = 0; $x < 20; $x++) { $data[$x]['src'] = $_SERVER['argv'][1]; $data[$x]['dest'] = "{$x}.jpg"; $data[$x]['x'] = (80 + 1) * ($x + 1); $data[$x]['y'] = NULL; } /* fire off each job */ foreach ($data as $img) { /* NOTE: if you want to asynchronously queue jobs use ** $task= $gmc->add_task_background("shrink_image", serialize($img)); ** however keep in mind that your complete callback will not get called */ if (!$gmc->addTask("shrink_image", serialize($img))) { echo "ERROR RET: " . $gmc->error() . "\n"; exit; } } if (!$gmc->runTasks()) { echo "ERROR RET:" . $gmc->error() . "\n"; exit; } echo "DONE\n"; exit; function thumb_created($task) { echo "CREATED -> job: " . $task->jobHandle() . "\n"; } function thumb_complete($task)
<?php # The client script # create our gearman client $gmc = new GearmanClient(); # add the default job server $gmc->addServer(); # set a couple of callbacks so we can track progress $gmc->setCompleteCallback("reverse_complete"); $gmc->setStatusCallback("reverse_status"); # add a task for the "reverse" function $task = $gmc->addTask("reverse", "Hello World!", null, "1"); # add another task, but this one to run in the background $task = $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2"); if (!$gmc->runTasks()) { echo "ERROR " . $gmc->error() . "\n"; exit; } echo "DONE\n"; function reverse_status($task) { echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . "/" . $task->taskDenominator() . "\n"; } function reverse_complete($task) { echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n"; } ?>
<?php $pdo = new \PDO('mysql:dbname=app1;host=mysql;port:3307', 'root', 'root'); $result = $pdo->query('SELECT 1;'); print_r($result); $redis = new Redis(); $redis->connect('redis', '6379'); $redis->set("test", "aaa"); print_r($redis->get("test")); $gearman = new \GearmanClient(); $gearman->addServer("gearman"); $gearman->addTask("test", json_encode(['test' => 'test'])); $gearmanWorker = new \GearmanWorker(); $gearmanWorker->addFunction('test', function (\GearmanJob $job) { print_r($job->workload()); });
public function addTask($name, $params = '', &$context = null, $unique = null) { $method = self::GEARMAN_METHOD_ADDTASK; $params = serialize($params); $contextReference = array('context' => &$context); $unique = $this->generateUniqueKey($name, $params, $unique, $method); $this->gearmanClient->addTask($name, $params, $contextReference, $unique); return $unique; }
<?php $gmc = new GearmanClient(); $gmc->addServer(); $gmc->setCreatedCallback("reverse_created"); $gmc->setStatusCallback("reverse_status"); $gmc->setCompleteCallback("reverse_complete"); $gmc->setFailCallback("reverse_fail"); $task = $gmc->addTask("reverse", "this is a test", NULL); if (!$gmc->runTasks()) { echo "ERROR " . $gmc->error() . "\n"; exit; } echo "DONE\n"; function reverse_created($task) { echo "CREATED: " . $task->jobHandle() . "\n"; } function reverse_status($task) { echo "STATUS: " . $task->jobHandle() . " - " . $task->taskNumerator() . "/" . $task->taskDenominator() . "\n"; } function reverse_complete($task) { echo "COMPLETE: " . $task->jobHandle() . "\n"; } function reverse_fail($task) { echo "FAILED: " . $task->jobHandle() . "\n"; }
$tt1 = microtime(true); $ctg = new data(array('server' => '10.102.1.3', 'user' => 'sa', 'pass' => 'i3kygbb2', 'database' => 'sifinca', 'engine' => 'mssql')); $inmueble = new inmuebles($ctg); echo " Cargando inmuebles ->"; $inmuebles = $inmueble->getInmuebles(array('promocion' => 0)); echo count($inmuebles) . "\n"; $client = new GearmanClient(); //por defecto el localhost $client->addServer(); $client->setCompleteCallback("complete"); $map = array("total" => 0, "data" => null); $json = json_encode($map); // crear task $i = 0; foreach ($inmuebles as $row) { $i++; $json = json_encode(array("id" => 'C' . $row['id_inmueble'])); $job_handle = $client->addTask("delete", $json, null, $i); echo "Enviando Tasks -> {$i} \n"; } if (!$client->runTasks()) { echo "ERROR " . $client->error() . "\n"; exit; } $tt2 = microtime(true); $r = $tt2 - $tt1; echo "\n\nTiempo de " . $r . " para {$i} registros\n"; function complete($task) { print "COMPLETE: " . $task->unique() . "\n"; }
$monitor = new Monitor(); $monitor->addServer('front-1.iunait.es', 4730); $status = $monitor->getStatus(); foreach ($status as $host => $functions) { foreach ($functions as $function => $count) { if (preg_match('/control_(.*)/', $function) !== 0) { if ($count['workers']) { $control[] = $function; } } } } # Crea el cliente gearman $gmc = new GearmanClient(); # Añade el servidor de trabajos por defecto $gmc->addServer('front-1.iunait.es', 4730); # Establece la llamada de retorno para cuando el trabajo ha terminado $gmc->setCompleteCallback("reverse_complete"); foreach ($control as $function) { $task = $gmc->addTask($function, json_encode(['command' => 'shutdown']), null, "1"); } # Añade tareas, una de ellas de baja prioridad if (!$gmc->runTasks()) { echo "ERROR " . $gmc->error() . "\n"; exit; } echo "DONE\n"; function reverse_complete($task) { echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n"; }
public function addTask($function_name, $workload, &$context = null, $unique = null) { $function_name = $this->_processFunctionName($function_name); return parent::addTask($function_name, $workload, $context, $unique); }
<?php $client = new GearmanClient(); $client->addServer('127.0.0.1', 4730); //本机可以直接addServer(),默认服务器端使用4730端口 $client->setCompleteCallback('completeCallBack'); //先绑定才有效 $result1 = $client->do('say', 'do'); //do是同步进行,进行处理并返回处理结果。 $result2 = $client->doBackground('say', 'doBackground'); //异步进行,只返回处理句柄。 $result3 = $client->addTask('say', 'addTask'); //添加任务到队列,同步进行?通过添加task可以设置回调函数。 $result4 = $client->addTaskBackground('say', 'addTaskBackground'); //添加后台任务到队列,异步进行? $client->runTasks(); //运行队列中的任务,只是do系列不需要runTask()。 echo 'result1:'; var_dump($result1); echo '<br/>'; echo 'result2:'; var_dump($result2); echo '<br/>'; echo 'result3:'; var_dump($result3); echo '<br/>'; echo 'result4:'; var_dump($result4); echo '<br/>'; //绑定回调函数,只对addTask有效 function completeCallBack($task)
<?php $client = new GearmanClient(); $client->addServer(); $client->setCompleteCallback('completeCallBack'); $client->addTask('send mail', 'woshi shui'); $client->runTasks(); //运行队列中的任务,只是do系列不需要runTask()。 //绑定回调函数,只对addTask有效 function completeCallBack($task, $context = '') { echo PHP_EOL . $context . 'CompleteCallback!handle result:' . print_r($task->data(), true) . PHP_EOL; }