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: |
| 45 | logging.basicConfig(format='%(asctime)s %(message)s', |
| 46 | filename=self.debug_log, level=logging.DEBUG) |
| 47 | else: |
| 48 | logging.basicConfig(format='%(asctime)s %(message)s', |
| 49 | level=logging.WARN) |
| 50 | self.log.debug('Log pusher starting.') |
| 51 | |
| 52 | def load_plugins(self): |
| 53 | """ Load the available plugins from task_plugins """ |
| 54 | # Load plugins |
| 55 | for ent in os.listdir('task_plugins'): |
| 56 | if (os.path.isdir('task_plugins/' + ent) |
| 57 | and os.path.isfile('task_plugins/' + ent + '/task.py')): |
| 58 | plugin_info = imp.find_module('task', ['task_plugins/' + ent]) |
| 59 | self.plugins.append(imp.load_module('task', *plugin_info)) |
| 60 | |
| 61 | def run_tasks(self): |
| 62 | """ Run the tasks """ |
| 63 | for plugin in self.plugins: |
| 64 | self.tasks[plugin.__worker_name__] = plugin.Runner(self.config) |
| 65 | self.tasks[plugin.__worker_name__].daemon = True |
| 66 | self.tasks[plugin.__worker_name__].start() |
| 67 | |
| 68 | self.manager = worker_manager.GearmanManager(self.config, self.tasks) |
| 69 | self.manager.daemon = True |
| 70 | self.manager.start() |
| 71 | |
| 72 | def exit_handler(self, signum): |
| 73 | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 74 | for task_name, task in self.tasks.items(): |
| 75 | task.stop() |
| 76 | self.manager.stop() |
| 77 | sys.exit(0) |
| 78 | |
| 79 | def main(self): |
| 80 | self.setup_logging() |
| 81 | self.run_tasks() |
| 82 | |
| 83 | while True: |
| 84 | try: |
| 85 | signal.pause() |
| 86 | except KeyboardInterrupt: |
| 87 | print "Ctrl + C: asking tasks to exit nicely...\n" |
| 88 | self.exit_handler(signal.SIGINT) |
| 89 | |
| 90 | |
| 91 | def main(): |
| 92 | parser = argparse.ArgumentParser() |
| 93 | parser.add_argument('-c', '--config', |
| 94 | default= |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 95 | '/etc/turbo-hipster/config.json', |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 96 | help='Path to json config file.') |
Joshua Hesketh | 478f151 | 2013-07-26 11:47:27 +1000 | [diff] [blame] | 97 | parser.add_argument('--background', action='store_true', |
| 98 | help='Run in the background.') |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 99 | parser.add_argument('-p', '--pidfile', |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 100 | default='/var/run/turbo-hipster/' |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 101 | 'sql-migrate-gearman-worker.pid', |
| 102 | help='PID file to lock during daemonization.') |
| 103 | args = parser.parse_args() |
| 104 | |
| 105 | with open(args.config, 'r') as config_stream: |
| 106 | config = json.load(config_stream) |
| 107 | |
| 108 | server = Server(config) |
| 109 | |
Joshua Hesketh | 478f151 | 2013-07-26 11:47:27 +1000 | [diff] [blame] | 110 | if args.background: |
Joshua Hesketh | 1377f6f | 2013-07-26 12:02:15 +1000 | [diff] [blame^] | 111 | pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 112 | with daemon.DaemonContext(pidfile=pidfile): |
| 113 | server.main() |
Joshua Hesketh | 478f151 | 2013-07-26 11:47:27 +1000 | [diff] [blame] | 114 | else: |
| 115 | server.main() |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | main() |