Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame^] | 1 | #!/usr/bin/python2 |
| 2 | # |
| 3 | # Copyright 2013 ... |
| 4 | |
| 5 | import argparse |
| 6 | import daemon |
| 7 | import extras |
| 8 | import imp |
| 9 | import json |
| 10 | import logging |
| 11 | import os |
| 12 | import signal |
| 13 | import sys |
| 14 | |
| 15 | import worker_manager |
| 16 | |
| 17 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 18 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
| 19 | pid_file_module = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile']) |
| 20 | |
| 21 | |
| 22 | class Server(object): |
| 23 | |
| 24 | """ This is the worker server object to be daemonized """ |
| 25 | log = logging.getLogger("rcbau-ci.Server") |
| 26 | |
| 27 | def __init__(self, config): |
| 28 | # Config init |
| 29 | self.config = config |
| 30 | self.manager = None |
| 31 | self.plugins = [] |
| 32 | self.load_plugins() |
| 33 | |
| 34 | # Python logging output file. |
| 35 | self.debug_log = self.config['debug_log'] |
| 36 | |
| 37 | self.tasks = {} |
| 38 | |
| 39 | def setup_logging(self): |
| 40 | if self.debug_log: |
| 41 | logging.basicConfig(format='%(asctime)s %(message)s', |
| 42 | filename=self.debug_log, level=logging.DEBUG) |
| 43 | else: |
| 44 | logging.basicConfig(format='%(asctime)s %(message)s', |
| 45 | level=logging.WARN) |
| 46 | self.log.debug('Log pusher starting.') |
| 47 | |
| 48 | def load_plugins(self): |
| 49 | """ Load the available plugins from task_plugins """ |
| 50 | # Load plugins |
| 51 | for ent in os.listdir('task_plugins'): |
| 52 | if (os.path.isdir('task_plugins/' + ent) |
| 53 | and os.path.isfile('task_plugins/' + ent + '/task.py')): |
| 54 | plugin_info = imp.find_module('task', ['task_plugins/' + ent]) |
| 55 | self.plugins.append(imp.load_module('task', *plugin_info)) |
| 56 | |
| 57 | def run_tasks(self): |
| 58 | """ Run the tasks """ |
| 59 | for plugin in self.plugins: |
| 60 | self.tasks[plugin.__worker_name__] = plugin.Runner(self.config) |
| 61 | self.tasks[plugin.__worker_name__].daemon = True |
| 62 | self.tasks[plugin.__worker_name__].start() |
| 63 | |
| 64 | self.manager = worker_manager.GearmanManager(self.config, self.tasks) |
| 65 | self.manager.daemon = True |
| 66 | self.manager.start() |
| 67 | |
| 68 | def exit_handler(self, signum): |
| 69 | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 70 | for task_name, task in self.tasks.items(): |
| 71 | task.stop() |
| 72 | self.manager.stop() |
| 73 | sys.exit(0) |
| 74 | |
| 75 | def main(self): |
| 76 | self.setup_logging() |
| 77 | self.run_tasks() |
| 78 | |
| 79 | while True: |
| 80 | try: |
| 81 | signal.pause() |
| 82 | except KeyboardInterrupt: |
| 83 | print "Ctrl + C: asking tasks to exit nicely...\n" |
| 84 | self.exit_handler(signal.SIGINT) |
| 85 | |
| 86 | |
| 87 | def main(): |
| 88 | parser = argparse.ArgumentParser() |
| 89 | parser.add_argument('-c', '--config', |
| 90 | default= |
| 91 | '/etc/rcbau-ci/sql-migrate-gearman-worker.json', |
| 92 | help='Path to json config file.') |
| 93 | parser.add_argument('--foreground', action='store_true', |
| 94 | help='Run in the foreground.') |
| 95 | parser.add_argument('-p', '--pidfile', |
| 96 | default='/var/run/rcbau-ci/' |
| 97 | 'sql-migrate-gearman-worker.pid', |
| 98 | help='PID file to lock during daemonization.') |
| 99 | args = parser.parse_args() |
| 100 | |
| 101 | with open(args.config, 'r') as config_stream: |
| 102 | config = json.load(config_stream) |
| 103 | |
| 104 | server = Server(config) |
| 105 | |
| 106 | if args.foreground: |
| 107 | server.main() |
| 108 | else: |
| 109 | pidfile = pid_file_module.TimeoutPIDLockFile(args.pidfile, 10) |
| 110 | with daemon.DaemonContext(pidfile=pidfile): |
| 111 | server.main() |
| 112 | |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | main() |