#! /usr/bin/env python """merge""" import os import sys import signal from time import sleep from mergetools import parse_args from multiprocessing import Process import merge_fetch, merge_backup, merge_sth, merge_dist def run_once(): """Merge once.""" ret = merge_fetch.main() if ret == 0: ret = merge_backup.main() if ret == 0: ret = merge_sth.main() if ret == 0: ret = merge_dist.main() return ret def term(signal, arg): sys.exit(1) def run_continuously(pidfilepath): """Run continuously.""" parts = ('fetch', merge_fetch), ('backup', merge_backup), ('sth', merge_sth), ('dist', merge_dist) procs = {} for part, mod in parts: procs[part] = Process(target=mod.main, name='merge_%s' % part) procs[part].daemon = True procs[part].start() if pidfilepath: open(pidfilepath, 'w').write(str(os.getpid()) + '\n') signal.signal(signal.SIGTERM, term) while True: for name, p in procs.items(): if not p.is_alive(): print >>sys.stderr, "\nERROR:", name, "process is gone; exiting" return 1 sleep(1) def main(): """Main""" args, _, _ = parse_args() if args.mergeinterval is None: ret = run_once() else: ret = run_continuously(args.pidfile) return ret if __name__ == '__main__': sys.exit(main())