Skip to content

Commit 126619b

Browse files
committed
fix: notifications about packages being duplicated
1 parent fe0334c commit 126619b

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/shared/channels.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,24 @@ class ContainerChannel(TriggerChannel):
4040
lock_notifications = True
4141

4242

43+
# We have two channels for CVEDerivationClusterProposal for two usages:
44+
# 1. Caching suggestions: this operation is idempotent and performance sensitive so we disable locking on this channel
45+
# 2. Notifying subscribed users of activity on their packages: this operation is not performance sensitive and we don't want duplicate notifications so we enable locking on this channel
4346
@dataclass
44-
class CVEDerivationClusterProposalChannel(TriggerChannel):
47+
class CVEDerivationClusterProposalCacheChannel(TriggerChannel):
4548
model = CVEDerivationClusterProposal
4649
# We don't need to lock notifications.
4750
# If we are caching twice the same proposal, we will just replace it.
4851
lock_notifications = False
4952

5053

54+
@dataclass
55+
class CVEDerivationClusterProposalNotificationChannel(TriggerChannel):
56+
model = CVEDerivationClusterProposal
57+
# We don't want to trigger user notifications more than once
58+
lock_notifications = True
59+
60+
5161
@dataclass
5262
class NixpkgsIssueChannel(TriggerChannel):
5363
model = NixpkgsIssue

src/shared/listeners/cache_suggestions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pgpubsub
99
from django.db.models import Prefetch
1010

11-
from shared.channels import CVEDerivationClusterProposalChannel
11+
from shared.channels import CVEDerivationClusterProposalCacheChannel
1212
from shared.models import NixDerivation, NixMaintainer
1313
from shared.models.cached import CachedSuggestions
1414
from shared.models.cve import AffectedProduct, Metric, Version
@@ -170,7 +170,7 @@ def cache_new_suggestions(suggestion: CVEDerivationClusterProposal) -> None:
170170
# CachedSuggestions.objects.filter(pk=new.pk).delete()
171171

172172

173-
@pgpubsub.post_insert_listener(CVEDerivationClusterProposalChannel)
173+
@pgpubsub.post_insert_listener(CVEDerivationClusterProposalCacheChannel)
174174
def cache_new_suggestions_following_new_container(
175175
old: CVEDerivationClusterProposal, new: CVEDerivationClusterProposal
176176
) -> None:

src/shared/listeners/notify_users.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pgpubsub
44
from django.contrib.auth.models import User
55

6-
from shared.channels import CVEDerivationClusterProposalChannel
6+
from shared.channels import CVEDerivationClusterProposalNotificationChannel
77
from shared.models.linkage import CVEDerivationClusterProposal
88
from webview.models import Notification
99

@@ -61,7 +61,7 @@ def create_package_subscription_notifications(
6161
logger.error(f"Failed to create notification for user {user.username}: {e}")
6262

6363

64-
@pgpubsub.post_insert_listener(CVEDerivationClusterProposalChannel)
64+
@pgpubsub.post_insert_listener(CVEDerivationClusterProposalNotificationChannel)
6565
def notify_subscribed_users_following_suggestion_insert(
6666
old: CVEDerivationClusterProposal, new: CVEDerivationClusterProposal
6767
) -> None:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 4.2.24 on 2025-10-31 13:17
2+
3+
from django.db import migrations
4+
import pgtrigger.compiler
5+
import pgtrigger.migrations
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('shared', '0057_nixderivation_shared_nixd_attribu_5fcca7_btree'),
12+
]
13+
14+
operations = [
15+
pgtrigger.migrations.RemoveTrigger(
16+
model_name='cvederivationclusterproposal',
17+
name='pgpubsub_8c7ef',
18+
),
19+
pgtrigger.migrations.AddTrigger(
20+
model_name='cvederivationclusterproposal',
21+
trigger=pgtrigger.compiler.Trigger(name='pgpubsub_6aede', sql=pgtrigger.compiler.UpsertTriggerSql(declare='DECLARE payload JSONB; notification_context_text TEXT;', func='\n \n payload := \'{"app": "shared", "model": "CVEDerivationClusterProposal"}\'::jsonb;\n payload := jsonb_insert(payload, \'{old}\', COALESCE(to_jsonb(OLD), \'null\'));\n payload := jsonb_insert(payload, \'{new}\', COALESCE(to_jsonb(NEW), \'null\'));\n SELECT current_setting(\'pgpubsub.notification_context\', True) INTO notification_context_text;\n IF COALESCE(notification_context_text, \'\') = \'\' THEN\n notification_context_text := \'{}\';\n END IF;\n payload := jsonb_insert(payload, \'{context}\', notification_context_text::jsonb);\n \n \n perform pg_notify(\'pgpubsub_6aede\', payload::text);\n RETURN NEW;\n ', hash='3fc8ef40725c909c32771363fbb4ae378e50302c', operation='INSERT', pgid='pgtrigger_pgpubsub_6aede_fc173', table='shared_cvederivationclusterproposal', when='AFTER')),
22+
),
23+
pgtrigger.migrations.AddTrigger(
24+
model_name='cvederivationclusterproposal',
25+
trigger=pgtrigger.compiler.Trigger(name='pgpubsub_07e32', sql=pgtrigger.compiler.UpsertTriggerSql(declare='DECLARE payload JSONB; notification_context_text TEXT;', func='\n \n payload := \'{"app": "shared", "model": "CVEDerivationClusterProposal"}\'::jsonb;\n payload := jsonb_insert(payload, \'{old}\', COALESCE(to_jsonb(OLD), \'null\'));\n payload := jsonb_insert(payload, \'{new}\', COALESCE(to_jsonb(NEW), \'null\'));\n SELECT current_setting(\'pgpubsub.notification_context\', True) INTO notification_context_text;\n IF COALESCE(notification_context_text, \'\') = \'\' THEN\n notification_context_text := \'{}\';\n END IF;\n payload := jsonb_insert(payload, \'{context}\', notification_context_text::jsonb);\n \n \n INSERT INTO pgpubsub_notification (channel, payload)\n VALUES (\'pgpubsub_07e32\', payload);\n \n perform pg_notify(\'pgpubsub_07e32\', payload::text);\n RETURN NEW;\n ', hash='eef760200ecd9145771d2d5b5c1d9bc1f96fb2ac', operation='INSERT', pgid='pgtrigger_pgpubsub_07e32_acce7', table='shared_cvederivationclusterproposal', when='AFTER')),
26+
),
27+
]

0 commit comments

Comments
 (0)