Skip to content
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

Add logs to signals #649

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions database/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging

from google.cloud import pubsub_v1
from shared.config import get_config
Expand All @@ -8,6 +9,8 @@

_pubsub_publisher = None

log = logging.getLogger(__name__)


def _get_pubsub_publisher():
global _pubsub_publisher
Expand All @@ -17,22 +20,29 @@


def _sync_repo(repository: Repository):
pubsub_project_id = get_config("setup", "shelter", "pubsub_project_id")
pubsub_topic_id = get_config("setup", "shelter", "sync_repo_topic_id")

if pubsub_project_id and pubsub_topic_id:
publisher = _get_pubsub_publisher()
topic_path = publisher.topic_path(pubsub_project_id, pubsub_topic_id)
publisher.publish(
topic_path, json.dumps({"sync": repository.repoid}).encode("utf-8")
)
log.info(f"Signal triggered for repository {repository.repoid}")
try:
pubsub_project_id = get_config("setup", "shelter", "pubsub_project_id")
pubsub_topic_id = get_config("setup", "shelter", "sync_repo_topic_id")

if pubsub_project_id and pubsub_topic_id:
publisher = _get_pubsub_publisher()
topic_path = publisher.topic_path(pubsub_project_id, pubsub_topic_id)
publisher.publish(
topic_path, json.dumps({"sync": repository.repoid}).encode("utf-8")
)
log.info(f"Message published for repository {repository.repoid}")
except Exception as e:
log.warning(f"Failed to publish message for repo {repository.repoid}: {e}")

Check warning on line 36 in database/events.py

View check run for this annotation

Codecov Notifications / codecov/patch

database/events.py#L35-L36

Added lines #L35 - L36 were not covered by tests


@event.listens_for(Repository, "after_insert")
def after_insert_repo(mapper, connection, target):
log.info("After insert signal")
_sync_repo(target)


@event.listens_for(Repository, "after_update")
def after_update_repo(mapper, connection, target):
log.info("After update signal")
_sync_repo(target)
Loading