#!/usr/bin/env python
import gear
import simplejson
import uuid
import time
import argparse
import sys
from collections import deque

# simple python client

class Client(object):
    def __init__(self):
        self.args = None
        self.config = None

    def parse_arguments(self):
        parser = argparse.ArgumentParser(description='Gearman Client')
        parser.add_argument('-s', dest='server',
                            default='localhost',
                            help='Gearman server host (default=localhost)')
        parser.add_argument('-p', dest='port', default=4730,
                            help='Gearman server port (default=4730)')
        parser.add_argument('--function', dest='function',
                            help='Gearman function')
        parser.add_argument('--iterations', dest='iterations', default=1,
                            help='Num of iterations to run the job (default=1)')
        parser.add_argument('--params', dest='params',
                            default={"OFFLINE_NODE_WHEN_COMPLETE":"false"},
                            help='Parameters to pass to build')
        parser.add_argument('--log-config', dest='log_config',
                            help='logging config file')
        self.args = parser.parse_args()

    def main(self):
        gclient = gear.Client()
        gclient.addServer(self.args.server)
        gclient.waitForServer()  # Wait for at least one server to be connected

        if not isinstance(self.args.params, dict):
            build_params = simplejson.loads(self.args.params)
        else:
            build_params = self.args.params

        job_queue = deque()
        job = None
        print "\n" + time.asctime( time.localtime(time.time()))
        for x in range(0, int(self.args.iterations)):
            job_id = uuid.uuid4().hex
            build_params.update({'uuid':job_id})
            job = gear.Job(self.args.function,
                           simplejson.dumps(build_params),
                           unique=job_id)

            print "Sending job: " + self.args.function + " to " + self.args.server + " with params=" + str(build_params)
            gclient.submitJob(job)
            job_queue.append(job)
          

        # wait for jobs to complete before exiting
        print ("\nWaiting for jobs to finish"),
        finished = False
        while True:
            sys.stdout.write('.')
            sys.stdout.flush()
            if (job.complete):
                print "\n\n---- Job Results ----"
                print time.asctime( time.localtime(time.time())) + "\n"
                while (len(job_queue) != 0) :
                    cjob = job_queue.popleft()
                    print cjob.unique + ' : ' + str(cjob.data)
                finished = True

            time.sleep(1);
            if finished:
                break


def main():
    client = Client()
    client.parse_arguments()
    client.main()

if __name__ == '__main__':
    sys.path.insert(0, '.')
    main()

@@ -128,16 +128,18 @@
 deque%0A%0A#
+ A
  simple 
@@ -151,16 +151,35 @@
 n client
+ for gearman plugin
 %0A%0Aclass 
@@ -1689,32 +1689,331 @@
      job = None%0A
+        num_jobs = int(self.args.iterations)%0A        offline_node = build_params.get(%22OFFLINE_NODE_WHEN_COMPLETE%22, %22false%22)%0A        if (num_jobs %3E 1 and offline_node.lower()==%22true%22):%0A            print %22WARN: Offline node requested multiple times, may need to online nodes to complete this request%22%0A
         print %22%5C
@@ -2090,33 +2090,16 @@
 (0, 
-int(self.args.iterations)
+num_jobs
 ):%0A 
@@ -2534,17 +2534,16 @@
        %0A
-%0A
         
@@ -2805,16 +2805,18 @@
 %5Cn%5Cn----
+- 
  Job Res
@@ -2824,35 +2824,12 @@
 lts 
-----%22%0A                print
+(%22 +
  tim
@@ -2830,33 +2830,32 @@
  + time.asctime(
- 
 time.localtime(t
@@ -2870,16 +2870,26 @@
 ())) + %22
+)  ------ 
 %5Cn%22%0A    
@@ -2973,24 +2973,229 @@
 e.popleft()%0A
+                    if (cjob.failure or cjob.exception):%0A                        # job request failed for some reason%0A                        print cjob.unique + ' :  Failed'%0A                    else:%0A    
             

update error handling slightly