browsertrix/backend/btrixcloud/migrations/migration_0022_partial_complete.py
Ilya Kreymer dfba4b3940
Replace partial_complete -> stopped_by_user or stopped_quota_reached + operator edge cases (#1368)
- Adds two new crawl finished state, stopped_by_user and
stopped_quota_reached
- Tracking other possible 'stop reasons' in operator, though not making
them distinct states for now.
- Updated frontend with 'Stopped by User' and 'Stopped: Time Quota
Reached', shown with same icon as current partial_complete
- Added migration of partial_complete to either stopped_by_user or
complete (no historical quota data available)
- Addresses edge case in scaling: if crawl never scaled (no redis entry,
no pod), automatically scale down
- Edge case in status: if crawl is somehow 'canceled' but not deleted,
immediately delete crawl object and begin finalizing.

---------
Co-authored-by: Tessa Walsh <tessa@bitarchivist.net>
2023-11-14 11:17:16 -08:00

43 lines
1.3 KiB
Python

"""
Migration 00233 -- Partial Complete
"""
from btrixcloud.migrations import BaseMigration
MIGRATION_VERSION = "0022"
class Migration(BaseMigration):
"""Migration class."""
def __init__(self, mdb, migration_version=MIGRATION_VERSION):
super().__init__(mdb, migration_version)
async def migrate_up(self):
"""Perform migration up.
Convert partial_complete -> complete, stopped_by_user or stopped_quota_reached
"""
# pylint: disable=duplicate-code
crawls = self.mdb["crawls"]
crawl_configs = self.mdb["crawl_configs"]
await crawls.update_many(
{"state": "partial_complete", "stopping": True},
{"$set": {"state": "stopped_by_user"}},
)
await crawls.update_many(
{"state": "partial_complete", "stopping": {"$ne": True}},
{"$set": {"state": "complete"}},
)
async for config in crawl_configs.find({"lastCrawlState": "partial_complete"}):
crawl = await crawls.find_one({"_id": config.get("lastCrawlId")})
if not crawl:
continue
await crawl_configs.find_one_and_update(
{"_id": config.get("_id")},
{"$set": {"lastCrawlState": crawl.get("state")}},
)