blob: d011a030858bfe647dca8bb0161219dd314df85e [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")
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100057 self.gearman_worker.shutdown()
58
59 def stopped(self):
60 return self._stop.isSet()
61
62 def run(self):
Joshua Hesketh81b5fb82014-03-05 13:06:08 +110063 while not self.stopped():
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100064 try:
65 # gearman_worker.getJob() blocks until a job is available
Joshua Heskethdd50d272014-01-05 15:35:52 +110066 self.log.debug("Waiting for server")
67 self.gearman_worker.waitForServer()
Joshua Hesketh81b5fb82014-03-05 13:06:08 +110068 if (not self.stopped() and self.gearman_worker.running and
69 self.gearman_worker.active_connections):
70 logging.debug("Waiting for job")
71 self.current_step = 0
72 job = self.gearman_worker.getJob()
73 self._handle_job(job)
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100074 except:
75 logging.exception('Exception retrieving log event.')
Joshua Hesketh81b5fb82014-03-05 13:06:08 +110076 self.log.debug("Finished manager thread")
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100077
78 def _handle_job(self, job):
79 """ Handle the requested job """
80 try:
81 job_arguments = json.loads(job.arguments.decode('utf-8'))
82 self.tasks[job_arguments['name']].stop_worker(
83 job_arguments['number'])
Joshua Hesketh4a30d272013-08-12 11:17:25 +100084 job.sendWorkComplete()
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100085 except Exception as e:
Joshua Heskethdd50d272014-01-05 15:35:52 +110086 self.log.exception('Exception waiting for management job.')
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100087 job.sendWorkException(str(e).encode('utf-8'))
Joshua Hesketh4343b952013-11-20 12:11:55 +110088
89
90class ZuulClient(threading.Thread):
91
92 """ ..."""
93
94 log = logging.getLogger("worker_manager.ZuulClient")
95
96 def __init__(self, global_config, worker_name):
97 super(ZuulClient, self).__init__()
98 self._stop = threading.Event()
99 self.global_config = global_config
100
101 self.worker_name = worker_name
102
103 # Set up the runner worker
104 self.gearman_worker = None
105 self.functions = {}
106
107 self.job = None
Joshua Hesketh4343b952013-11-20 12:11:55 +1100108
109 self.setup_gearman()
110
111 def setup_gearman(self):
112 self.log.debug("Set up gearman worker")
113 self.gearman_worker = gear.Worker(self.worker_name)
114 self.gearman_worker.addServer(
115 self.global_config['zuul_server']['gearman_host'],
116 self.global_config['zuul_server']['gearman_port']
117 )
Joshua Hesketh4343b952013-11-20 12:11:55 +1100118
119 def register_functions(self):
Joshua Heskethfc449062013-11-20 16:06:06 +1100120 self.log.debug("Register functions with gearman")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100121 for function_name, plugin in self.functions.items():
122 self.gearman_worker.registerFunction(function_name)
Joshua Heskethb39f8842013-11-21 13:12:00 +1100123 self.log.debug(self.gearman_worker.functions)
Joshua Hesketh4343b952013-11-20 12:11:55 +1100124
125 def add_function(self, function_name, plugin):
Joshua Heskethfc449062013-11-20 16:06:06 +1100126 self.log.debug("Add function, %s, to list" % function_name)
Joshua Hesketh4343b952013-11-20 12:11:55 +1100127 self.functions[function_name] = plugin
128
129 def stop(self):
130 self._stop.set()
131 # Unblock gearman
132 self.log.debug("Telling gearman to stop waiting for jobs")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100133 self.gearman_worker.shutdown()
134
135 def stopped(self):
136 return self._stop.isSet()
137
138 def run(self):
Joshua Hesketh81b5fb82014-03-05 13:06:08 +1100139 while not self.stopped():
Joshua Hesketh4343b952013-11-20 12:11:55 +1100140 try:
Joshua Hesketh4343b952013-11-20 12:11:55 +1100141 # gearman_worker.getJob() blocks until a job is available
Joshua Heskethdd50d272014-01-05 15:35:52 +1100142 self.log.debug("Waiting for server")
143 self.gearman_worker.waitForServer()
Joshua Hesketh81b5fb82014-03-05 13:06:08 +1100144 if (not self.stopped() and self.gearman_worker.running and
145 self.gearman_worker.active_connections):
146 self.log.debug("Waiting for job")
147 self.job = self.gearman_worker.getJob()
148 self._handle_job()
Joshua Hesketh4343b952013-11-20 12:11:55 +1100149 except:
Joshua Heskethdd50d272014-01-05 15:35:52 +1100150 self.log.exception('Exception waiting for job.')
Joshua Hesketh81b5fb82014-03-05 13:06:08 +1100151 self.log.debug("Finished client thread")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100152
153 def _handle_job(self):
154 """ We have a job, give it to the right plugin """
Joshua Hesketh81b5fb82014-03-05 13:06:08 +1100155 if self.job:
156 self.log.debug("We have a job, we'll launch the task now.")
157 self.functions[self.job.name].start_job(self.job)