Scott Baker | 1b3b37b | 2017-02-21 22:53:33 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | import os |
| 3 | import argparse |
| 4 | import sys |
| 5 | |
| 6 | sys.path.append('/opt/xos') |
| 7 | |
| 8 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings") |
| 9 | from xos.config import Config, DEFAULT_CONFIG_FN |
| 10 | from xos.logger import Logger, logging, logger |
| 11 | import time |
| 12 | |
| 13 | from synchronizers.new_base.modelaccessor import * |
| 14 | from synchronizers.new_base.backend import Backend |
| 15 | |
| 16 | config = Config() |
| 17 | |
| 18 | logger = Logger(level=logging.INFO) |
| 19 | |
| 20 | # after http://www.erlenstar.demon.co.uk/unix/faq_2.html |
| 21 | def daemon(): |
| 22 | """Daemonize the current process.""" |
| 23 | if os.fork() != 0: os._exit(0) |
| 24 | os.setsid() |
| 25 | if os.fork() != 0: os._exit(0) |
| 26 | os.umask(0) |
| 27 | devnull = os.open(os.devnull, os.O_RDWR) |
| 28 | os.dup2(devnull, 0) |
| 29 | # xxx fixme - this is just to make sure that nothing gets stupidly lost - should use devnull |
| 30 | logdir=os.path.dirname(config.observer_logfile) |
| 31 | # when installed in standalone we might not have httpd installed |
| 32 | if not os.path.isdir(logdir): os.mkdir(logdir) |
| 33 | crashlog = os.open('%s'%config.observer_logfile, os.O_RDWR | os.O_APPEND | os.O_CREAT, 0644) |
| 34 | os.dup2(crashlog, 1) |
| 35 | os.dup2(crashlog, 2) |
| 36 | |
| 37 | if hasattr(config, "observer_pidfile"): |
| 38 | pidfile = config.get("observer_pidfile") |
| 39 | else: |
| 40 | pidfile = "/var/run/xosobserver.pid" |
| 41 | try: |
| 42 | file(pidfile,"w").write(str(os.getpid())) |
| 43 | except: |
| 44 | print "failed to create pidfile %s" % pidfile |
| 45 | |
| 46 | def main(): |
| 47 | # Generate command line parser |
| 48 | parser = argparse.ArgumentParser(usage='%(prog)s [options]') |
| 49 | parser.add_argument('-d', '--daemon', dest='daemon', action='store_true', default=False, |
| 50 | help='Run as daemon.') |
| 51 | # smbaker: util/config.py parses sys.argv[] directly to get config file name; include the option here to avoid |
| 52 | # throwing unrecognized argument exceptions |
| 53 | parser.add_argument('-C', '--config', dest='config_file', action='store', default=DEFAULT_CONFIG_FN, |
| 54 | help='Name of config file.') |
| 55 | args = parser.parse_args() |
| 56 | |
| 57 | if args.daemon: daemon() |
| 58 | |
| 59 | models_active = False |
| 60 | wait = False |
| 61 | while not models_active: |
| 62 | try: |
| 63 | _ = Instance.objects.first() |
| 64 | _ = NetworkTemplate.objects.first() |
| 65 | models_active = True |
| 66 | except Exception,e: |
| 67 | logger.info(str(e)) |
| 68 | logger.info('Waiting for data model to come up before starting...') |
| 69 | time.sleep(10) |
| 70 | wait = True |
| 71 | |
| 72 | if (wait): |
| 73 | time.sleep(60) # Safety factor, seeing that we stumbled waiting for the data model to come up. |
| 74 | backend = Backend() |
| 75 | backend.run() |
| 76 | |
| 77 | if __name__ == '__main__': |
| 78 | |
| 79 | main() |