private function init()
 {
     if (!$this->gc) {
         $this->gc = new \GearmanClient();
         $this->gc->addServer();
         $this->gc->setCompleteCallback([$this, 'onTaskComplete']);
     }
 }
 public function initCallbacks()
 {
     $this->client->setStatusCallback([$this, 'handleStatus']);
     $this->client->setDataCallback([$this, 'handleData']);
     $this->client->setCompleteCallback([$this, 'handleComplete']);
     $this->client->setFailCallback([$this, 'handleFail']);
     $this->client->setWarningCallback([$this, 'handleWarning']);
     return null;
 }
示例#3
0
function CreateNewClient()
{
    $_client = new GearmanClient();
    $_client->addServer("192.168.201.12");
    $_client->setCompleteCallback("jobEcho");
    return $_client;
}
 /**
  * Assign all GearmanClient callbacks as Symfony2 events
  *
  * @param \GearmanClient $gearmanClient Gearman client
  *
  * @return GearmanCallbacksDispatcher self Object
  */
 public function assignTaskCallbacks(\GearmanClient $gearmanClient)
 {
     $gearmanClient->setCompleteCallback(array($this, 'assignCompleteCallback'));
     $gearmanClient->setFailCallback(array($this, 'assignFailCallback'));
     $gearmanClient->setDataCallback(array($this, 'assignDataCallback'));
     $gearmanClient->setCreatedCallback(array($this, 'assignCreatedCallback'));
     $gearmanClient->setExceptionCallback(array($this, 'assignExceptionCallback'));
     $gearmanClient->setStatusCallback(array($this, 'assignStatusCallback'));
     $gearmanClient->setWarningCallback(array($this, 'assignWarningCallback'));
     $gearmanClient->setWorkloadCallback(array($this, 'assignWorkloadCallback'));
 }
示例#5
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();
		}
	}
示例#6
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.');
 }
 * Gearman PHP Extension
 *
 * Copyright (C) 2008 James M. Luedke (jluedke@jamesluedke.com)
 *                           Eric Day (eday@oddments.org)
 * All rights reserved.
 *
 * Use and distribution licensed under the PHP license.  See
 * the LICENSE file in this directory for full text.
 */
/* create our object */
$gmc = new GearmanClient();
/* add the default server */
$gmc->addServer();
/* 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;
示例#8
0
<?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";
}
?>
 
示例#9
0
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = (require_once 'vendor/autoload.php');
$input = new SplFileObject($argv[1], 'r');
$output = new SplFileObject($argv[2], 'w');
$checked = 0;
$valid = 0;
$invalid = 0;
$started = microtime(1);
$client = new GearmanClient();
$client->addServer();
$client->setCompleteCallback(function (GearmanTask $task) use(&$output, &$checked, $started, &$valid, &$invalid) {
    $data = json_decode($task->data(), true);
    if ($data['status']) {
        $output->fwrite($data['email'] . PHP_EOL);
        $valid++;
    } else {
        $invalid++;
    }
    $checked++;
    echo "\r" . date(DATE_ATOM) . " Speed: " . $checked / (microtime(1) - $started) . " emails / sec. Valid: {$valid}, Invalid: {$invalid}.                    ";
});
$client->setExceptionCallback(function (GearmanTask $task) {
    echo "Exception!\n";
});
while (!$input->eof()) {
    //    for ($i = 0; $i < 2048; $i++) {
    $client->doBackground('check_email', trim($input->fgets()));
    //    }
    //    break;
}
//$client->runTasks();
示例#10
0
 public function setCompleteCallback(callable $callback)
 {
     return $this->gearmanClient->setCompleteCallback($callback);
 }
示例#11
0
/**
 * Functions registered with the worker
 *
 * @param GearmanJob $job
 * @return boolean
 */
function send_email($job)
{
    //Get the info of the job
    $workload = unserialize($job->workload());
    //Ensure the minimum info
    if (!array_key_exists('text', $workload) && !array_key_exists('html', $workload)) {
        echo sprintf("%s: To send an email we need at least the text or html\n", date('r'));
        $job->sendFail();
        return FALSE;
    }
    if (!array_key_exists('to', $workload) || array_key_exists('to', $workload) && empty($workload['to'])) {
        echo sprintf("%s: To send an email we need the recipient address\n", date('r'));
        $job->sendFail();
        return FALSE;
    }
    if (!array_key_exists('subject', $workload)) {
        echo sprintf("%s: To send an email we need the subject of the email\n", date('r'));
        $job->sendFail();
        return FALSE;
    }
    echo sprintf("%s: Received a task to send email to %s\n", date('r'), implode(', ', is_array($workload['to']) ? $workload['to'] : array($workload['to'])));
    $config = getConfig();
    $mail = new Zend_Mail('utf-8');
    if ($config->system->email_system->send_by_amazon_ses) {
        $transport = new App_Mail_Transport_AmazonSES(array('accessKey' => $config->amazon->aws_access_key, 'privateKey' => $config->amazon->aws_private_key));
    }
    if (array_key_exists('text', $workload)) {
        $mail->setBodyText($workload['text']);
    }
    if (array_key_exists('html', $workload)) {
        $mail->setBodyHtml($workload['html']);
    }
    if (array_key_exists('reply', $workload) && !empty($workload['reply'])) {
        $mail->setReplyTo($workload['reply']);
    }
    $mail->setFrom($config->amazon->ses->from_address, $config->amazon->ses->from_name);
    $mail->addTo($workload['to']);
    $mail->setSubject($workload['subject']);
    //Prepare gearman client
    $config = getConfig();
    $gearmanClient = new GearmanClient();
    if (!empty($config->gearman->servers)) {
        $gearmanClient->addServers($config->gearman->servers->toArray());
    } else {
        $gearmanClient->addServer();
    }
    //Add the callbacks
    $gearmanClient->setCompleteCallback('taskCompleted');
    $gearmanClient->setFailCallback('taskFailed');
    try {
        if (isset($transport) && $transport instanceof App_Mail_Transport_AmazonSES) {
            $mail->send($transport);
        } else {
            $mail->send();
        }
        //Some status info
        echo sprintf("%s: Email (%s) sent to %s\n", date('r'), $workload['subject'], implode(', ', is_array($workload['to']) ? $workload['to'] : array($workload['to'])));
        echo sprintf("%s: Task finished successfully\n\n", date('r'));
        $job->sendComplete(TRUE);
        return TRUE;
    } catch (Exception $e) {
        logError(sprintf("Error while sending an email to %s.\n\nError: %s\n", $workload['to'], $e->getMessage()));
        $job->sendFail();
        return FALSE;
    }
}
示例#12
0
<?php

require_once '../vendor/autoload.php';
use upload\lib\data;
use upload\model\inmuebles;
$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;
示例#13
0
<?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)
示例#14
0
 /**
  * Constructor method checks for gearman
  * 
  * @throws  Kohana_Request_Exception
  */
 public function __construct()
 {
     if (!extension_loaded('gearman')) {
         throw new Kohana_Request_Exception('Unable to load PHP Gearman. Check your local environment.');
     }
     // Create a new Gearman client and add a server
     $this->_gearman_client = new GearmanClient();
     /**
      * @todo    support multiple Gearman servers
      * @todo    support customisable servers
      */
     if (!$this->_gearman_client->addServer()) {
         throw new Kohana_Request_Exception('Unable to attach to Gearman server');
     }
     // Setup callback functions for Gearman tasks
     $this->_gearman_client->setDataCallback(array($this, '_task_data'));
     $this->_gearman_client->setFailCallback(array($this, '_task_failed'));
     $this->_gearman_client->setCompleteCallback(array($this, '_task_complete'));
     $this->_gearman_client->setExceptionCallback(array($this, '_task_exception'));
 }