diff options
author | Linus Nordberg <linus@nordu.net> | 2016-11-25 14:29:00 +0100 |
---|---|---|
committer | Linus Nordberg <linus@nordu.net> | 2016-11-25 14:37:29 +0100 |
commit | 616be2a18cea1ecfee327105a58f4f397a7e28f1 (patch) | |
tree | 520ea9a3fbe29ac78ce76c4b4527a70e8a70ae1d /tools/merge | |
parent | d8ce0c149f10d841dc19cbc32c8fc844aa4d65fb (diff) |
Manual rebase onto master with map/robust-distribution.
Diffstat (limited to 'tools/merge')
-rwxr-xr-x | tools/merge | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/tools/merge b/tools/merge index b5a50d5..4ba0438 100755 --- a/tools/merge +++ b/tools/merge @@ -1,10 +1,69 @@ -#! /bin/sh +#! /usr/bin/env python +"""merge""" -set -o errexit +import os +import sys +import signal +from time import sleep +from mergetools import parse_args, terminate_child_procs +from multiprocessing import Process +import merge_fetch, merge_backup, merge_sth, merge_dist -BINDIR=$(dirname $0) +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 -$BINDIR/merge_fetch.py "$@" -$BINDIR/merge_backup.py "$@" -$BINDIR/merge_sth.py "$@" -$BINDIR/merge_dist.py "$@" +def term(signal, arg): + terminate_child_procs() + 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].start() + #print >>sys.stderr, "DEBUG:", part, "started, pid", procs[part].pid + + if pidfilepath: + open(pidfilepath, 'w').write(str(os.getpid()) + '\n') + + signal.signal(signal.SIGTERM, term) + retval = 0 + keep_going = True + while keep_going: + sleep(1) + for name, p in procs.items(): + if not p.is_alive(): + print >>sys.stderr, "\nERROR:", name, "process is gone; exiting" + retval = 1 # Fail. + keep_going = False + break + + terminate_child_procs() + return retval + +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()) |