Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get status of Gearman Jobs by their uniq id?

I need to get status of Gearman jobs by these uniq id, not by open handlers, as desribed every place I seen

Is it possible? using in python-gearman v. 2...

Thanks for assistance!

like image 221
Michael_XIII Avatar asked Aug 08 '11 14:08

Michael_XIII


1 Answers

Had to dig quite a bit to solve this issue, as it's not exposed in a friendly manner in the python-gearman-API. You can however solve it by creating appropriate instances of the GearmanJob and GearmanJobRequest yourself.

Here's a small example of how you can do this:

import gearman

client = gearman.GearmanClient(['localhost'])
result = client.submit_job('reverse', 'this is a string', background=True);

You want to keep track of which server got to handle the job (if you have more than one Gearman server handling tasks), and the handle of the task. The connection information is available through result.job.connection (.gearman_host and .gearman_port), while the handle is available through result.job.handle.

To check the status of a currently running job you create a GearmanClient, but only supply the server you want to query for the current state:

client = gearman.GearmanClient(['localhost'])

# configure the job to request status for - the last four is not needed for Status requests.
j = gearman.job.GearmanJob(client.connection_list[0], result.job.handle, None, None, None, None)

# create a job request 
jr = gearman.job.GearmanJobRequest(j)
jr.state = 'CREATED'

# request the state from gearmand
res = client.get_job_status(jr)

# the res structure should now be filled with the status information about the task
print(str(res.status.numerator) + " / " + str(res.status.denominator))

Hopefully that helps!

like image 142
MatsLindh Avatar answered Oct 18 '22 17:10

MatsLindh