Gearman addTaskBackground完成回调不会触发

时间:2014-02-09 06:54:16

标签: php callback background-process gearman complete

我正在尝试在后台完成一些工作并将结果写入完整回调的文件结果,但它仅适用于addTask(不在后台)而不适用于addTaskBackground

有人有任何想法吗?

感谢您的帮助!     

$client = new GearmanClient();

$client->addServer('localhost');

$client->setCompleteCallback("complete");
$client->addTaskBackground('upload', 'http://youtube.com/watch?v=o3mP3mJDL2k', null, 1);
$client->addTaskBackground('upload', 'http://www.youtube.com/watch?v=SgAVnlnf8w0', null, 2);

/* in these case complete callback works
$client->addTask('upload', 'http://youtube.com/watch?v=o3mP3mJDL2k');
$client->addTask('upload', 'http://www.youtube.com/watch?v=SgAVnlnf8w0');
*/

$client->runTasks();

function complete($task){
    file_put_contents('/home/vonica/public_html/test/tmp/1.txt', $task->unique() . ", " . $task->data() . "\n", FILE_APPEND);
}

$worker = new GearmanWorker();
$worker->addServer('localhost');

$worker->addFunction('upload', 'uploader');

while($worker->work()){
    if ($worker->returnCode() != GEARMAN_SUCCESS) {
        echo "ret code: " . $worker->returnCode() . "\n";
        break;
    }
};

function uploader($job){

   $content = $job->workload();
   exec ('echo $( youtube-dl -f worst "'.$content.'")');
   return $content;
}

1 个答案:

答案 0 :(得分:4)

后台任务完成时不会调用

CompleteCallback。如果要检查后台作业的使用状态GearmanClient::jobStatus

Client.php

// save this $job_handle somewhere
$job_handle = $client->doBackground('upload', 'http://youtube.com/watch?v=o3mP3mJDL2k', null, 1);

Status.php

// use previously saved job handle to check job's status
$job_handle = $_GET['job_handle'];
$stat = $client->jobStatus($job_handle);
echo "Running: " . 
     ($stat[1] ? "true" : "false") . 
     ", numerator: " . 
     $stat[2] . 
     ", denomintor: " . 
     $stat[3] . "\n";

了解更多here