Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
Joshua Hesketh | 39a0fee | 2013-07-31 12:00:53 +1000 | [diff] [blame] | 3 | # Copyright 2013 Rackspace Australia |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 16 | |
Joshua Hesketh | 1377f6f | 2013-07-26 12:02:15 +1000 | [diff] [blame] | 17 | |
| 18 | """ worker_server.py is an executable worker server that loads and runs |
| 19 | task_plugins. """ |
| 20 | |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 21 | import argparse |
| 22 | import daemon |
| 23 | import extras |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 24 | import json |
| 25 | import logging |
| 26 | import os |
| 27 | import signal |
| 28 | import sys |
| 29 | |
| 30 | import worker_manager |
| 31 | |
| 32 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 33 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
Joshua Hesketh | 1377f6f | 2013-07-26 12:02:15 +1000 | [diff] [blame] | 34 | PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile']) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | class Server(object): |
| 38 | |
| 39 | """ This is the worker server object to be daemonized """ |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 40 | log = logging.getLogger("worker_server.Server") |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 41 | |
| 42 | def __init__(self, config): |
| 43 | # Config init |
| 44 | self.config = config |
| 45 | self.manager = None |
| 46 | self.plugins = [] |
| 47 | self.load_plugins() |
| 48 | |
| 49 | # Python logging output file. |
| 50 | self.debug_log = self.config['debug_log'] |
| 51 | |
| 52 | self.tasks = {} |
| 53 | |
| 54 | def setup_logging(self): |
| 55 | if self.debug_log: |
Joshua Hesketh | 6b6700b | 2013-07-26 16:47:32 +1000 | [diff] [blame] | 56 | if not os.path.isdir(os.path.dirname(self.debug_log)): |
| 57 | os.makedirs(os.path.dirname(self.debug_log)) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 58 | logging.basicConfig(format='%(asctime)s %(message)s', |
| 59 | filename=self.debug_log, level=logging.DEBUG) |
| 60 | else: |
| 61 | logging.basicConfig(format='%(asctime)s %(message)s', |
| 62 | level=logging.WARN) |
| 63 | self.log.debug('Log pusher starting.') |
| 64 | |
| 65 | def load_plugins(self): |
| 66 | """ Load the available plugins from task_plugins """ |
| 67 | # Load plugins |
Joshua Hesketh | 6b6700b | 2013-07-26 16:47:32 +1000 | [diff] [blame] | 68 | for plugin in self.config['plugins']: |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 69 | self.plugins.append({ |
| 70 | 'module': __import__('turbo_hipster.task_plugins.' + |
Joshua Hesketh | 3b74fcd | 2013-09-06 16:19:52 +1000 | [diff] [blame] | 71 | plugin['name'] + '.task', |
| 72 | fromlist='turbo_hipster.task_plugins' + |
| 73 | plugin['name']), |
Joshua Hesketh | 4acd716 | 2013-09-06 16:05:37 +1000 | [diff] [blame] | 74 | 'plugin_config': plugin |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 75 | }) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 76 | |
| 77 | def run_tasks(self): |
| 78 | """ Run the tasks """ |
| 79 | for plugin in self.plugins: |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 80 | module = plugin['module'] |
Joshua Hesketh | 1217693 | 2013-09-17 13:34:46 +1000 | [diff] [blame] | 81 | self.tasks[module.__worker_name__] = module.Runner( |
| 82 | self.config, |
| 83 | plugin['plugin_config'] |
| 84 | ) |
Joshua Hesketh | a74f49d | 2013-09-06 15:52:49 +1000 | [diff] [blame] | 85 | self.tasks[module.__worker_name__].daemon = True |
| 86 | self.tasks[module.__worker_name__].start() |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 87 | |
| 88 | self.manager = worker_manager.GearmanManager(self.config, self.tasks) |
| 89 | self.manager.daemon = True |
| 90 | self.manager.start() |
| 91 | |
| 92 | def exit_handler(self, signum): |
| 93 | signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 94 | for task_name, task in self.tasks.items(): |
| 95 | task.stop() |
| 96 | self.manager.stop() |
| 97 | sys.exit(0) |
| 98 | |
| 99 | def main(self): |
| 100 | self.setup_logging() |
| 101 | self.run_tasks() |
| 102 | |
| 103 | while True: |
| 104 | try: |
| 105 | signal.pause() |
| 106 | except KeyboardInterrupt: |
| 107 | print "Ctrl + C: asking tasks to exit nicely...\n" |
| 108 | self.exit_handler(signal.SIGINT) |
| 109 | |
| 110 | |
| 111 | def main(): |
| 112 | parser = argparse.ArgumentParser() |
| 113 | parser.add_argument('-c', '--config', |
| 114 | default= |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 115 | '/etc/turbo-hipster/config.json', |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 116 | help='Path to json config file.') |
Joshua Hesketh | 05d0158 | 2013-09-09 15:16:08 +1000 | [diff] [blame] | 117 | parser.add_argument('-b', '--background', action='store_true', |
| 118 | help='Run as a daemon in the background.') |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 119 | parser.add_argument('-p', '--pidfile', |
Joshua Hesketh | 363d004 | 2013-07-26 11:44:07 +1000 | [diff] [blame] | 120 | default='/var/run/turbo-hipster/' |
Joshua Hesketh | e58ecb2 | 2013-09-06 12:39:46 +1000 | [diff] [blame] | 121 | 'turbo-hipster-worker-server.pid', |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 122 | help='PID file to lock during daemonization.') |
| 123 | args = parser.parse_args() |
| 124 | |
| 125 | with open(args.config, 'r') as config_stream: |
| 126 | config = json.load(config_stream) |
| 127 | |
| 128 | server = Server(config) |
| 129 | |
Joshua Hesketh | 478f151 | 2013-07-26 11:47:27 +1000 | [diff] [blame] | 130 | if args.background: |
Joshua Hesketh | 1377f6f | 2013-07-26 12:02:15 +1000 | [diff] [blame] | 131 | pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 132 | with daemon.DaemonContext(pidfile=pidfile): |
| 133 | server.main() |
Joshua Hesketh | 478f151 | 2013-07-26 11:47:27 +1000 | [diff] [blame] | 134 | else: |
| 135 | server.main() |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | if __name__ == '__main__': |
Joshua Hesketh | ac445b5 | 2013-08-14 12:55:22 +1000 | [diff] [blame] | 139 | sys.path.insert(0, os.path.abspath( |
| 140 | os.path.join(os.path.dirname(__file__), '../'))) |
Joshua Hesketh | 0ddd638 | 2013-07-26 10:33:36 +1000 | [diff] [blame] | 141 | main() |