【问题标题】:Gearman addTaskBackground complete callback does not fireGearman addTaskBackground 完成回调不触发
【发布时间】:2014-02-09 06:54:16
【问题描述】:

我正在尝试在后台运行一些作业并将结果写入完成回调的文件结果,但它仅适用于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;
}

【问题讨论】:

    标签: php callback background-process gearman


    【解决方案1】:

    CompleteCallback 不会在后台任务完成时被调用。如果要检查后台作业的状态,请使用GearmanClient::jobStatus

    客户端.php

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

    状态.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

    【讨论】:

      猜你喜欢
      • 2016-11-15
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多