Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore failed fetch requests from reminders #58

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions src/bot/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ async def start(self):
pass

async def check_for_updates(self):
applications_to_update = await self.db.fetch_applications_needing_update(
self.refresh,
self.not_found_refresh
)
applications_to_update = await self.db.fetch_applications_needing_update(self.refresh, self.not_found_refresh)

if not applications_to_update:
logger.info("No applications need status refresh")
Expand Down Expand Up @@ -71,23 +68,18 @@ async def expire_stale_not_found_applications(self):
for app in applications_to_expire:

message = {
"application_id": app['application_id'],
"application_id": app["application_id"],
"chat_id": app["chat_id"],
"number": app['application_number'],
"suffix": app['application_suffix'],
"type": app['application_type'],
"year": app['application_year'],
"number": app["application_number"],
"suffix": app["application_suffix"],
"type": app["application_type"],
"year": app["application_year"],
"request_type": "expire",
"last_updated": app["created_at"].isoformat() if app["created_at"] else "0",
}
oam_full_string = generate_oam_full_string(app)
logger.info(
f"Scheduling expiration for {oam_full_string}, user: {app['chat_id']}, created_at: {app['created_at']}"
)
await self.rabbit.publish_message(
message,
routing_key="ExpirationQueue"
)
logger.info(f"Scheduling expiration for {oam_full_string}, user: {app['chat_id']}, created_at: {app['created_at']}")
await self.rabbit.publish_message(message, routing_key="ExpirationQueue")

def stop(self):
self.shutdown_event.set()
Expand Down Expand Up @@ -129,6 +121,7 @@ async def trigger_reminders(self):
"force_refresh": True,
"failed": False,
"request_type": "fetch",
"is_reminder": True,
"last_updated": reminder["last_updated"].isoformat() if reminder["last_updated"] else "0",
}
oam_full_string = generate_oam_full_string(reminder)
Expand Down
22 changes: 16 additions & 6 deletions src/bot/rabbitmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def discard_message_id(self, unique_id):

def is_resolved(self, status):
"""Check if the application was resolved to its final status"""
final_statuses = MVCR_STATUSES.get('approved')[0] + MVCR_STATUSES.get('denied')[0]
final_statuses = MVCR_STATUSES.get("approved")[0] + MVCR_STATUSES.get("denied")[0]
return any(final_status in status for final_status in final_statuses)

def _generate_error_message(self, app_details, lang):
Expand All @@ -102,6 +102,7 @@ async def on_update_message(self, message: aio_pika.IncomingMessage):
force_refresh = msg_data.get("force_refresh", False)
failed = msg_data.get("failed", False)
request_type = msg_data.get("request_type", None)
is_reminder = msg_data.get("is_reminder", False)
has_changed = False
oam_full_string = generate_oam_full_string(msg_data)

Expand All @@ -122,7 +123,7 @@ async def on_update_message(self, message: aio_pika.IncomingMessage):
if failed and request_type == "refresh":
# Drop failed refresh requests with log message
# But do not update status in DB to avoid mass status rewrite
# in case of issues at fetcher
# in case of issues on fetchers
logger.warning(f"[REFRESH FAILED] Failed to refresh status {oam_full_string}, user {chat_id}")
return

Expand All @@ -142,8 +143,18 @@ async def on_update_message(self, message: aio_pika.IncomingMessage):
await self.db.update_last_checked(chat_id, number, type_, year)
return

if failed and request_type == "fetch":
is_resolved = True
if failed:
if request_type == "fetch":
# Tolerate failed fetch requests triggered by reminders, do not notify users / change status in db
if is_reminder:
logger.error(
f"[REMINDER] Failed to to fetch status for {oam_full_string}, "
f"user {chat_id}, status: {received_status}"
)
return
else:
# If we failed during initial application fetch, further processing is frozen
is_resolved = True
else:
is_resolved = self.is_resolved(received_status)

Expand Down Expand Up @@ -205,7 +216,7 @@ async def on_update_message(self, message: aio_pika.IncomingMessage):
async def on_expiration_message(self, message: aio_pika.IncomingMessage):
"""Async function to handle messages from ExpirationQueue"""
async with message.process():
msg_data = json.loads(message.body.decode('utf-8'))
msg_data = json.loads(message.body.decode("utf-8"))
application_id = msg_data.get("application_id")
chat_id = msg_data.get("chat_id")
oam_full_string = generate_oam_full_string(msg_data)
Expand All @@ -225,7 +236,6 @@ async def on_expiration_message(self, message: aio_pika.IncomingMessage):
except Exception as e:
logger.error(f"Failed to send expiration notification to {chat_id}: {e}")


async def on_service_message(self, message: aio_pika.IncomingMessage):
"""Async function to handle service messages from FetcherMetricsQueue"""
async with message.process():
Expand Down
1 change: 1 addition & 0 deletions src/bot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"in_progress": (["zpracovává se", "v-prubehu-rizeni"], "🟡"),
"approved": (["bylo <b>povoleno</b>", "rizeni-povoleno"], "🟢"),
"denied": (["bylo <b>nepovoleno</b>", "zamítlo", "zastavilo"], "🔴"),
"error": (["ERROR"], "🔴"),
}


Expand Down
Loading