2023-08-27 16:27:28 +03:00
|
|
|
import json
|
|
|
|
|
|
|
|
import redis.asyncio as redis
|
|
|
|
|
|
|
|
|
|
|
|
class RedisClient:
|
|
|
|
SET_NAME = "queue"
|
|
|
|
TASKS_NAME = "tasks_working"
|
|
|
|
TASKS_DONE_NAME = "tasks_done"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.connection = redis.Redis(host="localhost", port=6379, db=0)
|
|
|
|
|
|
|
|
async def _set_task(self, task: dict) -> int:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.set(f'{self.TASKS_NAME}:{task["link"]}', json.dumps(task, indent=4).encode('utf-8'))
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def _set_task_done(self, task: dict) -> int:
|
|
|
|
async with self.connection as connection:
|
2023-09-20 14:43:59 +03:00
|
|
|
res = await connection.sadd(
|
|
|
|
f'{self.TASKS_DONE_NAME}:1',
|
2023-08-27 16:27:28 +03:00
|
|
|
json.dumps(task, indent=4).encode('utf-8')
|
|
|
|
)
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def _del_task(self, task: dict) -> int:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.delete(f'{self.TASKS_NAME}:{task["link"]}')
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def set_task_to_queue(self, task: dict) -> int:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.sadd(self.SET_NAME, json.dumps(task, indent=4).encode('utf-8'))
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def get_queue(self) -> set:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.smembers(self.SET_NAME)
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def del_task_from_queue(self, task: dict) -> int:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.srem(self.SET_NAME, json.dumps(task, indent=4).encode('utf-8'))
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def del_task_from_queue_and_add_to_tasks(self, task: dict) -> int:
|
|
|
|
|
|
|
|
await self.del_task_from_queue(task)
|
|
|
|
return await self._set_task(task)
|
|
|
|
|
|
|
|
async def del_task_from_tasks_and_add_to_task_done(self, task: dict) -> int:
|
|
|
|
await self._del_task(task)
|
|
|
|
return await self._set_task_done(task)
|
|
|
|
|
2023-09-20 14:43:59 +03:00
|
|
|
async def get_task_done_queue(self) -> set:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.smembers(self.TASKS_DONE_NAME + f":1")
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def del_task_from_task_done_queue(self, task) -> int:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.srem(self.TASKS_DONE_NAME + f":1", json.dumps(task, indent=4).encode('utf-8'))
|
|
|
|
return res
|
|
|
|
|
|
|
|
async def get_tasks_queue(self) -> set:
|
|
|
|
async with self.connection as connection:
|
|
|
|
res = await connection.json().get(self.TASKS_NAME)
|
|
|
|
return res
|
|
|
|
|
|
|
|
|