Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | import os |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 3 | import argparse |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 4 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
Sapan Bhatia | 16be143 | 2016-01-14 11:41:38 -0500 | [diff] [blame] | 5 | from synchronizers.base.backend import Backend |
Scott Baker | 86e132c | 2015-02-11 21:38:09 -0800 | [diff] [blame] | 6 | from xos.config import Config, DEFAULT_CONFIG_FN |
Sapan Bhatia | 8660adf | 2016-01-11 16:18:47 -0500 | [diff] [blame] | 7 | from core.models import Instance |
| 8 | from util.logger import Logger, logging, logger |
| 9 | from django.db import ProgrammingError |
| 10 | import time |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 11 | |
| 12 | try: |
| 13 | from django import setup as django_setup # django 1.7 |
| 14 | except: |
| 15 | django_setup = False |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 16 | |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 17 | config = Config() |
| 18 | |
| 19 | # after http://www.erlenstar.demon.co.uk/unix/faq_2.html |
| 20 | def 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 Mack | 6eb1ef8 | 2014-04-16 20:47:20 -0400 | [diff] [blame] | 29 | logdir=os.path.dirname(config.observer_logfile) |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 30 | # when installed in standalone we might not have httpd installed |
| 31 | if not os.path.isdir(logdir): os.mkdir(logdir) |
Tony Mack | 6eb1ef8 | 2014-04-16 20:47:20 -0400 | [diff] [blame] | 32 | crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644) |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 33 | os.dup2(crashlog, 1) |
| 34 | os.dup2(crashlog, 2) |
| 35 | |
Scott Baker | 9363946 | 2015-02-19 13:50:06 -0800 | [diff] [blame] | 36 | 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 Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 45 | def main(): |
| 46 | # Generate command line parser |
| 47 | parser = argparse.ArgumentParser(usage='%(prog)s [options]') |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 48 | parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False, |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 49 | help='Run as daemon.') |
Scott Baker | 6ef7615 | 2014-04-30 09:40:23 -0700 | [diff] [blame] | 50 | # 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 Baker | d71335d | 2015-02-02 15:10:13 -0800 | [diff] [blame] | 52 | parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN, |
Scott Baker | 6ef7615 | 2014-04-30 09:40:23 -0700 | [diff] [blame] | 53 | help='Name of config file.') |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 54 | args = parser.parse_args() |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 55 | |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 56 | if args.daemon: daemon() |
Tony Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 57 | |
Scott Baker | f80c2be | 2014-09-16 17:34:21 -0700 | [diff] [blame] | 58 | if django_setup: # 1.7 |
| 59 | django_setup() |
| 60 | |
Sapan Bhatia | 8660adf | 2016-01-11 16:18:47 -0500 | [diff] [blame] | 61 | 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 Mack | 9b7a8bd | 2013-06-24 15:08:01 -0400 | [diff] [blame] | 73 | backend = Backend() |
Tony Mack | 2c91110 | 2014-04-16 19:52:09 -0400 | [diff] [blame] | 74 | backend.run() |
| 75 | |
| 76 | if __name__ == '__main__': |
| 77 | |
| 78 | main() |