Skip to content

Commit

Permalink
fix(Site): Serialize enable database access calls
Browse files Browse the repository at this point in the history
- Since job is fast, we need to check the `is_database_access_enabled`
  field's status in addition to pending and running jobs.
- This will only work if job callback also puts a lock on site doc
  before updating the field
  • Loading branch information
balamurali27 committed Sep 2, 2024
1 parent 854eb5c commit fcc0672
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 2 additions & 0 deletions press/press/doctype/agent_job/agent_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ def get_pair_jobs() -> tuple[str]:
"Remove Site from Upstream",
"Rename Site",
"Rename Site on Upstream",
"Add User to ProxySQL",
"Remove User from ProxySQL",
)


Expand Down
15 changes: 12 additions & 3 deletions press/press/doctype/site/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -2160,9 +2160,7 @@ def _create_initial_site_plan_change(self, plan):
}
).insert(ignore_permissions=True)

@dashboard_whitelist()
@site_action(["Active"])
def enable_database_access(self, mode="read_only"):
def check_db_access_enabling(self):
if frappe.db.get_value(
"Agent Job",
filters={
Expand All @@ -2176,8 +2174,19 @@ def enable_database_access(self, mode="read_only"):
"Database Access is already being enabled on this site. Please check after a while."
)

def check_db_access_enabled_already(self):
if frappe.db.get_value(
self.doctype, self.name, "is_database_access_enabled", for_update=True
):
frappe.throw("Database Access already enabled. Reload the page and try.")

@dashboard_whitelist()
@site_action(["Active"])
def enable_database_access(self, mode="read_only"):
if not frappe.db.get_value("Site Plan", self.plan, "database_access"):
frappe.throw(f"Database Access is not available on {self.plan} plan")
self.check_db_access_enabling()
self.check_db_access_enabled_already()
log_site_activity(self.name, "Enable Database Access")

server_agent = Agent(self.server)
Expand Down

0 comments on commit fcc0672

Please sign in to comment.