Skip to content

Commit 837252b

Browse files
authored
Merge pull request #11088 from opaduchak/feature/ENG-7756
[ENG-7310] [ENG-7756] Implemnted communication from GV to OSF and addon action logging
2 parents ce8d261 + d0f1cfb commit 837252b

File tree

3 files changed

+98
-11
lines changed

3 files changed

+98
-11
lines changed

osf/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .tasks import log_gv_addon
2+
__all__ = ['log_gv_addon']

osf/tasks.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import logging
2+
import re
3+
4+
from django.db.models import Model
5+
6+
from framework.celery_tasks import app
7+
from framework.sentry import log_message
8+
9+
logger = logging.getLogger(__name__)
10+
iri_regex = re.compile(r'http://[^/]+/(?P<id>\w{5})/?')
11+
12+
def get_object_by_url[T: Model](url: str, model: type[T]) -> T | None:
13+
if not (match := iri_regex.match(url)):
14+
log_message(f"received invalid {model.__name__} {url=}. ", skip_session=True)
15+
return None
16+
try:
17+
return model.objects.get(guids___id=match['id'])
18+
except model.DoesNotExist:
19+
log_message(f"Could not find {model.__name__} with id={match['id']}", skip_session=True)
20+
return None
21+
22+
@app.task(max_retries=5, name='osf.tasks.log_gv_addon', default_retry_delay=10)
23+
def log_gv_addon(node_url: str, action: str, user_url: str, addon: str):
24+
from osf.models import NodeLog, OSFUser, Node
25+
26+
PERMITTED_GV_ACTIONS = frozenset({
27+
NodeLog.ADDON_ADDED,
28+
NodeLog.ADDON_REMOVED
29+
})
30+
if action not in PERMITTED_GV_ACTIONS:
31+
log_message(f"{action} is not permitted to be logged from GV", skip_session=True)
32+
return
33+
34+
node = get_object_by_url(node_url, Node)
35+
user = get_object_by_url(user_url, OSFUser)
36+
if not node or not user:
37+
return
38+
39+
node.add_log(
40+
action=action,
41+
auth=user,
42+
params={
43+
'node': node._id,
44+
'project': node.parent_id,
45+
'addon': addon
46+
}
47+
)

website/settings/defaults.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ class CeleryConfig:
421421
task_high_queue = 'high'
422422
task_remote_computing_queue = 'remote'
423423
task_account_status_changes_queue = 'account_status_changes'
424+
task_external_high_queue = 'external_high'
425+
task_external_low_queue = 'external_low'
424426

425427
remote_computing_modules = {
426428
'addons.boa.tasks.submit_to_boa',
@@ -489,17 +491,53 @@ class CeleryConfig:
489491
pass
490492
else:
491493
task_queues = (
492-
Queue(task_remote_computing_queue, Exchange(task_remote_computing_queue),
493-
routing_key=task_remote_computing_queue, consumer_arguments={'x-priority': -10}),
494-
Queue(task_low_queue, Exchange(task_low_queue),
495-
routing_key=task_low_queue, consumer_arguments={'x-priority': -1}),
496-
Queue(task_default_queue, Exchange(task_default_queue),
497-
routing_key=task_default_queue, consumer_arguments={'x-priority': 0}),
498-
Queue(task_med_queue, Exchange(task_med_queue),
499-
routing_key=task_med_queue, consumer_arguments={'x-priority': 1}),
500-
Queue(task_high_queue, Exchange(task_high_queue),
501-
routing_key=task_high_queue, consumer_arguments={'x-priority': 10}),
502-
Queue(task_account_status_changes_queue, Exchange(task_account_status_changes_queue), routing_key=task_account_status_changes_queue)
494+
Queue(
495+
task_remote_computing_queue,
496+
Exchange(task_remote_computing_queue),
497+
routing_key=task_remote_computing_queue,
498+
consumer_arguments={'x-priority': -10},
499+
),
500+
Queue(
501+
task_low_queue,
502+
Exchange(task_low_queue),
503+
routing_key=task_low_queue,
504+
consumer_arguments={'x-priority': -1},
505+
),
506+
Queue(
507+
task_default_queue,
508+
Exchange(task_default_queue),
509+
routing_key=task_default_queue,
510+
consumer_arguments={'x-priority': 0},
511+
),
512+
Queue(
513+
task_med_queue,
514+
Exchange(task_med_queue),
515+
routing_key=task_med_queue,
516+
consumer_arguments={'x-priority': 1},
517+
),
518+
Queue(
519+
task_high_queue,
520+
Exchange(task_high_queue),
521+
routing_key=task_high_queue,
522+
consumer_arguments={'x-priority': 10},
523+
),
524+
Queue(
525+
task_account_status_changes_queue,
526+
Exchange(task_account_status_changes_queue),
527+
routing_key=task_account_status_changes_queue,
528+
),
529+
Queue(
530+
task_external_high_queue,
531+
Exchange(task_external_high_queue),
532+
routing_key=task_external_high_queue,
533+
consumer_arguments={'x-priority': 9},
534+
),
535+
Queue(
536+
task_external_low_queue,
537+
Exchange(task_external_low_queue),
538+
routing_key=task_external_low_queue,
539+
consumer_arguments={'x-priority': -2},
540+
),
503541
)
504542

505543
task_default_exchange_type = 'direct'

0 commit comments

Comments
 (0)