9

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!

Michael_XIII
  • 175
  • 1
  • 14

1 Answers1

6

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!

MatsLindh
  • 49,529
  • 4
  • 53
  • 84
  • I've also added a convenience method to my fork of python-gearman at github. I don't think it'll be in the official distribution any time, but the patch is available here: https://github.com/matslindh/python-gearman/commit/983e97c5055f1ccf7059f00215cc6e026ebc1ba0 – MatsLindh Oct 03 '11 at 13:32
  • Thanks. It is looks like what I need.But I solve it by using memcache wrapper for storing statuses and additional data for jobs. – Michael_XIII Oct 08 '11 at 05:26