-
Notifications
You must be signed in to change notification settings - Fork 13
OSIDB-4738: Add context to async process in history. #1213
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| import json | ||
| from contextlib import contextmanager | ||
| from datetime import timedelta | ||
| from typing import Optional, Type | ||
| from typing import Any, Iterator, Optional, Type | ||
|
|
||
| import pghistory | ||
| from celery.exceptions import Ignore | ||
| from celery.utils.log import get_task_logger | ||
| from django.conf import settings | ||
|
|
@@ -15,6 +17,22 @@ | |
| logger = get_task_logger(__name__) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def pghistory_context( | ||
| action: str, | ||
| celery_task_id: str, | ||
| *, | ||
| source: str = "celery", | ||
| user: str = "celery_task", | ||
| **extra_context: Any, | ||
| ) -> Iterator[None]: | ||
| ctx = {"source": source, "user": user, "action": action, **extra_context} | ||
| if celery_task_id: | ||
| ctx["celery_task_id"] = celery_task_id | ||
| with pghistory.context(**ctx): | ||
| yield | ||
|
Comment on lines
+20
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Every call site passes Proposed fix `@contextmanager`
def pghistory_context(
action: str,
- celery_task_id: str,
+ celery_task_id: Optional[str],
*,
source: str = "celery",
user: str = "celery_task",
**extra_context: Any,
) -> Iterator[None]:🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
| class SyncManager(models.Model): | ||
| """ | ||
| Model to handle synchronization of some OSIDB data with external system like Bugzilla | ||
|
|
@@ -486,7 +504,7 @@ def __str__(self): | |
| return result | ||
|
|
||
| @staticmethod | ||
| def link_tracker_with_affects(tracker_id): | ||
| def link_tracker_with_affects(tracker_id, task="UNKNOWN"): | ||
| # Code adapted from collectors.bzimport.convertors.BugzillaTrackerConvertor.affects | ||
|
|
||
| from osidb.models import Affect, Flaw, Tracker | ||
|
|
@@ -561,9 +579,13 @@ def link_tracker_with_affects(tracker_id): | |
| affects = list(set(affects)) | ||
|
|
||
| with transaction.atomic(): | ||
| tracker.affects.clear() | ||
| tracker.affects.add(*affects) | ||
| tracker.save(raise_validation_error=False, auto_timestamps=False) | ||
| with pghistory_context( | ||
| action="link_tracker_with_affects", | ||
| celery_task_id=getattr(getattr(task, "request", None), "id", None), | ||
| ): | ||
| tracker.affects.clear() | ||
| tracker.affects.add(*affects) | ||
| tracker.save(raise_validation_error=False, auto_timestamps=False) | ||
|
|
||
| return affects, failed_flaws, failed_affects | ||
|
|
||
|
|
@@ -580,7 +602,9 @@ def sync_task(task, tracker_id, **kwargs): | |
| collector = collectors.BugzillaTrackerCollector() | ||
| try: | ||
| collector.sync_tracker(tracker_id) | ||
| result = BZTrackerDownloadManager.link_tracker_with_affects(tracker_id) | ||
| result = BZTrackerDownloadManager.link_tracker_with_affects( | ||
| tracker_id, task | ||
| ) | ||
| # Handle link failures | ||
| affects, failed_flaws, failed_affects = result | ||
| if failed_flaws: | ||
|
|
@@ -819,8 +843,12 @@ def sync_task(task, flaw_id, **kwargs): | |
| set_user_acls(settings.ALL_GROUPS) | ||
|
|
||
| try: | ||
| flaw = Flaw.objects.get(uuid=flaw_id) | ||
| flaw._create_or_update_task() | ||
| with pghistory_context( | ||
| action="jira_task_sync", | ||
| celery_task_id=getattr(getattr(task, "request", None), "id", None), | ||
| ): | ||
| flaw = Flaw.objects.get(uuid=flaw_id) | ||
| flaw._create_or_update_task() | ||
|
|
||
| except Exception as e: | ||
| JiraTaskSyncManager.failed(flaw_id, e) | ||
|
|
@@ -869,8 +897,12 @@ def sync_task(task, flaw_id, **kwargs): | |
| set_user_acls(settings.ALL_GROUPS) | ||
|
|
||
| try: | ||
| flaw = Flaw.objects.get(uuid=flaw_id) | ||
| flaw._transition_task() | ||
| with pghistory_context( | ||
| action="jira_task_transition", | ||
| celery_task_id=getattr(getattr(task, "request", None), "id", None), | ||
| ): | ||
| flaw = Flaw.objects.get(uuid=flaw_id) | ||
| flaw._transition_task() | ||
|
|
||
| except Exception as e: | ||
| JiraTaskTransitionManager.failed(flaw_id, e) | ||
|
|
@@ -990,7 +1022,11 @@ def sync_task(task, tracker_id, **kwargs): | |
| tracker_data = JiraQuerier().get_issue(tracker_id) | ||
| tracker = JiraTrackerConvertor(tracker_data).tracker | ||
| if tracker: | ||
| tracker.save() | ||
| with pghistory_context( | ||
| action="jira_tracker_download", | ||
| celery_task_id=getattr(getattr(task, "request", None), "id", None), | ||
| ): | ||
| tracker.save() | ||
|
|
||
| # Link this Jira tracker to OSIDB Affects as part of the sync process. | ||
| result = JiraTrackerDownloadManager.link_tracker_with_affects(tracker_id) | ||
|
Jincxz marked this conversation as resolved.
Comment on lines
1024
to
1032
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jira tracker path loses pghistory attribution for the affects re-link — inconsistent with the BZ flow. For the BZ flow,
Net effect: every Jira-driven tracker re-link writes history entries without Proposed fix `@staticmethod`
- def link_tracker_with_affects(tracker_id):
+ def link_tracker_with_affects(tracker_id, task=None):
# Code adapted from collectors.jiraffe.convertors.JiraTrackerConvertor.affects
...
with transaction.atomic():
- tracker.affects.clear()
- tracker.affects.add(*affects)
- tracker.save(raise_validation_error=False, auto_timestamps=False)
+ with pghistory_context(
+ action="link_tracker_with_affects",
+ celery_task_id=getattr(getattr(task, "request", None), "id", None),
+ ):
+ tracker.affects.clear()
+ tracker.affects.add(*affects)
+ tracker.save(raise_validation_error=False, auto_timestamps=False)And in - # Link this Jira tracker to OSIDB Affects as part of the sync process.
- result = JiraTrackerDownloadManager.link_tracker_with_affects(tracker_id)
+ # Link this Jira tracker to OSIDB Affects as part of the sync process.
+ result = JiraTrackerDownloadManager.link_tracker_with_affects(
+ tracker_id, task
+ )🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was left like this so only when an actual change in the tracker happens it will use the context. The linking is happening always so it will only create noise. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
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.
the user is already logged on the access logs (check for /var/log/(prod|stage|uat)-access.log on splunk)
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.
I'm getting the user so people can tell from the error why their instance is outdated without having to go to the history or the logs.
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.
I'm not sure I follow the logic of adding the user here. If the message is for the person making the change, would
_get_actual_usernot just return that person?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.
yes, is true. This was a leftover from the first change where I took the last change user from history to explain the user who or what changed the model.
@costaconrado now that the history is indexed, is it safe to take the last changed from history?
if not it true that it doesn't really make sense.