X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=ud-replicated;h=dd58a24ea5eeb803951635b19cd08940a34939fc;hb=606067cc38aa39ca0acbb7169f5e9cf87c351e1b;hp=26f8695eb82d9916a720f5a2e5eca51b86992690;hpb=b8723ee3aabc24e0fc9c9cfef1edb73b68b8a158;p=mirror%2Fuserdir-ldap.git diff --git a/ud-replicated b/ud-replicated index 26f8695..dd58a24 100755 --- a/ud-replicated +++ b/ud-replicated @@ -25,11 +25,13 @@ from dsa_mq.connection import Connection from dsa_mq.config import Config +import json import logging import logging.handlers import optparse import os import platform +import stat import subprocess import sys import time @@ -64,16 +66,30 @@ SFORMAT='ud-replicated[%(process)s]: %(message)s' logging.basicConfig(format=FORMAT, level=lvl) LOG = logging.getLogger(__name__) logsock = '/dev/log' -if os.path.exists('/var/run/log'): # Kfreebsd randomly different +if os.path.exists('/var/run/log') and stat.S_ISSOCK(os.stat('/var/run/log').st_mode): # Kfreebsd randomly different logsock = '/var/run/log' syslog_handler = logging.handlers.SysLogHandler(address = logsock) formatter = logging.Formatter(SFORMAT) syslog_handler.setFormatter(formatter) LOG.addHandler(syslog_handler) +last_run = 0 + def do_replicate(message): - last_update = time.time() - LOG.debug("Got message at %s" % last_update) + global last_run + last_update = int(time.time()) + timestamp = last_update + try: + message = json.loads(message) + except ValueError: + pass + + if isinstance(message, dict): + timestamp = message.get('timestamp', last_update) + message = message.get('message', 'update required') + LOG.debug("Got message at %s: %s" % (last_update, message)) + if last_run > timestamp: + return command = ['/usr/bin/ud-replicate'] if options.dryrun: @@ -88,8 +104,9 @@ def do_replicate(message): else: LOG.debug('%s finished with ret: 0' % ' '.join(command)) finally: - os.environ['TERM'] = old_term - LOG.debug(message) + if old_term is not None: + os.environ['TERM'] = old_term + last_run = last_update def main(): conn = Connection(conf=mq_conf) @@ -110,5 +127,7 @@ def main(): sys.exit(0) if __name__ == '__main__': - do_replicate('startup complete') + do_replicate(json.dumps( + {'timestamp': time.time(), + 'message': 'startup complete'})) main()