Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
| 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. |
| 16 | |
| 17 | |
| 18 | import argparse |
| 19 | import daemon |
| 20 | import extras |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 21 | import os |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 22 | import signal |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 23 | import sys |
Joshua Hesketh | 1a1ac56 | 2014-05-27 15:21:40 +1000 | [diff] [blame] | 24 | import time |
Matthew Oliver | 1761669 | 2014-03-13 21:33:55 +1100 | [diff] [blame] | 25 | import yaml |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 26 | |
| 27 | from turbo_hipster import worker_server |
| 28 | |
| 29 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 30 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
| 31 | PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile']) |
| 32 | |
| 33 | |
Matthew Oliver | f94977b | 2014-04-09 15:24:32 +1000 | [diff] [blame] | 34 | def setup_server(args): |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 35 | |
| 36 | with open(args.config, 'r') as config_stream: |
Matthew Oliver | 1761669 | 2014-03-13 21:33:55 +1100 | [diff] [blame] | 37 | config = yaml.safe_load(config_stream) |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 38 | |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 39 | if not config['debug_log']: |
| 40 | # NOTE(mikal): debug logging _must_ be enabled for the log writing |
| 41 | # in lib.utils.execute_to_log to work correctly. |
| 42 | raise Exception('Debug log not configured') |
| 43 | |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 44 | server = worker_server.Server(config) |
Joshua Hesketh | 5cd8ea3 | 2014-03-27 15:28:00 +1100 | [diff] [blame] | 45 | server.setup_logging(config['debug_log']) |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 46 | |
| 47 | def term_handler(signum, frame): |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 48 | server.shutdown() |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 49 | signal.signal(signal.SIGTERM, term_handler) |
| 50 | |
| 51 | if args.background: |
| 52 | server.daemon = True |
| 53 | server.start() |
| 54 | |
| 55 | while not server.stopped(): |
| 56 | try: |
Joshua Hesketh | 1a1ac56 | 2014-05-27 15:21:40 +1000 | [diff] [blame] | 57 | time.sleep(3) |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 58 | except KeyboardInterrupt: |
| 59 | print "Ctrl + C: asking tasks to exit nicely...\n" |
Joshua Hesketh | 96adb28 | 2014-03-25 16:26:45 +1100 | [diff] [blame] | 60 | server.shutdown() |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 61 | |
| 62 | |
Matthew Oliver | f94977b | 2014-04-09 15:24:32 +1000 | [diff] [blame] | 63 | def main(): |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame] | 64 | sys.path.insert(0, os.path.abspath( |
| 65 | os.path.join(os.path.dirname(__file__), '../'))) |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 66 | parser = argparse.ArgumentParser() |
| 67 | parser.add_argument('-c', '--config', |
Joshua Hesketh | a4b178d | 2015-06-04 14:13:31 +1000 | [diff] [blame] | 68 | default='/etc/turbo-hipster/config.yaml', |
Matthew Oliver | 1761669 | 2014-03-13 21:33:55 +1100 | [diff] [blame] | 69 | help='Path to yaml config file.') |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 70 | parser.add_argument('-b', '--background', action='store_true', |
| 71 | help='Run as a daemon in the background.') |
| 72 | parser.add_argument('-p', '--pidfile', |
| 73 | default='/var/run/turbo-hipster/' |
| 74 | 'turbo-hipster-worker-server.pid', |
| 75 | help='PID file to lock during daemonization.') |
| 76 | args = parser.parse_args() |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 77 | if args.background: |
| 78 | pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10) |
| 79 | with daemon.DaemonContext(pidfile=pidfile): |
Matthew Oliver | f94977b | 2014-04-09 15:24:32 +1000 | [diff] [blame] | 80 | setup_server(args) |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 81 | else: |
Matthew Oliver | f94977b | 2014-04-09 15:24:32 +1000 | [diff] [blame] | 82 | setup_server(args) |
| 83 | |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | main() |