-
-
Notifications
You must be signed in to change notification settings - Fork 66
Delete background jobs on org delete + ensure org delete happens in background job #3112
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ff9116
Delete background jobs on org delete
tw4l e11434a
Add migration to delete bg jobs for deleted orgs from db
tw4l f92485d
Add UUID import
tw4l 7af9346
Modify org deletion to be safer
tw4l 079e7a7
Fix cancel subscription test
tw4l 41c2b4f
Add missing test import
tw4l 966886d
Add missing break
tw4l f2816d7
Log how many background jobs are deleted in migration
tw4l a063d60
Add comment
tw4l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ | |
| ) = object | ||
|
|
||
|
|
||
| CURR_DB_VERSION = "0056" | ||
| CURR_DB_VERSION = "0057" | ||
|
|
||
| MIN_DB_VERSION = 7.0 | ||
|
|
||
|
|
||
60 changes: 60 additions & 0 deletions
60
backend/btrixcloud/migrations/migration_0057_deleted_org_bg_job_cleanup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """ | ||
| Migration 0057 - Remove background jobs for deleted orgs from db | ||
| """ | ||
|
|
||
| from uuid import UUID | ||
|
|
||
| from motor.motor_asyncio import AsyncIOMotorDatabase | ||
|
|
||
| from btrixcloud.migrations import BaseMigration | ||
| from btrixcloud.models import BgJobType | ||
|
|
||
| MIGRATION_VERSION = "0057" | ||
|
|
||
|
|
||
| class Migration(BaseMigration): | ||
| """Migration class.""" | ||
|
|
||
| # pylint: disable=unused-argument | ||
| def __init__(self, mdb: AsyncIOMotorDatabase, **kwargs): | ||
| super().__init__(mdb, migration_version=MIGRATION_VERSION) | ||
|
|
||
| async def migrate_up(self): | ||
| """Perform migration up. | ||
|
|
||
| Delete background jobs from deleted orgs from the database. | ||
| """ | ||
| # pylint: disable=duplicate-code | ||
| jobs_mdb = self.mdb["jobs"] | ||
| orgs_mdb = self.mdb["organizations"] | ||
|
|
||
| job_orgs_to_delete: list[UUID] = [] | ||
|
|
||
| job_oids = await jobs_mdb.distinct("oid", {}) | ||
|
|
||
| for oid in job_oids: | ||
| res = await orgs_mdb.find_one({"_id": oid}) | ||
| if res is None: | ||
| job_orgs_to_delete.append(oid) | ||
|
|
||
| if job_orgs_to_delete: | ||
| del_count = len(job_orgs_to_delete) | ||
| print( | ||
| f"Deleting background jobs for {del_count} deleted orgs", | ||
| flush=True, | ||
| ) | ||
|
|
||
| try: | ||
| res = await jobs_mdb.delete_many( | ||
| { | ||
| "oid": {"$in": job_orgs_to_delete}, | ||
| # Maintain consistency with behavior moving forward, to | ||
| # retain only the one org deletion background job from | ||
| # deleted orgs | ||
| "type": {"$ne": BgJobType.DELETE_ORG}, | ||
| } | ||
| ) | ||
| print(f"Deleted {res.deleted_count} jobs from database", flush=True) | ||
| # pylint: disable=broad-exception-caught | ||
| except Exception as err: | ||
| print(f"Error deleting jobs from deleted orgs: {err}", flush=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, good catch on this!