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 |
| 19 | |
| 20 | from common.utils.dockerhelpers import EventProcessor |
| 21 | |
| 22 | class Podder(object): |
| 23 | |
| 24 | log = get_logger() |
| 25 | |
| 26 | def __init__(self, args, slave_config): |
| 27 | self.log.info('Initializing Podder') |
| 28 | self.running = False |
| 29 | self.events = EventProcessor() |
| 30 | self.handlers = { 'podder_config' : slave_config } |
| 31 | |
| 32 | def run(self): |
| 33 | if self.running: |
| 34 | return |
| 35 | self.running = True |
| 36 | |
| 37 | self.initialize() |
| 38 | |
| 39 | def shutdown(self): |
| 40 | try: |
| 41 | self.events.stop_listening() |
| 42 | except: |
| 43 | self.log.info('Shutting down') |
| 44 | |
| 45 | def initialize(self): |
| 46 | self.define_handlers() |
| 47 | while True: |
| 48 | try: |
| 49 | self.events.listen_for_events(self.handlers) |
| 50 | except KeyboardInterrupt: |
| 51 | self.shutdown() |
| 52 | break |
| 53 | except Exception, e: |
| 54 | self.log.info('Handler exception', e) |
| 55 | |
| 56 | def define_handlers(self): |
| 57 | self.handlers['start'] = handler_start |
| 58 | self.handlers['stop'] = handler_stop |
| 59 | |