summaryrefslogtreecommitdiff
path: root/tools/merge
blob: 4ba0438bb8c38580b9ab9eab7178a34ede475e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/env python
"""merge"""

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

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):
    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())