|
1 | 1 | import json |
2 | 2 | import logging |
| 3 | +from typing import Any, Dict, Optional, Type |
3 | 4 |
|
4 | 5 | from django.conf import settings |
5 | 6 | from django.db.models.signals import post_save |
|
13 | 14 |
|
14 | 15 | @receiver(post_save, sender=Owner) |
15 | 16 | def create_owner_profile_when_owner_is_created( |
16 | | - sender, instance: Owner, created, **kwargs |
17 | | -): |
| 17 | + sender: Type[Owner], instance: Owner, created: bool, **kwargs: Dict[str, Any] |
| 18 | +) -> Optional[OwnerProfile]: |
18 | 19 | if created: |
19 | 20 | return OwnerProfile.objects.create(owner_id=instance.ownerid) |
20 | 21 |
|
21 | 22 |
|
22 | | -_pubsub_publisher = None |
| 23 | +class ShelterPubsub: |
| 24 | + pubsub_publisher = None |
| 25 | + _instance = None |
23 | 26 |
|
| 27 | + @classmethod |
| 28 | + def get_instance(cls) -> "ShelterPubsub": |
| 29 | + """ |
| 30 | + This class needs the Django settings to be fully loaded before it can be instantiated, |
| 31 | + therefore use this method to get an instance rather than instantiating directly. |
| 32 | + """ |
| 33 | + if cls._instance is None: |
| 34 | + cls._instance = cls() |
| 35 | + return cls._instance |
24 | 36 |
|
25 | | -def _get_pubsub_publisher(): |
26 | | - global _pubsub_publisher |
27 | | - if not _pubsub_publisher: |
28 | | - _pubsub_publisher = pubsub_v1.PublisherClient() |
29 | | - return _pubsub_publisher |
| 37 | + def __init__(self) -> None: |
| 38 | + if not self.pubsub_publisher: |
| 39 | + self.pubsub_publisher = pubsub_v1.PublisherClient() |
| 40 | + pubsub_project_id: str = settings.SHELTER_PUBSUB_PROJECT_ID |
| 41 | + |
| 42 | + # topic_id has REPO in the name but it is used for all types of objects |
| 43 | + topic_id: str = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID |
| 44 | + self.topic_path = self.pubsub_publisher.topic_path(pubsub_project_id, topic_id) |
| 45 | + |
| 46 | + def publish(self, data: Dict[str, Any]) -> None: |
| 47 | + try: |
| 48 | + self.pubsub_publisher.publish( |
| 49 | + self.topic_path, |
| 50 | + json.dumps(data).encode("utf-8"), |
| 51 | + ) |
| 52 | + except Exception as e: |
| 53 | + log.warning( |
| 54 | + "Failed to publish a message", |
| 55 | + extra=dict(data_to_publish=data, error=e), |
| 56 | + ) |
30 | 57 |
|
31 | 58 |
|
32 | 59 | @receiver( |
33 | 60 | post_save, sender=OrganizationLevelToken, dispatch_uid="shelter_sync_org_token" |
34 | 61 | ) |
35 | | -def update_org_token(sender, instance: OrganizationLevelToken, **kwargs): |
36 | | - pubsub_project_id = settings.SHELTER_PUBSUB_PROJECT_ID |
37 | | - topic_id = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID |
38 | | - if pubsub_project_id and topic_id: |
39 | | - publisher = _get_pubsub_publisher() |
40 | | - topic_path = publisher.topic_path(pubsub_project_id, topic_id) |
41 | | - publisher.publish( |
42 | | - topic_path, |
43 | | - json.dumps( |
44 | | - { |
45 | | - "type": "org_token", |
46 | | - "sync": "one", |
47 | | - "id": instance.id, |
48 | | - } |
49 | | - ).encode("utf-8"), |
50 | | - ) |
| 62 | +def update_org_token( |
| 63 | + sender: Type[OrganizationLevelToken], |
| 64 | + instance: OrganizationLevelToken, |
| 65 | + **kwargs: Dict[str, Any], |
| 66 | +) -> None: |
| 67 | + data = { |
| 68 | + "type": "org_token", |
| 69 | + "sync": "one", |
| 70 | + "id": instance.id, |
| 71 | + } |
| 72 | + ShelterPubsub.get_instance().publish(data) |
51 | 73 |
|
52 | 74 |
|
53 | 75 | @receiver(post_save, sender=Owner, dispatch_uid="shelter_sync_owner") |
54 | | -def update_owner(sender, instance: Owner, **kwargs): |
| 76 | +def update_owner( |
| 77 | + sender: Type[Owner], instance: Owner, **kwargs: Dict[str, Any] |
| 78 | +) -> None: |
55 | 79 | """ |
56 | 80 | Shelter tracks a limited set of Owner fields - only update if those fields have changed. |
57 | 81 | """ |
58 | | - created = kwargs["created"] |
| 82 | + created: bool = kwargs["created"] |
59 | 83 | tracked_fields = [ |
60 | 84 | "upload_token_required_for_public_repos", |
61 | 85 | "username", |
62 | 86 | "service", |
63 | 87 | ] |
64 | 88 | if created or any(instance.tracker.has_changed(field) for field in tracked_fields): |
65 | | - pubsub_project_id = settings.SHELTER_PUBSUB_PROJECT_ID |
66 | | - topic_id = settings.SHELTER_PUBSUB_SYNC_REPO_TOPIC_ID |
67 | | - if pubsub_project_id and topic_id: |
68 | | - try: |
69 | | - publisher = _get_pubsub_publisher() |
70 | | - topic_path = publisher.topic_path(pubsub_project_id, topic_id) |
71 | | - publisher.publish( |
72 | | - topic_path, |
73 | | - json.dumps( |
74 | | - { |
75 | | - "type": "owner", |
76 | | - "sync": "one", |
77 | | - "id": instance.ownerid, |
78 | | - } |
79 | | - ).encode("utf-8"), |
80 | | - ) |
81 | | - except Exception as e: |
82 | | - log.warning( |
83 | | - "Failed to publish message for owner", |
84 | | - extra=dict(owner_id=instance.pk, error=e), |
85 | | - ) |
| 89 | + data = { |
| 90 | + "type": "owner", |
| 91 | + "sync": "one", |
| 92 | + "id": instance.ownerid, |
| 93 | + } |
| 94 | + ShelterPubsub.get_instance().publish(data) |
0 commit comments