2013-06-18 56 views
0

看來setCompleteCallback並不適用於我。我做錯了什麼?謝謝。Gearman setCompleteCallback不起作用

<?php 
    # Create our client object. 
    $client = new GearmanClient(); 

    # Add default server (localhost). 
    $client->addServer("127.0.0.1", 4730); 

    echo "Sending job\n"; 

    $client->addTask("reverse", "Hello!", null, "1"); 
    $client->addTask("reverse", "Hello!", null, "2"); 
    $client->setCompleteCallback("complete"); 
    $client->runTasks(); 

    function complete($task) 
    { 
     print "Выполнено: " . $task->unique() . ", " . $task->data() . "\n"; 
    } 

    ?> 

編輯:

<?php 
$worker = new GearmanWorker(); 
$worker->addServer("127.0.0.1", 4730); 
$worker->addFunction("reverse", "reverse_fn"); 
while (1) { 
    $ret = $worker->work(); 
    if ($worker->returnCode() != GEARMAN_SUCCESS) 
     break; 
} 
function reverse_fn($job) 
{ 
    $workload = $job->workload(); 
    sleep(5); 
    $result = strrev($workload); 
    $job->sendComplete($result); 
    return $result; 
} 
?> 

這是我的工作人員。仍然不起作用。也許我需要別的東西?

+0

請給你看看工人腳本。 –

+0

好吧,我的工作人員在後 – sandra1n

回答

3

您應該在添加任務之前設置完整的回調。

$client->setCompleteCallback("complete"); 
$client->addTask("reverse", "Hello!", null, "1"); 
$client->addTask("reverse", "Hello!", null, "2"); 
+0

你救了我的一天! – sandra1n