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