browsertrix/backend/btrixcloud/migrations/migration_0047_scale_to_browser_windows.py
Tessa Walsh dc41468daf
Allow users to run crawls with 1 or 2 browser windows (#2627)
Fixes #2425 

## Changed

- Switch backend to primarily using number of browser windows rather
than scale multiplier (including migration to calculate `browserWindows`
from `scale` for existing workflows and crawls)
- Still support `scale` in addition to `browserWindows` in input models
for creating and updating workflows and re-adjusting live crawl scale
for backwards compatibility
- Adds new `max_browser_windows` value to Helm chart, but calculates the
value from `max_crawl_scale` as fallback for users with that value
already set in local charts
- Rework frontend to allow users to select multiples of
`crawler_browser_instances` or any value below
`crawler_browser_instances` for browser windows. For instance, with
`crawler_browser_instances=4` and `max_browser_windows=8`, the user
would be presented with the following options: 1, 2, 3, 4, 8
- Sets maximum width of screencast to image width returned by `message`

---------

Co-authored-by: Ilya Kreymer <ikreymer@gmail.com>
Co-authored-by: sua yoo <sua@suayoo.com>
Co-authored-by: Ilya Kreymer <ikreymer@users.noreply.github.com>
2025-06-03 13:37:30 -07:00

63 lines
2.0 KiB
Python

"""
Migration 0047 - Convert scale to browserWindows
"""
from btrixcloud.migrations import BaseMigration
from btrixcloud.utils import browser_windows_from_scale
MIGRATION_VERSION = "0047"
# pylint: disable=duplicate-code
class Migration(BaseMigration):
"""Migration class."""
# pylint: disable=unused-argument
def __init__(self, mdb, **kwargs):
super().__init__(mdb, migration_version=MIGRATION_VERSION)
async def migrate_up(self):
"""Perform migration up.
Calculate and store browserWindows from existing scale on workflows and crawls
"""
configs_mdb = self.mdb["crawl_configs"]
crawls_mdb = self.mdb["crawls"]
async for config_raw in configs_mdb.find({"browserWindows": None}):
config_id = config_raw["_id"]
scale = config_raw.get("scale", 1)
try:
await configs_mdb.find_one_and_update(
{"_id": config_id},
{
"$set": {"browserWindows": browser_windows_from_scale(scale)},
},
)
# pylint: disable=broad-exception-caught
except Exception as err:
print(
f"Unable to set browser windows from scale for workflow {config_id}: {err}",
flush=True,
)
async for crawl_raw in crawls_mdb.find({"browserWindows": None}):
crawl_id = crawl_raw["_id"]
scale = crawl_raw.get("scale", 1)
try:
await crawls_mdb.find_one_and_update(
{"_id": crawl_id},
{
"$set": {"browserWindows": browser_windows_from_scale(scale)},
},
)
# pylint: disable=broad-exception-caught
except Exception as err:
print(
f"Unable to set browser windows from scale for crawl {crawl_id}: {err}",
flush=True,
)