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 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";
 }
Exemplo n.º 3
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());
 }
Exemplo n.º 4
0
 public function run($task)
 {
     $client = new GearmanClient();
     $client->addServers($task["server"]);
     $client->doBackground($task["cmd"], $task["ext"]);
     if (($code = $client->returnCode()) != GEARMAN_SUCCESS) {
         Main::log_write("Gearman:" . $task["cmd"] . " to " . $task["server"] . " error,code=" . $code);
         exit;
     }
     Main::log_write("Gearman:" . $task["cmd"] . " to " . $task["server"] . " success,code=" . $code);
     exit;
 }
Exemplo n.º 5
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');
 }
Exemplo n.º 6
0
 * All rights reserved.
 *
 * Use and distribution licensed under the PHP license.  See
 * the LICENSE file in this directory for full text.
 */
echo "Starting\n";
# Create our client object.
$gmclient = new GearmanClient();
# Add default server (localhost).
$gmclient->addServer();
echo "Sending job\n";
# Send reverse job
do {
    $result = $gmclient->do("reverse", "Hello!");
    # Check for various return packets and errors.
    switch ($gmclient->returnCode()) {
        case GEARMAN_WORK_DATA:
            echo "Data: {$result}\n";
            break;
        case GEARMAN_WORK_STATUS:
            list($numerator, $denominator) = $gmclient->doStatus();
            echo "Status: {$numerator}/{$denominator} complete\n";
            break;
        case GEARMAN_SUCCESS:
            break;
        default:
            echo "RET: " . $gmclient->returnCode() . "\n";
            exit;
    }
} while ($gmclient->returnCode() != GEARMAN_SUCCESS);
echo "Success: {$result}\n";
Exemplo n.º 7
0
<?php

$client = new GearmanClient();
$client->addServer();
$job_handle = $client->doBackground("setup", 1);
var_dump($job_handle);
if ($client->returnCode() != GEARMAN_SUCCESS) {
    echo "bad return code\n";
    exit;
}
$done = false;
do {
    sleep(3);
    $stat = $client->jobStatus($job_handle);
    if (!$stat[0]) {
        // the job is known so it is not done
        $done = true;
    }
    echo "Running: " . ($stat[1] ? "true" : "false") . ", numerator: " . $stat[2] . ", denomintor: " . $stat[3] . "\n";
} while (!$done);
echo "done!\n";
Exemplo n.º 8
0
 /**
  * Get the last Gearman return code
  *
  * @return int
  * @link http://docs.php.net/manual/en/gearmanclient.returncode.php
  */
 public function returnCode()
 {
     return $this->gearmanClient->returnCode();
 }
Exemplo n.º 9
0
function do_background_job($action, $data)
{
    // 保存数据到gearman,异步执行
    $gm = new GearmanClient();
    foreach (explode(";", $GLOBALS['AX_GEARMAN_SERVER']) as $server) {
        list($host, $port) = explode(":", $server);
        $gm->addServer($host, $port);
    }
    @$gm->doBackground($action, $data);
    if ($gm->returnCode() == GEARMAN_SUCCESS) {
        return true;
    }
    return false;
}
Exemplo n.º 10
0
 public function sendParams($newPass)
 {
     try {
         $gmclient = new GearmanClient();
         $gmclient->addServer(GEARMAN_SERVER, GEARMAN_PORT);
         $payload = serialize(array('contextid' => $this->inputs['cid'], 'userid' => $this->inputs['userid'], 'newpassword' => $newPass));
         $gmclient->doNormal(CHANGE_PASSWORD_FUNCTION, $payload);
         if ($gmclient->returnCode() == GEARMAN_SUCCESS) {
             $this->logMessage = sprintf("[INFO] Change password for uid: %s within context: %s succeeded.", $this->inputs['username'], $this->inputs['cid']);
             $this->logToFile($this->logMessage);
             exit(0);
         } else {
             if ($gmclient->returnCode() == GEARMAN_COULD_NOT_CONNECT) {
                 $this->logMessage = sprintf("[ERROR] Could not connect to Gearman Server with IP : %s:%s", GEARMAN_SERVER, GEARMAN_PORT);
                 $this->logToFile($this->logMessage);
                 exit(1);
             } else {
                 $this->logMessage = sprintf("[ERROR] Gearman Server error, error code: %s", $gmclient->returnCode());
                 $this->logToFile($this->logMessage);
                 exit(1);
             }
         }
     } catch (GearmanException $e) {
         print_r($e->getMessage() . PHP_EOL);
         exit(1);
     }
 }
 *
 * 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";
<?php

/*
 * 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();
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) {
    $job_handle[] = $gmc->doBackground("shrink_image", serialize($img));
    if ($gmc->returnCode() != GEARMAN_SUCCESS) {
        echo "ERROR RET: " . $gmc->error() . "\n";
        exit;
    }
}
echo "DONE\n";