Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt when browser crash, make a new browser
Browse files Browse the repository at this point in the history
AlexCXC committed Dec 4, 2024
1 parent 7e6c815 commit 332ef75
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions dtable_events/convert_page/manager.py
Original file line number Diff line number Diff line change
@@ -28,16 +28,29 @@ def __init__(self, index, task_queue: Queue, pages=10):
self.context = None
self.pages = pages

self.is_browser_alive = False

self.loop = asyncio.new_event_loop() # each thread has own event loop

def disconnect_browser_cb(self):
self.is_browser_alive = False
self.browser = None
self.context = None
logger.error(f"Thread-{self.thread_id} browser disconnected... will use new browser")

async def get_context(self):
if not self.is_browser_alive:
logger.info(f"Thread-{self.thread_id} browser make a new browser...")

if self.context:
return self.context

if not self.playwright:
self.playwright = await async_playwright().start()
if not self.browser:
self.is_browser_alive = True
self.browser = await self.playwright.chromium.launch(headless=True)
self.browser.on('disconnected', self.disconnect_browser_cb)
self.context = await self.browser.new_context()
return self.context

@@ -134,15 +147,14 @@ async def convert_with_rows(self, task_info, resources):
table = resources.get('table')
target_column = resources.get('target_column')

context = await self.get_context()

# convert
# open all tabs of rows pages by pages
# wait render and convert to pdf one by one
pages = self.pages
dtable_server_api = DTableServerAPI('dtable-events', dtable_uuid, dtable_server_url)
for i in range(0, len(row_ids), pages):
tasks = []
context = await self.get_context()
# open rows
for row_id in row_ids[i: i+pages]:
url = ''
@@ -157,7 +169,7 @@ async def convert_with_rows(self, task_info, resources):
results = await asyncio.gather(*tasks, return_exceptions=True)
for result in results:
if isinstance(result, Exception):
logger.exception(result)
logger.exception(f'Thread-{self.thread_id} convert rows error: {e}')

# callbacks
if action_type == 'convert_page_to_pdf':

0 comments on commit 332ef75

Please sign in to comment.