吉尔曼& PHP:worker 发回失败的正确方法
PHP 文档对此有点模糊,所以我在这里询问。给定这个工作代码:
<?php
$gmworker= new GearmanWorker();
$gmworker->addServer();
$gmworker->addFunction("doSomething", "doSomethingFunc");
while($gmworker->work());
function doSomethingFunc()
{
try {
$value = doSomethingElse($job->workload());
} catch (Exception $e) {
// Need to notify the client of the error
}
return $value;
}
通知客户端发生的任何错误的正确方法是什么?返回假?使用 GearmanJob::sendFail()?如果是后者,我是否需要在调用 sendFail() 后从 doSomethingFunc() 返回?返回值应该是 sendFail() 返回的值吗?
客户端使用 GearmanClient::returnCode() 来检查故障。此外,简单地使用“return $value”似乎可行,但我应该使用 GearmanJob::sendData() 还是 GearmanJob::sendComplete() 来代替?
The PHP docs are a bit fuzzy on this one, so I'm asking it here. Given this worker code:
<?php
$gmworker= new GearmanWorker();
$gmworker->addServer();
$gmworker->addFunction("doSomething", "doSomethingFunc");
while($gmworker->work());
function doSomethingFunc()
{
try {
$value = doSomethingElse($job->workload());
} catch (Exception $e) {
// Need to notify the client of the error
}
return $value;
}
What's the proper way to notify the client of any error that took place? Return false? Use GearmanJob::sendFail()? If it's the latter, do I need to return from my doSomethingFunc() after calling sendFail()? Should the return value be whatever sendFail() returns?
The client is using GearmanClient::returnCode() to check for failures. Additionally, simply using "return $value" seems to work, but should I be using GearmanJob::sendData() or GearmanJob::sendComplete() instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不是最好的方法,但这是我过去使用过的方法,而且对我来说效果很好。
我在工作线程中使用 sendException() 和 sendFail() 来返回作业失败。异常部分是可选的,但我使用它是为了客户端可以出错并大致了解失败的原因。 sendFail 之后我什么也没返回。
例如,这是工作人员注册为执行工作的回调的方法:
在 sendFail() 之后不要返回任何其他内容,否则您可能会得到奇怪的结果,例如作业服务器认为作业正常结束。
关于返回数据,如果我在工作期间以不同的时间间隔返回块数据(例如流式转码视频,或任何我不想在一个大块中移动的“大”数据),我会使用 sendData()最后有一个 sendComplete() 。否则,如果我只想在作业结束时一次性返回数据,我只使用 sendComplete()。
希望这有帮助。
This may not the be the best way to do it, but it is the method i have used in the past and it has worked well for me.
I use sendException() followed by sendFail() in the worker to return a job failure. The exception part is optional but i use it so the client can error and know roughly why it failed. After the sendFail I return nothing else.
As an example this is a the method that the worker registers as the callback for doing work:
After sendFail() do not return anything else, otherwise you may get strange results such as the jobserver thinking the job ended ok.
As regards returning data, i use sendData() if i am returning data in chunks (such as streaming transcoded video, or any 'big' data where i don't want to move around one large blobs) at various intervals during my job with a sendComplete() at the end. Otherwise if I only want to return my data in one go at the end of the job I only use sendComplete().
Hope this helps.