blob: b40cbe7fb248fe731714e04924c7a9e422bd155c [file] [log] [blame]
Tony Mack9b7a8bd2013-06-24 15:08:01 -04001#!/usr/bin/env python
2import os
Tony Mack2c911102014-04-16 19:52:09 -04003import argparse
Scott Baker86e132c2015-02-11 21:38:09 -08004os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
Sapan Bhatia16be1432016-01-14 11:41:38 -05005from synchronizers.base.backend import Backend
Scott Baker86e132c2015-02-11 21:38:09 -08006from xos.config import Config, DEFAULT_CONFIG_FN
Sapan Bhatia8660adf2016-01-11 16:18:47 -05007from core.models import Instance
8from util.logger import Logger, logging, logger
9from django.db import ProgrammingError
10import time
Scott Bakerf80c2be2014-09-16 17:34:21 -070011
12try:
13 from django import setup as django_setup # django 1.7
14except:
15 django_setup = False
Tony Mack9b7a8bd2013-06-24 15:08:01 -040016
Tony Mack2c911102014-04-16 19:52:09 -040017config = Config()
18
19# after http://www.erlenstar.demon.co.uk/unix/faq_2.html
20def daemon():
21 """Daemonize the current process."""
22 if os.fork() != 0: os._exit(0)
23 os.setsid()
24 if os.fork() != 0: os._exit(0)
25 os.umask(0)
26 devnull = os.open(os.devnull, os.O_RDWR)
27 os.dup2(devnull, 0)
28 # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull
Tony Mack6eb1ef82014-04-16 20:47:20 -040029 logdir=os.path.dirname(config.observer_logfile)
Tony Mack2c911102014-04-16 19:52:09 -040030 # when installed in standalone we might not have httpd installed
31 if not os.path.isdir(logdir): os.mkdir(logdir)
Tony Mack6eb1ef82014-04-16 20:47:20 -040032 crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644)
Tony Mack2c911102014-04-16 19:52:09 -040033 os.dup2(crashlog, 1)
34 os.dup2(crashlog, 2)
35
Scott Baker93639462015-02-19 13:50:06 -080036 if hasattr(config, "observer_pidfile"):
37 pidfile = config.get("observer_pidfile")
38 else:
39 pidfile = "/var/run/xosobserver.pid"
40 try:
41 file(pidfile,"w").write(str(os.getpid()))
42 except:
43 print "failed to create pidfile %s" % pidfile
44
Tony Mack2c911102014-04-16 19:52:09 -040045def main():
46 # Generate command line parser
47 parser = argparse.ArgumentParser(usage='%(prog)s [options]')
Scott Bakerf80c2be2014-09-16 17:34:21 -070048 parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False,
Tony Mack2c911102014-04-16 19:52:09 -040049 help='Run as daemon.')
Scott Baker6ef76152014-04-30 09:40:23 -070050 # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid
51 # throwing unrecognized argument exceptions
Scott Bakerd71335d2015-02-02 15:10:13 -080052 parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN,
Scott Baker6ef76152014-04-30 09:40:23 -070053 help='Name of config file.')
Tony Mack2c911102014-04-16 19:52:09 -040054 args = parser.parse_args()
Scott Bakerf80c2be2014-09-16 17:34:21 -070055
Tony Mack2c911102014-04-16 19:52:09 -040056 if args.daemon: daemon()
Tony Mack9b7a8bd2013-06-24 15:08:01 -040057
Scott Bakerf80c2be2014-09-16 17:34:21 -070058 if django_setup: # 1.7
59 django_setup()
60
Sapan Bhatia8660adf2016-01-11 16:18:47 -050061 models_active = False
62 wait = False
63 while not models_active:
64 try:
65 _ = Instance.objects.first()
66 models_active = True
67 except ProgrammingError:
68 logger.info('Waiting for data model to come up before starting...')
69 wait = True
70
71 if (wait):
72 time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up.
Tony Mack9b7a8bd2013-06-24 15:08:01 -040073 backend = Backend()
Tony Mack2c911102014-04-16 19:52:09 -040074 backend.run()
75
76if __name__ == '__main__':
77
78 main()