Exemplo n.º 1
0
from threading import Timer, Thread
from queue import Queue
import time

from backend.utils.log import get_logger

logger = get_logger("backend")

INITIALIZE = 'INITIALIZE'
PLAY = 'PLAY'
PAUSE = 'PAUSE'
STOP = 'STOP'
TIMERS_UP = 'TIMERS_UP'
FINISH = 'FINISH'

STATE_RUNNING = 'RUNNING'
STATE_PAUSED = 'PAUSED'
STATE_STOPPED = 'STOPPED'


class Controller(Thread):
    def __init__(self, client_proxy, preload_in_secs=15):
        # The important part of the state is composed by the following 2 members
        self._state = STATE_STOPPED
        self._remaining_in_secs = 0

        self.queue = Queue()
        self._timer = None
        self._preload_in_secs = preload_in_secs
        self._proxy = client_proxy
        self._current_event = None
import os
import time
from requests import get, post, exceptions

from backend.utils.log import get_logger

logger = get_logger("backend_debug")

SIMPLE_KV_HOST = os.environ.get("SIMPLE_KV_HOST", "simple_kv")
SIMPLE_KV_PORT = os.environ.get("SIMPLE_KV_PORT", 5002)
MAX_RETRIES = os.environ.get("SIMPLE_KV_MAX_RETRIES", 5)
SIMPLE_KV_URL = "http://{}:{}".format(SIMPLE_KV_HOST, SIMPLE_KV_PORT)


def ping(max_retries=MAX_RETRIES):
    """ Pings the Simple KV and retries up to MAX_RETRIES in case there's no answer """
    while True:
        try:
            logger.debug("Pinging the simpleKV to %s:%s", SIMPLE_KV_HOST,
                         SIMPLE_KV_PORT)
            r = get(SIMPLE_KV_URL + "/ping")
            r.raise_for_status()
        except (exceptions.ConnectionError, exceptions.ConnectTimeout) as err:
            if max_retries == 0:
                raise RuntimeError(
                    "PING to simple-kv failed after {} attempts: {}".format(
                        5, err))
            time.sleep(0.5)
            max_retries -= 1
            continue
        except exceptions.HTTPError as err: