import sys
import os
import signal

def write_stderr(msg):
    sys.stderr.write(msg + '\n')
    sys.stderr.flush()

def main():
    while True:
        sys.stdout.write('READY\n')
        sys.stdout.flush()

        line = sys.stdin.readline()
        write_stderr('Header: ' + repr(line))

        headers = dict(x.split(':', 1) for x in line.strip().split())
        payload = sys.stdin.read(int(headers['len']))
        write_stderr('Payload: ' + repr(payload))

        pdata = dict(x.split(':', 1) for x in payload.strip().split())

        if pdata.get('processname') == 'model-server':
            write_stderr('model-server exited, killing supervisord')
            sys.stdout.write('RESULT 2\nOK')
            sys.stdout.flush()
            os.kill(1, signal.SIGTERM)
        else:
            write_stderr('Ignoring event for: ' + pdata.get('processname', 'unknown'))
            sys.stdout.write('RESULT 2\nOK')
            sys.stdout.flush()

main()
