blob: 4b2e02fb9eeefda4b03a924370c86008481cb3ce [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 Hesketh363d0042013-07-26 11:44:07 +100031 log = logging.getLogger("worker_manager.GearmanManager")
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):
118 for function_name, plugin in self.functions.items():
119 self.gearman_worker.registerFunction(function_name)
120
121 def add_function(self, function_name, plugin):
122 self.functions[function_name] = plugin
123
124 def stop(self):
125 self._stop.set()
126 # Unblock gearman
127 self.log.debug("Telling gearman to stop waiting for jobs")
128 self.gearman_worker.stopWaitingForJobs()
129 self.gearman_worker.shutdown()
130
131 def stopped(self):
132 return self._stop.isSet()
133
134 def run(self):
135 while True and not self.stopped():
136 try:
137 self.cancelled = False
138 # gearman_worker.getJob() blocks until a job is available
139 self.log.debug("Waiting for job")
140 self.job = self.gearman_worker.getJob()
141 self._handle_job()
142 except:
143 self.log.exception('Exception retrieving log event.')
144
145 def _handle_job(self):
146 """ We have a job, give it to the right plugin """
147 self.functions[self.job.name].start_job(self.job)