Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding workaround for rasterize command #37258

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions Packs/rasterize/Integrations/rasterize/rasterize.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
f'--user-agent="{USER_AGENT}"',
]

USE_ONLY_ONE_CHROME_PORT = demisto.params().get('using_one_chrome_port', True)
WITH_ERRORS = demisto.params().get('with_error', True)
IS_HTTPS = argToBoolean(demisto.params().get('is_https', False))

Expand Down Expand Up @@ -560,6 +561,58 @@ def chrome_manager() -> tuple[Any | None, str | None]:
return browser, chrome_port


def chrome_manager_one_port() -> tuple[Any | None, str | None]:
"""
Manages Chrome instances based on user-specified chrome options and integration instance ID.

This function performs the following steps:
1. Retrieves the instance ID of the integration and the Chrome options set by the user.
2. Checks if the instance ID has been used previously.
- If the instance ID is new, generates a new Chrome instance with the specified Chrome options.
- If the instance ID has been used:
- If the current Chrome options differ from the saved options for this instance ID,
it terminates the existing Chrome instance and generates a new one with the new options.
- If the current Chrome options match the saved options for this instance ID,
it reuses the existing Chrome instance.

Returns:
tuple[Any | None, int | None]: A tuple containing:
- The Browser or None if an error occurred.
- The chrome port or None if an error occurred.
"""
# If instance_id or chrome_options are not set, assign 'None' to these variables.
# This way, when fetching the content from the file, if there was no instance_id or chrome_options before,
# it can compare between the fetched 'None' string and the 'None' that assigned.
instance_id = demisto.callingContext.get('context', {}).get('IntegrationInstanceID', 'None') or 'None'
chrome_options = demisto.params().get('chrome_options', 'None')
chrome_instances_contents = read_json_file(CHROME_INSTANCES_FILE_PATH)
demisto.debug(f'[test] chrome_manager {chrome_instances_contents=} {chrome_options=} {instance_id=}')
chrome_options_dict = {
value[CHROME_INSTANCE_OPTIONS]: {
'chrome_port': key
}
for key, value in chrome_instances_contents.items()
}
chrome_port = chrome_options_dict.get(chrome_options, {}).get('chrome_port', '')
demisto.debug(f'[test] chrome_manager {chrome_options_dict=} \n {chrome_instances_contents=}')
if not chrome_instances_contents: # or instance_id not in chrome_options_dict.keys():
demisto.debug('[test] chrome_manager: first condition- chrome_instances_contents empty')
return generate_new_chrome_instance(instance_id, chrome_options)
if chrome_options in chrome_options_dict:
demisto.debug('[test] chrome_manager: second condition chrome_options in chrome_options_dict='
f'{chrome_options in chrome_options_dict}')
browser = get_chrome_browser(chrome_port)
return browser, chrome_port
for chrome_port_ in chrome_instances_contents:
if chrome_port_ == 'None':
terminate_port_chrome_instances_file(chrome_port_)
demisto.debug(f"chrome_manager {chrome_port_=}, removing the port from chrome_instances file")
continue
demisto.debug(f"chrome_manager {chrome_port_=}, terminating the port")
terminate_chrome(chrome_port=chrome_port_)
return generate_new_chrome_instance(instance_id, chrome_options)


def generate_new_chrome_instance(instance_id: str, chrome_options: str) -> tuple[Any | None, str | None]:
chrome_port = generate_chrome_port()
return start_chrome_headless(chrome_port, instance_id, chrome_options)
Expand Down Expand Up @@ -841,7 +894,7 @@ def perform_rasterize(path: str | list[str],
return None

demisto.debug(f"perform_rasterize, {paths=}, {rasterize_type=}")
browser, chrome_port = chrome_manager()
browser, chrome_port = chrome_manager() if not USE_ONLY_ONE_CHROME_PORT else chrome_manager_one_port()

if browser:
support_multithreading()
Expand Down Expand Up @@ -1133,7 +1186,6 @@ def get_width_height(args: dict):
def main(): # pragma: no cover
demisto.debug(f"main, {demisto.command()=}")
demisto.debug(f'Using performance params: {MAX_CHROMES_COUNT=}, {MAX_CHROME_TABS_COUNT=}, {MAX_RASTERIZATIONS_COUNT=}')

threading.excepthook = excepthook_recv_loop
try:
if demisto.command() == 'test-module':
Expand Down
6 changes: 6 additions & 0 deletions Packs/rasterize/Integrations/rasterize/rasterize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ configuration:
defaultvalue:
type: 0
additionalinfo: Add or remove Chrome options used to rasterize. Use for advanced troubleshooting. See Help.
- name: using_one_chrome_port
display: Use only one chrome port
required: false
defaultvalue: false
type: 0
additionalinfo: 'In case of the "Could not use rasterize command" error, please use the using_one_chrome_port option.'
- name: max_chromes_count
display: Number of maximum Chrome instances to keep running simultaneously.
required: false
Expand Down
6 changes: 6 additions & 0 deletions Packs/rasterize/ReleaseNotes/2_0_27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Integrations

##### Rasterize

Added a workaround for the error **"Could not use local Chrome for rasterize command"** by using the ***using_one_chrome_port option***, until a fix is released by the Google team for running Chrome instance processes in parallel.
2 changes: 1 addition & 1 deletion Packs/rasterize/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Rasterize",
"description": "Converts URLs, PDF files, and emails to an image file or PDF file.",
"support": "xsoar",
"currentVersion": "2.0.26",
"currentVersion": "2.0.27",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
Loading