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 |
| 23 | import sys |
| 24 | |
| 25 | from turbo_hipster import worker_server |
| 26 | |
| 27 | # as of python-daemon 1.6 it doesn't bundle pidlockfile anymore |
| 28 | # instead it depends on lockfile-0.9.1 which uses pidfile. |
| 29 | PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile']) |
| 30 | |
| 31 | |
| 32 | def main(): |
| 33 | parser = argparse.ArgumentParser() |
| 34 | parser.add_argument('-c', '--config', |
| 35 | default= |
| 36 | '/etc/turbo-hipster/config.json', |
| 37 | help='Path to json config file.') |
| 38 | parser.add_argument('-b', '--background', action='store_true', |
| 39 | help='Run as a daemon in the background.') |
| 40 | parser.add_argument('-p', '--pidfile', |
| 41 | default='/var/run/turbo-hipster/' |
| 42 | 'turbo-hipster-worker-server.pid', |
| 43 | help='PID file to lock during daemonization.') |
| 44 | args = parser.parse_args() |
| 45 | |
| 46 | with open(args.config, 'r') as config_stream: |
| 47 | config = json.load(config_stream) |
| 48 | |
| 49 | server = worker_server.Server(config) |
| 50 | |
| 51 | if args.background: |
| 52 | pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10) |
| 53 | with daemon.DaemonContext(pidfile=pidfile): |
| 54 | server.main() |
| 55 | else: |
| 56 | server.main() |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | sys.path.insert(0, os.path.abspath( |
| 61 | os.path.join(os.path.dirname(__file__), '../'))) |
| 62 | main() |