File size: 1,506 Bytes
503b0e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import os, signal, socket, subprocess, sys, time

DBOPS_ROOT = os.environ.get('DBOPS_ROOT', '/data/adaptai/platform/dbops')
CFG = os.path.join(DBOPS_ROOT, 'run', 'janusgraph', 'gremlin-server.runtime.yaml')
BIN_DIR = os.path.join(DBOPS_ROOT, 'binaries', 'janusgraph', 'bin')
STARTER = os.path.join(DBOPS_ROOT, 'tools', 'start_janusgraph_daemon.sh')

stop_requested = False

def handle_signal(signum, frame):
    global stop_requested
    stop_requested = True

signal.signal(signal.SIGTERM, handle_signal)
signal.signal(signal.SIGINT, handle_signal)

def port_open(host='127.0.0.1', port=17002, timeout=1.0):
    try:
        with socket.create_connection((host, port), timeout=timeout):
            return True
    except Exception:
        return False

def stop_jg():
    for name in ('gremlin-server.sh', 'janusgraph-server.sh'):
        p = os.path.join(BIN_DIR, name)
        if os.path.exists(p):
            try:
                subprocess.run([p, 'stop', CFG], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            except Exception:
                pass

def ensure_started():
    # Render + start (daemon)
    subprocess.run([STARTER], check=True)

def main():
    ensure_started()
    # Monitor
    while not stop_requested:
        if not port_open():
            # Exit non-zero to let Supervisor restart us
            sys.exit(1)
        time.sleep(2)
    # Stop requested
    stop_jg()
    sys.exit(0)

if __name__ == '__main__':
    main()