What is the best way to send unsuccessful work from a gear worker?

#!/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 repeat the repeater job (which executes the fabric command) for the number of repetitions set by the user. For every attempt, I grab and add output. In the last retry, if the operation failed, I want to return the result back to the client.

What happens is that the job.send_fail () command just breaks out and doesn't go to the "return output" command at all to return the result of the failed command.

Is there a better way to fail the job, as well as return data to the client upon exit / failure without the death of the worker?

+4
source share
1 answer

In Gearman, the send_fail () function does not accept any parameters, just tell the job server that this job was not completed on this worker, so the server can try again or something else.

If you are executing your procedure synchronously, the best way would be to execute "retry or not" on the client side.

If you are executing your asynchronous procedure, I think you can use the sendException function. (in the installed gearman module for python on my computer, this is send_job_exception ().) This function can take exception data arguments for your client information.

And finally, you can just just make it so simple: (but your intermediary client will receive A GEARMAN_SUCCESS "RETURNS 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 also help you.

+3
source

Source: https://habr.com/ru/post/1392963/


All Articles