tidy(events): use tuple index access for event payloads

This commit is contained in:
psychedelicious 2024-05-25 20:56:42 +10:00
parent ed6c716ddc
commit c0aabcd8ea
2 changed files with 11 additions and 18 deletions

View File

@ -119,13 +119,10 @@ class SocketIO:
await self._sio.leave_room(sid, BulkDownloadSubscriptionEvent(**data).bulk_download_id)
async def _handle_queue_event(self, event: FastAPIEvent[QueueEventBase]):
event_name, payload = event
await self._sio.emit(event=event_name, data=payload.model_dump(mode="json"), room=payload.queue_id)
await self._sio.emit(event=event[0], data=event[1].model_dump(mode="json"), room=event[1].queue_id)
async def _handle_model_event(self, event: FastAPIEvent[ModelEventBase]) -> None:
event_name, payload = event
await self._sio.emit(event=event_name, data=payload.model_dump(mode="json"))
await self._sio.emit(event=event[0], data=event[1].model_dump(mode="json"))
async def _handle_bulk_image_download_event(self, event: FastAPIEvent[BulkDownloadEventBase]) -> None:
event_name, payload = event
await self._sio.emit(event=event_name, data=payload.model_dump(mode="json"), room=payload.bulk_download_id)
await self._sio.emit(event=event[0], data=event[1].model_dump(mode="json"), room=event[1].bulk_download_id)

View File

@ -64,34 +64,30 @@ class SqliteSessionQueue(SessionQueueBase):
# When a queue item has an error, we get an error event, then a completed event.
# Mark the queue item completed only if it isn't already marked completed, e.g.
# by a previously-handled error event.
_event_name, payload = event
queue_item = self.get_queue_item(payload.item_id)
queue_item = self.get_queue_item(event[1].item_id)
if queue_item.status not in ["completed", "failed", "canceled"]:
self._set_queue_item_status(item_id=payload.item_id, status="completed")
self._set_queue_item_status(item_id=event[1].item_id, status="completed")
except SessionQueueItemNotFoundError:
pass
async def _handle_error_event(self, event: FastAPIEvent[InvocationErrorEvent]) -> None:
try:
_event_name, payload = event
# always set to failed if have an error, even if previously the item was marked completed or canceled
self._set_queue_item_status(
item_id=payload.item_id,
item_id=event[1].item_id,
status="failed",
error_type=payload.error_type,
error_message=payload.error_message,
error_traceback=payload.error_traceback,
error_type=event[1].error_type,
error_message=event[1].error_message,
error_traceback=event[1].error_traceback,
)
except SessionQueueItemNotFoundError:
pass
async def _handle_cancel_event(self, event: FastAPIEvent[SessionCanceledEvent]) -> None:
try:
_event_name, payload = event
queue_item = self.get_queue_item(payload.item_id)
queue_item = self.get_queue_item(event[1].item_id)
if queue_item.status not in ["completed", "failed", "canceled"]:
self._set_queue_item_status(item_id=payload.item_id, status="canceled")
self._set_queue_item_status(item_id=event[1].item_id, status="canceled")
except SessionQueueItemNotFoundError:
pass