#!{{ python_executable }}

import datetime
import json
import sys
import xmlrpclib

from supervisor import childutils


def write_stdout(s):
    sys.stdout.write(s)
    sys.stdout.flush()

def write_stderr(s):
    sys.stderr.write(s)
    sys.stderr.flush()


def write_slapgrid_result(process):
    server = xmlrpclib.Server("http://{{- supervisord['server'] -}}")
    # Tuple of tuples containing 2 elements : process name and the path of its info file
    json_files = dict([("slapgrid-sr", "{{- slaprunner['software_info_json'] -}}"),
                       ("slapgrid-cp", "{{- slaprunner['instance_info_json'] -}}")])
    info = server.supervisor.getProcessInfo(process)
    result = dict()
    result['last_build'] = datetime.datetime.fromtimestamp(info['stop']).strftime("%Y-%m-%d %H:%M:%S")
    result['success'] = info['exitstatus']
    open(json_files[process], 'w').write(json.dumps(result))


def main():
    while 1:
        headers, payload = writer.wait()
        try:
            processname = dict(x.split(':') for x in payload.split())['processname']
            write_slapgrid_result(processname)
        except:
            pass
        writer.ok()

if __name__ == '__main__':
    writer = childutils.EventListenerProtocol()
    main()
