blob: c22d594878f4060b24f10c9c1f37e7631642d78f [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
Joshua Heskethdd50d272014-01-05 15:35:52 +110067 self.log.debug("Waiting for server")
68 self.gearman_worker.waitForServer()
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100069 logging.debug("Waiting for job")
70 self.current_step = 0
71 job = self.gearman_worker.getJob()
72 self._handle_job(job)
73 except:
74 logging.exception('Exception retrieving log event.')
75
76 def _handle_job(self, job):
77 """ Handle the requested job """
78 try:
79 job_arguments = json.loads(job.arguments.decode('utf-8'))
80 self.tasks[job_arguments['name']].stop_worker(
81 job_arguments['number'])
Joshua Hesketh4a30d272013-08-12 11:17:25 +100082 job.sendWorkComplete()
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100083 except Exception as e:
Joshua Heskethdd50d272014-01-05 15:35:52 +110084 self.log.exception('Exception waiting for management job.')
Joshua Hesketh0ddd6382013-07-26 10:33:36 +100085 job.sendWorkException(str(e).encode('utf-8'))
Joshua Hesketh4343b952013-11-20 12:11:55 +110086
87
88class ZuulClient(threading.Thread):
89
90 """ ..."""
91
92 log = logging.getLogger("worker_manager.ZuulClient")
93
94 def __init__(self, global_config, worker_name):
95 super(ZuulClient, self).__init__()
96 self._stop = threading.Event()
97 self.global_config = global_config
98
99 self.worker_name = worker_name
100
101 # Set up the runner worker
102 self.gearman_worker = None
103 self.functions = {}
104
105 self.job = None
106 self.cancelled = False
107
108 self.setup_gearman()
109
110 def setup_gearman(self):
111 self.log.debug("Set up gearman worker")
112 self.gearman_worker = gear.Worker(self.worker_name)
113 self.gearman_worker.addServer(
114 self.global_config['zuul_server']['gearman_host'],
115 self.global_config['zuul_server']['gearman_port']
116 )
Joshua Hesketh4343b952013-11-20 12:11:55 +1100117
118 def register_functions(self):
Joshua Heskethfc449062013-11-20 16:06:06 +1100119 self.log.debug("Register functions with gearman")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100120 for function_name, plugin in self.functions.items():
121 self.gearman_worker.registerFunction(function_name)
Joshua Heskethb39f8842013-11-21 13:12:00 +1100122 self.log.debug(self.gearman_worker.functions)
Joshua Hesketh4343b952013-11-20 12:11:55 +1100123
124 def add_function(self, function_name, plugin):
Joshua Heskethfc449062013-11-20 16:06:06 +1100125 self.log.debug("Add function, %s, to list" % function_name)
Joshua Hesketh4343b952013-11-20 12:11:55 +1100126 self.functions[function_name] = plugin
127
128 def stop(self):
129 self._stop.set()
130 # Unblock gearman
131 self.log.debug("Telling gearman to stop waiting for jobs")
132 self.gearman_worker.stopWaitingForJobs()
133 self.gearman_worker.shutdown()
134
135 def stopped(self):
136 return self._stop.isSet()
137
138 def run(self):
139 while True and not self.stopped():
140 try:
141 self.cancelled = False
142 # gearman_worker.getJob() blocks until a job is available
Joshua Heskethdd50d272014-01-05 15:35:52 +1100143 self.log.debug("Waiting for server")
144 self.gearman_worker.waitForServer()
Joshua Hesketh4343b952013-11-20 12:11:55 +1100145 self.log.debug("Waiting for job")
146 self.job = self.gearman_worker.getJob()
147 self._handle_job()
148 except:
Joshua Heskethdd50d272014-01-05 15:35:52 +1100149 self.log.exception('Exception waiting for job.')
Joshua Hesketh4343b952013-11-20 12:11:55 +1100150
151 def _handle_job(self):
152 """ We have a job, give it to the right plugin """
Joshua Hesketh6eb5fdc2013-11-20 12:36:06 +1100153 self.log.debug("We have a job, we'll launch the task now.")
Joshua Hesketh4343b952013-11-20 12:11:55 +1100154 self.functions[self.job.name].start_job(self.job)