worker_started = False
worker_notify = None
callback = None
+ poll_started_at = None
@classmethod
- def start_worker(cls, poll=30):
+ def next_wait_time(cls):
+ now = datetime.datetime.now()
+ if cls.poll_started_at is None:
+ cls.poll_started_at = now
+ delta = now - cls.poll_started_at
+
+ if delta < datetime.timedelta(minutes=30):
+ return 30
+ elif delta < datetime.timedelta(hours=1):
+ return 60
+ elif delta < datetime.timedelta(hours=4):
+ return 5*60
+ elif delta < datetime.timedelta(days=1):
+ return 60*60
+ else:
+ raise "Too long waiting"
+
+ @classmethod
+ def start_worker(cls):
import threading
cls.worker = threading.Thread(name="portfolio", daemon=True,
- target=cls.wait_for_notification, kwargs={"poll": poll})
+ target=cls.wait_for_notification)
cls.worker_notify = threading.Event()
cls.callback = threading.Event()
return cls.worker == threading.current_thread()
@classmethod
- def wait_for_notification(cls, poll=30):
+ def wait_for_notification(cls):
if not cls.is_worker_thread():
raise RuntimeError("This method needs to be ran with the worker")
while cls.worker_started:
cls.report.print_log("[Worker] Fetching cryptoportfolio")
cls.get_cryptoportfolio(refetch=True)
cls.callback.set()
- time.sleep(poll)
+ time.sleep(cls.next_wait_time())
@classmethod
def stop_worker(cls):
cls.callback.wait()
@classmethod
- def wait_for_recent(cls, delta=4, poll=30):
+ def wait_for_recent(cls, delta=4):
cls.get_cryptoportfolio()
while cls.last_date.get() is None or datetime.datetime.now() - cls.last_date.get() > datetime.timedelta(delta):
if cls.worker is None:
- time.sleep(poll)
+ time.sleep(cls.next_wait_time())
cls.report.print_log("Attempt to fetch up-to-date cryptoportfolio")
cls.get_cryptoportfolio(refetch=True)
cls.retrieve_cryptoportfolio()
cls.get_cryptoportfolio()
liquidities = cls.liquidities.get(liquidity)
- return liquidities[cls.last_date.get()].copy()
+ if liquidities is not None and cls.last_date.get() in liquidities:
+ return liquidities[cls.last_date.get()].copy()
@classmethod
def get_cryptoportfolio(cls, refetch=False):
cls.data.set(r.json(parse_int=D, parse_float=D))
cls.parse_cryptoportfolio()
cls.store_cryptoportfolio()
- except (JSONDecodeError, SimpleJSONDecodeError):
+ except (AssertionError, JSONDecodeError, SimpleJSONDecodeError):
cls.data.set(None)
cls.last_date.set(None)
cls.liquidities.set({})
high_liquidity = parse_weights(cls.data.get("portfolio_1"))
medium_liquidity = parse_weights(cls.data.get("portfolio_2"))
+ assert len(high_liquidity) > 0
+ assert len(medium_liquidity) > 0
cls.liquidities.set({
"medium": medium_liquidity,
"high": high_liquidity,