Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 59 lines (47 sloc) 1.23 kb
69ae95f Made worker.py work like a charm
Nikhil Singh authored
1 #!/usr/bin/env python
7719a04 Added License terms to each of the source code files
Nikhil Singh authored
2 #
3 # Copyright (c) 2012, Yahoo! Inc. All rights reserved.
4 # Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
5 #
6
69ae95f Made worker.py work like a charm
Nikhil Singh authored
7 import sys
8 import json
9 import gearman
afdce3d added worker.py file
Rahul Ramakrishna authored
10 from fabric import *
11 from fabric.api import *
12 from gearman import GearmanWorker
13
14
69ae95f Made worker.py work like a charm
Nikhil Singh authored
15 #
16 # Run the ssh task
17 #
18 def exe_job(worker, job):
19 d = json.loads(job.data)
20 env.host_string = d['host']
21 cmd = d['command']
9b8d148 The worker now handles the number of retries also
Nikhil Singh authored
22 retries = int(d['retries'])
23 output = ""
213f03c The worker does not send job_fail anymore
Nikhil Singh authored
24 rc = -1
69ae95f Made worker.py work like a charm
Nikhil Singh authored
25
26 # Run the fabric command. Do not abort on exit
213f03c The worker does not send job_fail anymore
Nikhil Singh authored
27 # Run job for the given number of retries
28 tmp_retries = retries;
9b8d148 The worker now handles the number of retries also
Nikhil Singh authored
29 while retries > 0:
30 with settings(warn_only=True):
31 result = run(cmd)
32 output = output + str(result)
69ae95f Made worker.py work like a charm
Nikhil Singh authored
33
9b8d148 The worker now handles the number of retries also
Nikhil Singh authored
34 if result.failed:
35 if retries == 1:
213f03c The worker does not send job_fail anymore
Nikhil Singh authored
36 rc = -1
9b8d148 The worker now handles the number of retries also
Nikhil Singh authored
37 else:
38 next
39 else:
213f03c The worker does not send job_fail anymore
Nikhil Singh authored
40 rc = 0
9b8d148 The worker now handles the number of retries also
Nikhil Singh authored
41 break
42
43 retries = retries - 1
213f03c The worker does not send job_fail anymore
Nikhil Singh authored
44
714b39e We are now printing output of the job on the console
Nikhil Singh authored
45 return json.dumps({ "rc": rc, "output": output, "host": d['host'] })
69ae95f Made worker.py work like a charm
Nikhil Singh authored
46
47
48 #
49 # Main function
50 #
51 def main():
52 gm_worker = gearman.GearmanWorker(['localhost:4730'])
53 gm_worker.register_task('exe_job',exe_job)
54 gm_worker.work()
55
afdce3d added worker.py file
Rahul Ramakrishna authored
56
69ae95f Made worker.py work like a charm
Nikhil Singh authored
57 if __name__ == '__main__':
58 main()
Something went wrong with that request. Please try again.