代码改变世界

gearman background后台job状态获取

2014-12-09 01:11  youxin  阅读(1015)  评论(0编辑  收藏  举报

GearmanClient background job有一个方法叫:

public array GearmanClient::jobStatus ( string $job_handle )

Get the status of a background job

返回值:

An array containing status information for the job corresponding to the supplied job handle. The first array element is a boolean indicating whether the job is even known, the second is a boolean indicating whether the job is still running, and the third and fourth elements correspond to the numerator and denominator of the fractional completion percentage, respectively.

example:Monitor the status of a long running background job

<?php

/* create our object */
$gmclient= new GearmanClient();

/* add the default server */
$gmclient->addServer();

/* run reverse client */
$job_handle = $gmclient->doBackground("reverse", "this is a test");

if ($gmclient->returnCode() != GEARMAN_SUCCESS)
{
  echo "bad return code\n";
  exit;
}

$done = false;
do
{
   sleep(3);
   $stat = $gmclient->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";

?>

问题是这个函数必须获得job_handle,在php官方文档里面找了很久没找到如何获得一个job_handle.

在stackoveflow看了很久,找到一个问题回答:

 http://stackoverflow.com/questions/20563424/gearman-receive-data-of-processed-task

from irc, and looking at some python answers (http://stackoverflow.com/a/8349166/487878) I noticed that you could not get the data of a processed task from gearmand.

Either we want to store the data once it is processed by gearman worker and get the data back on the next call.

But you could not store the task handle and quit the client and try again with another client after the task is finished at gearman worker.

Before the worker finished you may get the response, but not after that. Hope that helps someone.

 

http://stackoverflow.com/questions/8344561/python-gearman-get-data-from-background-task

background tasks are called such because they allow the client that submitted them to un-block and work disconnected. They do not keep a channel of communication open to the client, so you won't get any of those status updates. They essentially go into the bit-bucket. If you want a background task to communicate its progress, you need to have some other channel for it to communicate with interested programs.

If you want the client to keep running and get updates, but not block on them, you can use the "task" method where you add a bunch of tasks, and then wait for any of them to provide status or be completed. I'm not sure if the pure python gearman interface has this, but the libgearman interface does. Its available in source form here https://launchpad.net/gearman-interface or in some versions of Ubuntu/Debian as python-gearman.libgearman.

 一个比较好的回答:

http://stackoverflow.com/questions/11615941/gearman-is-there-still-no-way-to-retrieve-custom-data-from-a-background-worker