oncall-engine/engine/common/database.py

39 lines
1.7 KiB
Python
Raw Normal View History

import random
from django.conf import settings
from django.db import models
def get_random_readonly_database_key_if_present_otherwise_default() -> str:
"""
modify check_escalation_finished_task task (#1266) # What this PR does This PR: - modifies the `check_escalation_finished_task` celery task to: - do stricter escalation validation based on the alert group's escalation snapshot (see the `audit_alert_group_escalation` method in `engine/apps/alerts/tasks/check_escalation_finished.py` for the validation logic) - use a read-only database for querying alert-groups if one is configured, otherwise use the "default" one - ping a configurable heartbeat (new env var `ALERT_GROUP_ESCALATION_AUDITOR_CELERY_TASK_HEARTBEAT_URL` added) - increase the task frequency from every 10 to every 13 minutes (this can be configured via an env variable) - adds public documentation on how to configure this auditor task - modifies the local celery startup command to properly take into consideration all celery related env vars (similar to the ones we use in `engine/celery_with_exporter.sh`; this made it easier to enable `celery beat` locally for testing) - removes the following code: - removes references to `AlertGroup.estimate_escalation_finish_time` and marks the model field as deprecated using the [`django-deprecate-fields` library](https://pypi.org/project/django-deprecate-fields/). This field was only used for the previous version of this validation task - `EscalationSnapshotMixin.calculate_eta_for_finish_escalation` was only used to calculate the value for `AlertGroup.estimate_escalation_finish_time` - `calculate_escalation_finish_time` celery task ## Which issue(s) this PR fixes https://github.com/grafana/oncall-private/issues/1558 ## Checklist - [x] Tests updated - [x] Documentation added - [x] `CHANGELOG.md` updated
2023-03-17 11:14:08 +01:00
This function returns a string, representing a key in the `DATABASES` django settings.
If `settings.READONLY_DATABASES` is set, and non-empty, it randomly chooses one of the read-only databases,
otherwise it falls back to "default".
modify check_escalation_finished_task task (#1266) # What this PR does This PR: - modifies the `check_escalation_finished_task` celery task to: - do stricter escalation validation based on the alert group's escalation snapshot (see the `audit_alert_group_escalation` method in `engine/apps/alerts/tasks/check_escalation_finished.py` for the validation logic) - use a read-only database for querying alert-groups if one is configured, otherwise use the "default" one - ping a configurable heartbeat (new env var `ALERT_GROUP_ESCALATION_AUDITOR_CELERY_TASK_HEARTBEAT_URL` added) - increase the task frequency from every 10 to every 13 minutes (this can be configured via an env variable) - adds public documentation on how to configure this auditor task - modifies the local celery startup command to properly take into consideration all celery related env vars (similar to the ones we use in `engine/celery_with_exporter.sh`; this made it easier to enable `celery beat` locally for testing) - removes the following code: - removes references to `AlertGroup.estimate_escalation_finish_time` and marks the model field as deprecated using the [`django-deprecate-fields` library](https://pypi.org/project/django-deprecate-fields/). This field was only used for the previous version of this validation task - `EscalationSnapshotMixin.calculate_eta_for_finish_escalation` was only used to calculate the value for `AlertGroup.estimate_escalation_finish_time` - `calculate_escalation_finish_time` celery task ## Which issue(s) this PR fixes https://github.com/grafana/oncall-private/issues/1558 ## Checklist - [x] Tests updated - [x] Documentation added - [x] `CHANGELOG.md` updated
2023-03-17 11:14:08 +01:00
This is primarily intended to be used for django's `QuerySet.using()` function
"""
using_db = "default"
if hasattr(settings, "READONLY_DATABASES") and len(settings.READONLY_DATABASES) > 0:
using_db = random.choice(list(settings.READONLY_DATABASES.keys()))
return using_db
def NON_POLYMORPHIC_SET_NULL(collector, field, sub_objs, using):
"""
django-polymorphic has a bug where it doesn't properly handle the `on_delete` argument:
https://github.com/django-polymorphic/django-polymorphic/issues/229#issuecomment-398434412.
This is a workaround that uses the same code as the original `SET_NULL` function, but with the
`non_polymorphic()` function applied to the `sub_objs` queryset.
"""
return models.SET_NULL(collector, field, sub_objs.non_polymorphic(), using)
def NON_POLYMORPHIC_CASCADE(collector, field, sub_objs, using):
"""
django-polymorphic has a bug where it doesn't properly handle the `on_delete` argument:
https://github.com/django-polymorphic/django-polymorphic/issues/229#issuecomment-398434412.
This is a workaround that uses the same code as the original `CASCADE` function, but with the
`non_polymorphic()` function applied to the `sub_objs` queryset.
"""
return models.CASCADE(collector, field, sub_objs.non_polymorphic(), using)