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 |
| 21 | import json |
| 22 | import os |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame^] | 23 | import signal |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 24 | import sys |
| 25 | |
| 26 | from turbo_hipster import worker_server |
| 27 | |
| 28 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 29 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
| 30 | PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile']) |
| 31 | |
| 32 | |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame^] | 33 | def main(args): |
| 34 | |
| 35 | with open(args.config, 'r') as config_stream: |
| 36 | config = json.load(config_stream) |
| 37 | |
| 38 | server = worker_server.Server(config) |
| 39 | |
| 40 | def term_handler(signum, frame): |
| 41 | server.stop() |
| 42 | signal.signal(signal.SIGTERM, term_handler) |
| 43 | |
| 44 | if args.background: |
| 45 | server.daemon = True |
| 46 | server.start() |
| 47 | |
| 48 | while not server.stopped(): |
| 49 | try: |
| 50 | signal.pause() |
| 51 | except KeyboardInterrupt: |
| 52 | print "Ctrl + C: asking tasks to exit nicely...\n" |
| 53 | server.stop() |
| 54 | |
| 55 | |
| 56 | if __name__ == '__main__': |
| 57 | sys.path.insert(0, os.path.abspath( |
| 58 | os.path.join(os.path.dirname(__file__), '../'))) |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 59 | parser = argparse.ArgumentParser() |
| 60 | parser.add_argument('-c', '--config', |
| 61 | default= |
| 62 | '/etc/turbo-hipster/config.json', |
| 63 | help='Path to json config file.') |
| 64 | parser.add_argument('-b', '--background', action='store_true', |
| 65 | help='Run as a daemon in the background.') |
| 66 | parser.add_argument('-p', '--pidfile', |
| 67 | default='/var/run/turbo-hipster/' |
| 68 | 'turbo-hipster-worker-server.pid', |
| 69 | help='PID file to lock during daemonization.') |
| 70 | args = parser.parse_args() |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 71 | if args.background: |
| 72 | pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10) |
| 73 | with daemon.DaemonContext(pidfile=pidfile): |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame^] | 74 | main(args) |
Joshua Hesketh | 6f81333 | 2014-01-05 15:41:39 +1100 | [diff] [blame] | 75 | else: |
Joshua Hesketh | 1d8c4e5 | 2014-03-05 14:15:31 +1100 | [diff] [blame^] | 76 | main(args) |