|
3 | 3 |
|
4 | 4 |
|
5 | 5 | class RedisQueue:
|
6 |
| - def __init__(self, redis_client: redis.Redis, queue_id: str) -> None: |
| 6 | + def __init__( |
| 7 | + self, |
| 8 | + redis_client: redis.Redis, |
| 9 | + queue_id: str, |
| 10 | + expire: int | None = None, |
| 11 | + with_counter: bool = False, |
| 12 | + counter_pos_expire: int = 1, |
| 13 | + ) -> None: |
7 | 14 | self.redis_client = redis_client
|
8 | 15 | self.queue_id = queue_id
|
| 16 | + self.expire = expire |
| 17 | + self.with_counter = with_counter |
| 18 | + self.counter_pos_expire = counter_pos_expire |
9 | 19 |
|
10 |
| - async def enqueue(self, value: str, expire: int | None = None) -> None: |
11 |
| - pushed = await self.redis_client.rpush(self.queue_id, value) |
12 |
| - if expire is not None: |
13 |
| - await self.set_expire(expire) |
14 |
| - return pushed |
| 20 | + async def enqueue(self, value: str) -> int | None: |
| 21 | + await self.redis_client.rpush(self.queue_id, value) |
| 22 | + if self.expire is not None: |
| 23 | + await self.set_expire(self.expire) |
| 24 | + if self.with_counter: |
| 25 | + ctr = await self.redis_client.incr(f"ctr_enq:{self.queue_id}") |
| 26 | + await self.redis_client.set(f"pos:{value}", ctr, ex=self.counter_pos_expire) |
| 27 | + else: |
| 28 | + ctr = None |
| 29 | + return ctr |
15 | 30 |
|
16 |
| - async def dequeue(self, timeout: int = 1) -> str: |
17 |
| - return await self.redis_client.blpop(self.queue_id, timeout=timeout) |
| 31 | + async def dequeue(self, timeout: int = 1) -> str | None: |
| 32 | + val = await self.redis_client.blpop(self.queue_id, timeout=timeout) |
| 33 | + if val is not None and self.with_counter: |
| 34 | + await self.redis_client.incr(f"ctr_deq:{self.queue_id}") |
| 35 | + return val |
18 | 36 |
|
19 | 37 | async def set_expire(self, timeout: int) -> None:
|
20 | 38 | return await self.redis_client.expire(self.queue_id, timeout)
|
21 | 39 |
|
| 40 | + async def get_enq_counter(self) -> int: |
| 41 | + if not self.with_counter: |
| 42 | + return 0 |
| 43 | + enq = await self.redis_client.get(f"ctr_enq:{self.queue_id}") |
| 44 | + enq = int(enq) if enq is not None else 0 |
| 45 | + return enq |
22 | 46 |
|
23 |
| -def chat_queue(redis_client: redis.Redis, chat_id: str) -> RedisQueue: |
24 |
| - return RedisQueue(redis_client, f"chat:{chat_id}") |
| 47 | + async def get_deq_counter(self) -> int: |
| 48 | + if not self.with_counter: |
| 49 | + return 0 |
| 50 | + deq = await self.redis_client.get(f"ctr_deq:{self.queue_id}") |
| 51 | + deq = int(deq) if deq is not None else 0 |
| 52 | + return deq |
| 53 | + |
| 54 | + async def get_length(self) -> int: |
| 55 | + return await self.redis_client.llen(self.queue_id) |
| 56 | + |
| 57 | + |
| 58 | +async def get_pos_value(redis_client: redis.Redis, message_id: str) -> int: |
| 59 | + val = await redis_client.get(f"pos:{message_id}") |
| 60 | + if val is None: |
| 61 | + return 0 |
| 62 | + return int(val) |
25 | 63 |
|
26 | 64 |
|
27 | 65 | def message_queue(redis_client: redis.Redis, message_id: str) -> RedisQueue:
|
28 |
| - return RedisQueue(redis_client, f"message:{message_id}") |
| 66 | + return RedisQueue(redis_client, f"message:{message_id}", expire=settings.message_queue_expire) |
29 | 67 |
|
30 | 68 |
|
31 | 69 | def work_queue(redis_client: redis.Redis, worker_compat_hash: str) -> RedisQueue:
|
32 | 70 | if settings.allowed_worker_compat_hashes != "*":
|
33 | 71 | if worker_compat_hash not in settings.allowed_worker_compat_hashes_list:
|
34 | 72 | raise ValueError(f"Worker compat hash {worker_compat_hash} not allowed")
|
35 |
| - return RedisQueue(redis_client, f"work:{worker_compat_hash}") |
| 73 | + return RedisQueue( |
| 74 | + redis_client, f"work:{worker_compat_hash}", with_counter=True, counter_pos_expire=settings.message_queue_expire |
| 75 | + ) |
36 | 76 |
|
37 | 77 |
|
38 | 78 | def compliance_queue(redis_client: redis.Redis, worker_id: str) -> RedisQueue:
|
|
0 commit comments