blob: 8c8150fdc8109bbcd6fe449a3dec9b228f649e28 [file] [log] [blame]
Joshua Hesketh39a0fee2013-07-31 12:00:53 +10001# Copyright 2013 Rackspace Australia
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100015
16import gear
17import json
18import logging
19import os
20import threading
21
22
Joshua Hesketh4343b952013-11-20 12:11:55 +110023class ZuulManager(threading.Thread):
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100024
25 """ This thread manages all of the launched gearman workers.
26 As required by the zuul protocol it handles stopping builds when they
Joshua Hesketh363d0042013-07-26 11:44:07 +100027 are cancelled through stop:turbo-hipster-manager-%hostname.
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100028 To do this it implements its own gearman worker waiting for events on
29 that manager. """
30
Joshua Hesketh6eb5fdc2013-11-20 12:36:06 +110031 log = logging.getLogger("worker_manager.ZuulManager")
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100032
33 def __init__(self, config, tasks):
Joshua Hesketh4343b952013-11-20 12:11:55 +110034 super(ZuulManager, self).__init__()
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100035 self._stop = threading.Event()
36 self.config = config
37 self.tasks = tasks
38
39 self.gearman_worker = None
40 self.setup_gearman()
41
42 def setup_gearman(self):
43 hostname = os.uname()[1]
Joshua Hesketh363d0042013-07-26 11:44:07 +100044 self.gearman_worker = gear.Worker('turbo-hipster-manager-%s'
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100045 % hostname)
46 self.gearman_worker.addServer(
47 self.config['zuul_server']['gearman_host'],
48 self.config['zuul_server']['gearman_port']
49 )
50 self.gearman_worker.registerFunction(
Joshua Hesketh363d0042013-07-26 11:44:07 +100051 'stop:turbo-hipster-manager-%s' % hostname)
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100052
53 def stop(self):
54 self._stop.set()
55 # Unblock gearman
56 self.log.debug("Telling gearman to stop waiting for jobs")
57 self.gearman_worker.stopWaitingForJobs()
58 self.gearman_worker.shutdown()
59
60 def stopped(self):
61 return self._stop.isSet()
62
63 def run(self):
64 while True and not self.stopped():
65 try:
66 # gearman_worker.getJob() blocks until a job is available
67 logging.debug("Waiting for job")
68 self.current_step = 0
69 job = self.gearman_worker.getJob()
70 self._handle_job(job)
71 except:
72 logging.exception('Exception retrieving log event.')
73
74 def _handle_job(self, job):
75 """ Handle the requested job """
76 try:
77 job_arguments = json.loads(job.arguments.decode('utf-8'))
78 self.tasks[job_arguments['name']].stop_worker(
79 job_arguments['number'])
Joshua Hesketh4a30d272013-08-12 11:17:25 +100080 job.sendWorkComplete()
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100081 except Exception as e:
82 self.log.exception('Exception handling log event.')
83 job.sendWorkException(str(e).encode('utf-8'))
Joshua Hesketh4343b952013-11-20 12:11:55 +110084
85
86class ZuulClient(threading.Thread):
87
88 """ ..."""
89
90 log = logging.getLogger("worker_manager.ZuulClient")
91
92 def __init__(self, global_config, worker_name):
93 super(ZuulClient, self).__init__()
94 self._stop = threading.Event()
95 self.global_config = global_config
96
97 self.worker_name = worker_name
98
99 # Set up the runner worker
100 self.gearman_worker = None
101 self.functions = {}
102
103 self.job = None
104 self.cancelled = False
105
106 self.setup_gearman()
107
108 def setup_gearman(self):
109 self.log.debug("Set up gearman worker")
110 self.gearman_worker = gear.Worker(self.worker_name)
111 self.gearman_worker.addServer(
112 self.global_config['zuul_server']['gearman_host'],
113 self.global_config['zuul_server']['gearman_port']
114 )
115 self.register_functions()
116
117 def register_functions(self):
Joshua Heskethfc449062013-11-20 16:06:06 +1100118 self.log.debug("Register functions with gearman")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100119 for function_name, plugin in self.functions.items():
120 self.gearman_worker.registerFunction(function_name)
121
122 def add_function(self, function_name, plugin):
Joshua Heskethfc449062013-11-20 16:06:06 +1100123 self.log.debug("Add function, %s, to list" % function_name)
Joshua Hesketh4343b952013-11-20 12:11:55 +1100124 self.functions[function_name] = plugin
125
126 def stop(self):
127 self._stop.set()
128 # Unblock gearman
129 self.log.debug("Telling gearman to stop waiting for jobs")
130 self.gearman_worker.stopWaitingForJobs()
131 self.gearman_worker.shutdown()
132
133 def stopped(self):
134 return self._stop.isSet()
135
136 def run(self):
137 while True and not self.stopped():
138 try:
139 self.cancelled = False
140 # gearman_worker.getJob() blocks until a job is available
141 self.log.debug("Waiting for job")
142 self.job = self.gearman_worker.getJob()
143 self._handle_job()
144 except:
145 self.log.exception('Exception retrieving log event.')
146
147 def _handle_job(self):
148 """ We have a job, give it to the right plugin """
Joshua Hesketh6eb5fdc2013-11-20 12:36:06 +1100149 self.log.debug("We have a job, we'll launch the task now.")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100150 self.functions[self.job.name].start_job(self.job)