From 71d6e0d0e6807a9b727f37900b11bdabeb593d7a Mon Sep 17 00:00:00 2001 From: j Date: Mon, 5 Feb 2018 11:47:38 +0100 Subject: [PATCH] only connect to pandora as needed --- etc/systemd/system/dd-re.service | 1 - recommendation_engine.py | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/etc/systemd/system/dd-re.service b/etc/systemd/system/dd-re.service index 8954489..3cc90c0 100644 --- a/etc/systemd/system/dd-re.service +++ b/etc/systemd/system/dd-re.service @@ -7,7 +7,6 @@ Restart=always User=dd Group=dd WorkingDirectory=/srv/dd/re -ExecStartPre=/usr/bin/wait-for-it -h pandora.dmp -p 80 ExecStart=/srv/dd/re/server.py [Install] diff --git a/recommendation_engine.py b/recommendation_engine.py index 3c59770..009c9b1 100644 --- a/recommendation_engine.py +++ b/recommendation_engine.py @@ -5,18 +5,24 @@ Recommendation Engine Example from collections import defaultdict import json +import logging import os import random +import time import ox from utils import run_async +logger = logging.getLogger(__name__) + + class Engine: + _pandora = None def __init__(self, path, **kwargs): self.path = path - self.pandora = Pandora( + self.pandora_args = dict( url=kwargs.get('pandora', 'http://pandora.dmp/api/'), username=kwargs.get('username', 'dd.re'), password=kwargs.get('password', 'dd.re') @@ -28,6 +34,16 @@ class Engine: else: self.playlists = [] + @property + def pandora(self): + while not self._pandora: + try: + self._pandora = Pandora(**self.pandora_args) + except: + logger.error('failed to connect to pandora, retry in 10 seconds') + time.sleep(10) + return self._pandora + def _patch_clips(self, clips): inpoints = {} for index, clip in enumerate(clips):