blob: 06f94156f5c1460d99acfb285c76537e34f7f6f9 [file] [log] [blame]
Joshua Hesketh6f813332014-01-05 15:41:39 +11001#!/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
18import argparse
19import daemon
20import extras
Joshua Hesketh6f813332014-01-05 15:41:39 +110021import os
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110022import signal
Joshua Hesketh6f813332014-01-05 15:41:39 +110023import sys
Joshua Hesketh1a1ac562014-05-27 15:21:40 +100024import time
Matthew Oliver17616692014-03-13 21:33:55 +110025import yaml
Joshua Hesketh6f813332014-01-05 15:41:39 +110026
27from 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.
31PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile'])
32
33
Matthew Oliverf94977b2014-04-09 15:24:32 +100034def setup_server(args):
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110035
36 with open(args.config, 'r') as config_stream:
Matthew Oliver17616692014-03-13 21:33:55 +110037 config = yaml.safe_load(config_stream)
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110038
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110039 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 Hesketh1d8c4e52014-03-05 14:15:31 +110044 server = worker_server.Server(config)
Joshua Hesketh5cd8ea32014-03-27 15:28:00 +110045 server.setup_logging(config['debug_log'])
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110046
47 def term_handler(signum, frame):
Joshua Hesketh96adb282014-03-25 16:26:45 +110048 server.shutdown()
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110049 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 Hesketh1a1ac562014-05-27 15:21:40 +100057 time.sleep(3)
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110058 except KeyboardInterrupt:
59 print "Ctrl + C: asking tasks to exit nicely...\n"
Joshua Hesketh96adb282014-03-25 16:26:45 +110060 server.shutdown()
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110061
62
Matthew Oliverf94977b2014-04-09 15:24:32 +100063def main():
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110064 sys.path.insert(0, os.path.abspath(
65 os.path.join(os.path.dirname(__file__), '../')))
Joshua Hesketh6f813332014-01-05 15:41:39 +110066 parser = argparse.ArgumentParser()
67 parser.add_argument('-c', '--config',
Joshua Hesketha4b178d2015-06-04 14:13:31 +100068 default='/etc/turbo-hipster/config.yaml',
Matthew Oliver17616692014-03-13 21:33:55 +110069 help='Path to yaml config file.')
Joshua Hesketh6f813332014-01-05 15:41:39 +110070 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 Hesketh6f813332014-01-05 15:41:39 +110077 if args.background:
78 pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10)
79 with daemon.DaemonContext(pidfile=pidfile):
Matthew Oliverf94977b2014-04-09 15:24:32 +100080 setup_server(args)
Joshua Hesketh6f813332014-01-05 15:41:39 +110081 else:
Matthew Oliverf94977b2014-04-09 15:24:32 +100082 setup_server(args)
83
84
85if __name__ == '__main__':
86 main()