From 6ab70c760230948ced6458ccbf5fc5f61f1a5c09 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 14 May 2024 18:06:27 +0200 Subject: [PATCH] better att to queue, track score --- tubearchivist/home/src/ta/ta_redis.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tubearchivist/home/src/ta/ta_redis.py b/tubearchivist/home/src/ta/ta_redis.py index dd9ac511..b6c6af4b 100644 --- a/tubearchivist/home/src/ta/ta_redis.py +++ b/tubearchivist/home/src/ta/ta_redis.py @@ -127,11 +127,25 @@ class RedisQueue(RedisBase): return False + def add(self, to_add: str) -> None: + """add single item to queue""" + next_score = self._get_next_score() + self.conn.zadd(self.key, {to_add: next_score}) + def add_list(self, to_add: list) -> None: """add list to queue""" - mapping = {i: "+inf" for i in to_add} + next_score = self._get_next_score() + mapping = {i[1]: next_score + i[0] for i in enumerate(to_add)} self.conn.zadd(self.key, mapping) + def _get_next_score(self) -> float: + """get next score in queue to append""" + last = self.conn.zrange(self.key, -1, -1, withscores=True) + if not last: + return 0.0 + + return last[0][1] + 1 + def get_next(self) -> str | bool: """return next element in the queue, if available""" result = self.conn.zpopmin(self.key)