alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | from handlers import handler_start, handler_stop |
| 18 | from structlog import get_logger |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 19 | from jinja2 import Template |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 20 | |
| 21 | from common.utils.dockerhelpers import EventProcessor |
| 22 | |
| 23 | class Podder(object): |
| 24 | |
| 25 | log = get_logger() |
| 26 | |
| 27 | def __init__(self, args, slave_config): |
| 28 | self.log.info('Initializing Podder') |
| 29 | self.running = False |
alshabib | 05fb71f | 2016-12-04 16:08:29 -0800 | [diff] [blame] | 30 | self.events = EventProcessor(threads=args.threads) |
alshabib | 9d22202 | 2016-11-10 16:11:09 -0800 | [diff] [blame] | 31 | self.handlers = { 'podder_config' : Template(slave_config) } |
alshabib | 7941d40 | 2016-11-08 00:11:20 +0100 | [diff] [blame] | 32 | |
| 33 | def run(self): |
| 34 | if self.running: |
| 35 | return |
| 36 | self.running = True |
| 37 | |
| 38 | self.initialize() |
| 39 | |
| 40 | def shutdown(self): |
| 41 | try: |
| 42 | self.events.stop_listening() |
| 43 | except: |
| 44 | self.log.info('Shutting down') |
| 45 | |
| 46 | def initialize(self): |
| 47 | self.define_handlers() |
| 48 | while True: |
| 49 | try: |
| 50 | self.events.listen_for_events(self.handlers) |
| 51 | except KeyboardInterrupt: |
| 52 | self.shutdown() |
| 53 | break |
| 54 | except Exception, e: |
| 55 | self.log.info('Handler exception', e) |
| 56 | |
| 57 | def define_handlers(self): |
| 58 | self.handlers['start'] = handler_start |
| 59 | self.handlers['stop'] = handler_stop |
| 60 | |