AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): Add retry and error handling to block initialization (#11946)
Otto7dc53071e8a9
diff --git a/autogpt_platform/backend/backend/data/block.py b/autogpt_platform/backend/backend/data/block.pyindex 8d9ecfff4c..eb9360b037 100644--- a/autogpt_platform/backend/backend/data/block.py+++ b/autogpt_platform/backend/backend/data/block.py@@ -873,14 +873,13 @@ def is_block_auth_configured(async def initialize_blocks() -> None:- # First, sync all provider costs to blocks- # Imported here to avoid circular importfrom backend.sdk.cost_integration import sync_all_provider_costs+ from backend.util.retry import func_retrysync_all_provider_costs()- for cls in get_blocks().values():- block = cls()+ @func_retry+ async def sync_block_to_db(block: Block) -> None:existing_block = await AgentBlock.prisma().find_first(where={"OR": [{"id": block.id}, {"name": block.name}]})@@ -893,7 +892,7 @@ async def initialize_blocks() -> None:outputSchema=json.dumps(block.output_schema.jsonschema()),))- continue+ returninput_schema = json.dumps(block.input_schema.jsonschema())output_schema = json.dumps(block.output_schema.jsonschema())@@ -913,6 +912,25 @@ async def initialize_blocks() -> None:},)+ failed_blocks: list[str] = []+ for cls in get_blocks().values():+ block = cls()+ try:+ await sync_block_to_db(block)+ except Exception as e:+ logger.warning(+ f"Failed to sync block {block.name} to database: {e}. "+ "Block is still available in memory.",+ exc_info=True,+ )+ failed_blocks.append(block.name)++ if failed_blocks:+ logger.error(+ f"Failed to sync {len(failed_blocks)} block(s) to database: "+ f"{', '.join(failed_blocks)}. These blocks are still available in memory."+ )+# Note on the return type annotation: https://github.com/microsoft/pyright/issues/10281def get_block(block_id: str) -> AnyBlockSchema | None: