1010)
1111from hyperbrowser .exceptions import HyperbrowserError
1212from ....polling import (
13- has_exceeded_max_wait ,
13+ collect_paginated_results_async ,
1414 poll_until_terminal_status_async ,
1515 retry_operation_async ,
1616)
1717from ....schema_utils import inject_web_output_schemas
18- import asyncio
19- import time
2018
2119
2220class BatchFetchManager :
@@ -81,8 +79,6 @@ async def start_and_wait(
8179 retry_delay_seconds = 0.5 ,
8280 )
8381
84- failures = 0
85- page_fetch_start_time = time .monotonic ()
8682 job_response = BatchFetchJobResponse (
8783 jobId = job_id ,
8884 status = job_status ,
@@ -92,38 +88,28 @@ async def start_and_wait(
9288 totalPages = 0 ,
9389 batchSize = 100 ,
9490 )
95- first_check = True
9691
97- while (
98- first_check
99- or job_response .current_page_batch < job_response .total_page_batches
100- ):
101- if has_exceeded_max_wait (page_fetch_start_time , max_wait_seconds ):
102- raise HyperbrowserError (
103- f"Timed out fetching all pages for batch fetch job { job_id } after { max_wait_seconds } seconds"
104- )
105- try :
106- tmp_job_response = await self .get (
107- job_id ,
108- params = GetBatchFetchJobParams (
109- page = job_response .current_page_batch + 1 , batch_size = 100
110- ),
111- )
112- if tmp_job_response .data :
113- job_response .data .extend (tmp_job_response .data )
114- job_response .current_page_batch = tmp_job_response .current_page_batch
115- job_response .total_pages = tmp_job_response .total_pages
116- job_response .total_page_batches = tmp_job_response .total_page_batches
117- job_response .batch_size = tmp_job_response .batch_size
118- job_response .error = tmp_job_response .error
119- failures = 0
120- first_check = False
121- except Exception as e :
122- failures += 1
123- if failures >= POLLING_ATTEMPTS :
124- raise HyperbrowserError (
125- f"Failed to get batch page { job_response .current_page_batch } for job { job_id } after { POLLING_ATTEMPTS } attempts: { e } "
126- )
127- await asyncio .sleep (0.5 )
92+ def merge_page_response (page_response : BatchFetchJobResponse ) -> None :
93+ if page_response .data :
94+ job_response .data .extend (page_response .data )
95+ job_response .current_page_batch = page_response .current_page_batch
96+ job_response .total_pages = page_response .total_pages
97+ job_response .total_page_batches = page_response .total_page_batches
98+ job_response .batch_size = page_response .batch_size
99+ job_response .error = page_response .error
100+
101+ await collect_paginated_results_async (
102+ operation_name = f"batch fetch job { job_id } " ,
103+ get_next_page = lambda page : self .get (
104+ job_id ,
105+ params = GetBatchFetchJobParams (page = page , batch_size = 100 ),
106+ ),
107+ get_current_page_batch = lambda page_response : page_response .current_page_batch ,
108+ get_total_page_batches = lambda page_response : page_response .total_page_batches ,
109+ on_page_success = merge_page_response ,
110+ max_wait_seconds = max_wait_seconds ,
111+ max_attempts = POLLING_ATTEMPTS ,
112+ retry_delay_seconds = 0.5 ,
113+ )
128114
129115 return job_response
0 commit comments