我们如何接收已经运行的装备任务的数据。
这个想法是当脚本由于某种原因而死亡时想要从作业句柄获取已处理的任务。 (说mysql不见了,由于内存导致php分段错误)
这是我正在使用的示例代码。
<?php
# The client script
# create our gearman client
$gmclient = new GearmanClient();
# add the default job server
$gmclient->addServer();
# set a couple of callbacks so we can track progress
$gmclient->setCompleteCallback("reverse_complete");
$gmclient->setStatusCallback("reverse_status");
$gmclient->setCreatedCallback(function ($task) {
echo 'Job Handle server returned : ' . $task->jobHandle() . PHP_EOL;
});
# add another task, but this one to run in the background
$job_handle = $gmclient->doBackground("reverse", "!dlroW olleH");
echo " Job handle : " . $job_handle . PHP_EOL;
// add a task
// $gmclient->addTask("reverse", "!dlroW olleH");
// this will work, how to get data of bg task
if (! $gmclient->runTasks()) {
echo "ERROR " . $gmclient->error() . "\n";
exit;
}
echo "DONE\n";
function reverse_status($task) {
echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() .
"/" . $task->taskDenominator() . "\n";
}
function reverse_complete($task) {
echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}
工人
<?php
# The worker script
echo "Starting\n";
# Create our worker object.
$gmworker= new GearmanWorker();
# Add default server (localhost).
$gmworker->addServer();
# Register function "reverse" with the server.
$gmworker->addFunction("reverse", "reverse_fn");
print "Waiting for job...\n";
while($gmworker->work()) {
if ($gmworker->returnCode() != GEARMAN_SUCCESS) {
echo "return_code: " . $gmworker->returnCode() . "\n";
break;
}
}
function reverse_fn($job) {
echo "Received job: " . $job->handle() . "\n";
$workload = $job->workload();
$workload_size = $job->workloadSize();
echo "Workload: $workload ($workload_size)\n";
$result= strrev($workload);
echo "Result: $result\n";
# Return what we want to send back to the client.
return $result;
}
获取作业句柄数据的状态。
<?php
# The client script
# create our gearman client
$gmclient = new GearmanClient();
# add the default job server
$gmclient->addServer();
/*
$gmclient->setClientCallback(function () {
echo "setClientCallback ! " . PHP_EOL;
});
*/
$gmclient->setCompleteCallback(function () {
echo "setCompleteCallback ! " . PHP_EOL;
});
$gmclient->setCreatedCallback(function () {
echo "setCreatedCallback ! " . PHP_EOL;
});
$gmclient->setDataCallback(function () {
echo "setDataCallback ! " . PHP_EOL;
});
$gmclient->setExceptionCallback(function () {
echo "setExceptionCallback ! " . PHP_EOL;
});
$gmclient->setFailCallback(function () {
echo "setFailCallback ! " . PHP_EOL;
});
$gmclient->setStatusCallback(function () {
echo "setStatusCallback ! " . PHP_EOL;
echo "Is known ? " . PHP_EOL;
var_dump($task->returnCode());
echo "Return code" . PHP_EOL;
var_dump($task->returnCode());
echo "Is task running ? " . PHP_EOL;
var_dump($task->isRunning());
if (!$task->isKnown())
$done++;
});
$gmclient->setWarningCallback(function () {
echo "setWarningCallback ! " . PHP_EOL;
});
$gmclient->setWorkloadCallback(function () {
echo "setWorkloadCallback ! " . PHP_EOL;
});
// $job_handle = 'H:hari-Vostro-270s:21';
$job_handle = escapeshellarg($argv[1]);
// $gmtask = $gmclient->addTaskStatus($job_handle);
// $gmclient->runTasks();
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";
答案 0 :(得分:0)
,并查看一些python答案(https://stackoverflow.com/a/8349166/487878)我注意到你无法从gearmand获取已处理任务的数据。
要么我们想要在齿轮工人处理数据后存储数据,并在下次调用时获取数据。
但是你无法存储任务句柄并退出客户端,并在任务完成后在另一个客户端再次尝试。
在工人完成之前,你可以得到答复,但之后不会。希望有所帮助。