Exemplo n.º 1
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());
     }
 }
Exemplo n.º 2
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');
 }
$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)
{
Exemplo n.º 4
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";
}
?>
 
Exemplo n.º 5
0
$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";
}
 *
 * 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();
$data['src'] = $_SERVER['argv'][1];
$data['dest'] = "small_" . $_SERVER['argv'][1];
$data['x'] = 200;
$data['y'] = NULL;
/* run reverse client */
do {
    $value = $gmc->do("shrink_image", serialize($data));
    switch ($gmc->returnCode()) {
        case GEARMAN_WORK_DATA:
            echo "DATA: {$value}\n";
            break;
        case GEARMAN_SUCCESS:
            echo "SUCCESS: {$value}\n";
            break;
        case GEARMAN_WORK_STATUS:
            list($numerator, $denominator) = $gmc->doStatus();
            echo "Status: {$numerator}/{$denominator}\n";
            break;
        default:
            echo "ERR: " . $gmc->error() . "\n";
    }
} while ($gmc->returnCode() != GEARMAN_SUCCESS);
echo "DONE: {$value}\n";
Exemplo n.º 7
0
<?php

if (empty($_REQUEST['city'])) {
    error('city is a required parameter');
    exit;
}
$gearman = new \GearmanClient();
$gearman->addServer("gearmand", "4730");
$gearman->setTimeout(1 * 500000);
$temp = $gearman->doNormal('getTemp', json_encode(array('city' => $_REQUEST['city'])));
if (!$temp) {
    error($gearman->error());
} else {
    jsonify(array('temp' => $temp));
}
function error($message)
{
    jsonify(array('error' => $message), 500);
}
function jsonify($params, $status = 200)
{
    header(' ', true, $status);
    header('Content-Type: application/json');
    print json_encode($params);
}
Exemplo n.º 8
-1
 /**
  * Handles exceptions thrown by Kohana and Gearman client, setting the
  * errors array as appropriate
  *
  * @param GearmanTask $task 
  * @param Exception $exception 
  * @return void
  * @author Sam de Freyssinet
  */
 protected function _task_exception(GearmanTask $task, Exception $exception = NULL)
 {
     $uuid = $task->unique();
     $error = $exception === NULL;
     $this->errors[$uuid] = array('type' => $error ? 'error' : 'exception', 'errorNo' => $error ? $this->_gearman_client->getErrno() : $exception->getCode(), 'error' => $error ? $this->_gearman_client->error() : $exception);
 }