Skip to content

Commit

Permalink
Refactor _get_collection_name to use time module and switch to time.m…
Browse files Browse the repository at this point in the history
…onotonic()
  • Loading branch information
smalyu committed Sep 19, 2024
1 parent 7239567 commit 4dc582a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions sender_messages.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import json
import logging
from datetime import datetime, timedelta
import time

from aiohttp import ClientSession
from motor import motor_asyncio
Expand Down Expand Up @@ -59,8 +59,8 @@ def _prepare_data(self, text: str, photo_token: str | None, reply_markup: dict |

def _get_collection_name(self) -> str:
"""Creates a unique name for the MongoDB collection using Moscow time."""
moscow_time = datetime.utcnow() + timedelta(hours=3)
return moscow_time.strftime('%d_%m_%Y__%H_%M_%S')
moscow_time = time.gmtime(time.time() + 3 * 60 * 60)
return time.strftime('%d_%m_%Y__%H_%M_%S', moscow_time)

async def _send_messages(self, data: dict, chat_ids: list[int]) -> (int, int):
"""Starts the message sending process with logging."""
Expand Down Expand Up @@ -108,7 +108,7 @@ async def _execute_batches(self, batches: list[list]) -> (int, int):
delivered, not_delivered = 0, 0

for batch in batches:
batch_start_time = datetime.utcnow().timestamp()
batch_start_time = time.monotonic()

results = await asyncio.gather(*batch, return_exceptions=True)
for result in results:
Expand All @@ -117,7 +117,7 @@ async def _execute_batches(self, batches: list[list]) -> (int, int):
else:
delivered += 1

time_elapsed = datetime.utcnow().timestamp() - batch_start_time
time_elapsed = time.monotonic() - batch_start_time
if time_elapsed < self._delay_between_batches:
await asyncio.sleep(self._delay_between_batches - time_elapsed)

Expand Down

0 comments on commit 4dc582a

Please sign in to comment.