Skip to content

Commit

Permalink
feat: add replay CLI command
Browse files Browse the repository at this point in the history
Signed-off-by: freedisch <freeproduc@gmail.com>
  • Loading branch information
Freedisch authored and abompard committed Mar 20, 2024
1 parent 0917bf8 commit 5a621dd
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 310 deletions.
34 changes: 33 additions & 1 deletion fedora_messaging/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
.. _Click: http://click.pocoo.org/
"""


import errno
import importlib
import logging
Expand All @@ -30,8 +29,11 @@

import click
import pkg_resources
import requests
from twisted.internet import asyncioreactor, error

from fedora_messaging import message


try:
asyncioreactor.install()
Expand Down Expand Up @@ -91,6 +93,10 @@
"The name of the exchange to publish to. Can contain ASCII letters, "
"digits, hyphen, underscore, period, or colon."
)
_datagrepper_help = (
"The URL of the datagreeper instance to use, "
"defaults to the production environment"
)
_limit_help = "The maximum number of messages to record."


Expand Down Expand Up @@ -452,3 +458,29 @@ def record(exchange, queue_name, routing_key, app_name, limit, file):
_consume(
exchange, queue_name, routing_key, messages_recorder.collect_message, app_name
)


URL_TEMPLATE = "https://apps.fedoraproject.org/datagrepper/id?id={}&is_raw=true"


@cli.command()
@click.argument("message_id")
@click.option("--datagrepper-url", help=_datagrepper_help, default=URL_TEMPLATE)
def replay(message_id, datagrepper_url):
"""Replay a message from Datagrepper by its message ID"""
try:
message_data = _get_message(message_id, datagrepper_url)
except requests.HTTPError as e:
raise click.ClickException(f"Failed to retrieve message from Datagrepper: {e}")

if message_data:
api.publish(message.load_message(message_data))
click.echo(f"Message with ID {message_id} has been successfully replayed.")


def _get_message(message_id, datagrepper_url):
"""Fetch a message by ID from Datagreeper"""
url = datagrepper_url.format(message_id)
response = requests.get(url, timeout=5)
response.raise_for_status()
return response.json()
1 change: 1 addition & 0 deletions news/332.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a replay command
Loading

0 comments on commit 5a621dd

Please sign in to comment.