从 gearman 工作人员发送失败的作业输出的最佳方式是什么?

发布于 2024-12-29 04:49:16 字数 1265 浏览 1 评论 0原文

#!/usr/bin/env python
import sys
import json
import gearman
from fabric import *
from fabric.api import *
from gearman import GearmanWorker


#
# Run the ssh task
#
def exe_job(worker, job):
    d = json.loads(job.data)
    env.host_string = d['host'] 
    cmd = d['command']
    retries = int(d['retries'])
    output = ""

    # Run the fabric command. Do not abort on exit
    # Run job for the given number of retries
    tmp_retries = retries;
    while retries > 0:
        with settings(warn_only=True):
            result = run(cmd)
            output = output + str(result)

        if result.failed:
            if retries == 1:
                job.send_fail()
                break
            else:
                next
        else:
            break

        retries = retries - 1

    return output


#
# Main function
#
def main():
    gm_worker = gearman.GearmanWorker(['localhost:4730'])
    gm_worker.register_task('exe_job',exe_job) 
    gm_worker.work()


if __name__ == '__main__':
    main()

在我的代码中,我尝试重试 gearman 作业(运行 Fabric 命令)以达到用户指定的重试次数。对于每次尝试,我都会捕获并附加输出。在最后一次重试中,如果作业失败,我想将输出返回给客户端。

发生的情况是 job.send_fail() 命令只是中断,根本不会转到“返回输出”命令来返回失败命令的输出。

有没有更好的方法来使作业失败,并在退出/失败时将数据返回给客户端,而不会导致工作人员死亡?

#!/usr/bin/env python
import sys
import json
import gearman
from fabric import *
from fabric.api import *
from gearman import GearmanWorker


#
# Run the ssh task
#
def exe_job(worker, job):
    d = json.loads(job.data)
    env.host_string = d['host'] 
    cmd = d['command']
    retries = int(d['retries'])
    output = ""

    # Run the fabric command. Do not abort on exit
    # Run job for the given number of retries
    tmp_retries = retries;
    while retries > 0:
        with settings(warn_only=True):
            result = run(cmd)
            output = output + str(result)

        if result.failed:
            if retries == 1:
                job.send_fail()
                break
            else:
                next
        else:
            break

        retries = retries - 1

    return output


#
# Main function
#
def main():
    gm_worker = gearman.GearmanWorker(['localhost:4730'])
    gm_worker.register_task('exe_job',exe_job) 
    gm_worker.work()


if __name__ == '__main__':
    main()

In my code, I am trying to retry a gearman job (which runs a fabric command) for the number of retries specified by the user. For every try, I am capturing and appending the output. In the last retry, if the job fails, I want to return the output back to the client.

What is happening is that job.send_fail() command simply breaks out and does not go to the "return output" command at all to return the output of the failed command.

Is there a better way to fail a job and also return the data to the client on exit/failure without the worker dying?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

闻呓 2025-01-05 04:49:16

在 Gearman 中,send_fail() 函数不带任何参数,只是告诉作业服务器此工作线程上的作业失败,以便服务器可以重试或执行其他操作。

如果您同步执行过程,更好的方法应该是在客户端执行“重试或不重试”工作。

如果您异步执行程序,我认为您可以使用“sendException”函数。(在我的计算机中安装的 python gearman 模块中,它是 send_job_exception()。)此函数可以为您的客户端信息获取异常数据参数。

最后,您可以像这样简单地这样做:(但是您的 gearman 客户端将收到“GEARMAN_SUCCESS”返回代码!)

#some codes
while retries > 0:
    with settings(warn_only=True):
        result = run(cmd)
        output = output + str(result)

    if result.failed:
        if retries == 1:
            #job.send_fail()
            output = "FAILED_JOB" + "some_return_str"
            break
        else:
            next
    else:
        break

    retries = retries - 1

return output

此链接也会对您有所帮助。

In Gearman, the send_fail() function takes no parameters, just tell the job server this job on this worker is failed, so the server can do retry or something else.

If you do your procedure synchronously, the better way should be do the "retry or not" work on the client side.

If you do your procedure asynchronously, I think you can use "sendException" function.(in the installed gearman module for python in my computer it's send_job_exception(). ) This function can take a exception data arguments for your client information.

And at last, you can just simply do it like this easy way: (but your gearman client will receive A "GEARMAN_SUCCESS" RETURN CODE!)

#some codes
while retries > 0:
    with settings(warn_only=True):
        result = run(cmd)
        output = output + str(result)

    if result.failed:
        if retries == 1:
            #job.send_fail()
            output = "FAILED_JOB" + "some_return_str"
            break
        else:
            next
    else:
        break

    retries = retries - 1

return output

this link will help you, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文