blob: d54978d0d3698bde68f252513ebf8432499d6e0f [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
21import json
22import os
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110023import signal
Joshua Hesketh6f813332014-01-05 15:41:39 +110024import sys
25
26from 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.
30PID_FILE_MODULE = extras.try_imports(['daemon.pidlockfile', 'daemon.pidfile'])
31
32
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110033def 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
56if __name__ == '__main__':
57 sys.path.insert(0, os.path.abspath(
58 os.path.join(os.path.dirname(__file__), '../')))
Joshua Hesketh6f813332014-01-05 15:41:39 +110059 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 Hesketh6f813332014-01-05 15:41:39 +110071 if args.background:
72 pidfile = PID_FILE_MODULE.TimeoutPIDLockFile(args.pidfile, 10)
73 with daemon.DaemonContext(pidfile=pidfile):
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110074 main(args)
Joshua Hesketh6f813332014-01-05 15:41:39 +110075 else:
Joshua Hesketh1d8c4e52014-03-05 14:15:31 +110076 main(args)