Skip to content

Commit

Permalink
refactor: avoid div/0 fault, fix duplicate log entry for results w/errs
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Apr 11, 2024
1 parent 77a11aa commit 9ca7692
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions silverback/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ def pre_execute(self, message: TaskiqMessage) -> TaskiqMessage:
return message

def post_execute(self, message: TaskiqMessage, result: TaskiqResult):
percentage_time = 100 * (result.execution_time / self.block_time)
logger.success(
f"{self._create_label(message)} "
f"- {result.execution_time:.3f}s ({percentage_time:.1f}%)"
if self.block_time:
percentage_time = 100 * (result.execution_time / self.block_time)
percent_display = f" ({percentage_time:.1f}%)"

else:
percent_display = ""

(logger.error if result.error else logger.success)(
f"{self._create_label(message)} " f"- {result.execution_time:.3f}s{percent_display}"
)

async def post_save(self, message: TaskiqMessage, result: TaskiqResult):
Expand All @@ -107,15 +112,4 @@ async def post_save(self, message: TaskiqMessage, result: TaskiqResult):
except Exception as err:
logger.error(f"Error storing result: {err}")

async def on_error(
self,
message: TaskiqMessage,
result: TaskiqResult,
exception: BaseException,
):
percentage_time = 100 * (result.execution_time / self.block_time)
logger.error(
f"{self._create_label(message)} "
f"- {result.execution_time:.3f}s ({percentage_time:.1f}%)"
)
# NOTE: Unless stdout is ignored, error traceback appears in stdout
# NOTE: Unless stdout is ignored, error traceback appears in stdout, no need for `on_error`

0 comments on commit 9ca7692

Please sign in to comment.