from __future__ import print_function
import redis
import json
from time import sleep
from util.config import config


# for python 2, 3 compatible
def _parse_data(data):
    # python3
    if isinstance(data, bytes):
        return json.loads(data.decode('utf-8'))
    # python2
    return json.loads(data)


def _get_job_id_stdout_line(message, job_id):
    if not message or message['type'] != 'message':
        return None

    parsed_data = _parse_data(message['data'])
    if parsed_data.get('jobId', '') != job_id:
        return None

    return parsed_data.get('line', '')


def get_log_from_container(job_id):
    host = config['service_redis']['host']
    port = config['service_redis']['port']
    r = redis.StrictRedis(host=host,
                          port=port)
    p = r.pubsub()
    p.subscribe('SERVICE_LAYER')

    line = ''
    while line != '[SYSTEM] Train completed.':
        sleep(0.5)
        line = _get_job_id_stdout_line(p.get_message(), job_id)
        if line:
            print(line)
