Skip to content

Commit

Permalink
refactor: convert to TaskType for better processing
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed Apr 9, 2024
1 parent 2a7d62a commit 6aafc83
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions silverback/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,24 @@ def _create_label(self, message: TaskiqMessage) -> str:
return message.task_name

def pre_execute(self, message: TaskiqMessage) -> TaskiqMessage:
task_type = message.labels.pop("task_type", "<unknown>")
if not (task_type := message.labels.pop("task_type")):
return message # Not a silverback task

# NOTE: Don't compare `str` to `TaskType` using `is`
if task_type == TaskType.NEW_BLOCKS:
try:
task_type = TaskType(task_type)
except ValueError:
return message # Not a silverback task

# Add extra labels for our task to see what their source was
if task_type is TaskType.NEW_BLOCKS:
# NOTE: Necessary because we don't know the exact block class
message.args[0] = self.provider.network.ecosystem.decode_block(
hexbytes_dict(message.args[0])
)
message.labels["block_number"] = str(message.args[0].number)
message.labels["block_hash"] = message.args[0].hash.hex()

elif task_type == TaskType.EVENT_LOG:
elif task_type is TaskType.EVENT_LOG:
# NOTE: Just in case the user doesn't specify type as `ContractLog`
message.args[0] = ContractLog.model_validate(message.args[0])
message.labels["block_number"] = str(message.args[0].block_number)
Expand Down

0 comments on commit 6aafc83

Please sign in to comment.