2015-09-11 2 views
0

J'ai ce code simple:Tâches gearman Callback

<?php 
$client = new GearmanClient(); 

// Add a server 
$client->addServer(); // by default host/port will be "localhost" & 4730 

echo "Sending job\n"; 

// Send job 

$data = file_get_contents('test.html'); 
$client->addTask("convert", $data,null,1); 
$client->setCompleteCallback("complete"); 
$client->runTasks(); 
if (! $client->runTasks()) 
{ 
    echo "ERROR " . $client->error() . "\n"; 
    exit; 
} 
else 
{ 

} 

function complete($task) { 
    echo "Success: $task->unique()\n"; 
    echo($task->data()); 
} 

?> 

et un travailleur:

<?php 

// Create our worker object 
$worker = new GearmanWorker(); 

// Add a server (again, same defaults apply as a worker) 
$worker->addServer(); 

// Inform the server that this worker can process "reverse" function calls 
$worker->addFunction("convert", "convertToPDF"); 


while (1) { 
    print "Waiting for job...\n"; 
    $ret = $worker->work(); // work() will block execution until a job is delivered 
    if ($worker->returnCode() != GEARMAN_SUCCESS) { 
    break; 
    } 
} 

function convertToPDF(GearmanJob $job) { 
    $workload = $job->workload(); 
    $fd = fopen("temp.html", 'wr'); 

    fwrite($fd,$workload); 
    exec('wkhtmltopdf temp.html test.pdf'); 
    $job->sendData(file_get_contents('test.pdf')); 
    return file_get_contents('test.pdf'); 

} 
?> 

Hower, lorsque le travailleur se termine, je ne reçois pas enything dans le client. Si j'utilise des travaux au lieu de tâche, je peux obtenir les résultats (c'est-à-dire le fichier pdf). Pourquoi?

Répondre

1

Vous devez définir un rappel complet avant d'ajouter des tâches.

// Send job 

$data = file_get_contents('test.html'); 
$client->setCompleteCallback("complete"); 
$client->addTask("convert", $data,null,1); 
$client->runTasks(); 

et

exec('wkhtmltopdf temp.html test.pdf'); 
$job->sendComplete(file_get_contents('test.pdf')); 
return GEARMAN_SUCCESS;