2024-05-23 10:27:08 +02:00
|
|
|
import argparse
|
|
|
|
from enum import Enum
|
|
|
|
from sys import argv, exit
|
|
|
|
|
2024-08-20 16:01:48 +02:00
|
|
|
from app.config import EVENT_LISTENER_DB_URI
|
2024-05-23 11:32:45 +02:00
|
|
|
from app.log import LOG
|
2024-08-21 12:35:08 +02:00
|
|
|
from events import event_debugger
|
2024-05-23 10:27:08 +02:00
|
|
|
from events.runner import Runner
|
|
|
|
from events.event_source import DeadLetterEventSource, PostgresEventSource
|
2024-05-23 11:32:45 +02:00
|
|
|
from events.event_sink import ConsoleEventSink, HttpEventSink
|
2024-05-23 10:27:08 +02:00
|
|
|
|
2024-09-19 16:20:56 +02:00
|
|
|
_DEFAULT_MAX_RETRIES = 10
|
2024-07-23 16:11:16 +02:00
|
|
|
|
2024-05-23 10:27:08 +02:00
|
|
|
|
|
|
|
class Mode(Enum):
|
|
|
|
DEAD_LETTER = "dead_letter"
|
|
|
|
LISTENER = "listener"
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_str(value: str):
|
|
|
|
if value == Mode.DEAD_LETTER.value:
|
|
|
|
return Mode.DEAD_LETTER
|
|
|
|
elif value == Mode.LISTENER.value:
|
|
|
|
return Mode.LISTENER
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Invalid mode: {value}")
|
|
|
|
|
|
|
|
|
2024-07-23 16:11:16 +02:00
|
|
|
def main(mode: Mode, dry_run: bool, max_retries: int):
|
2024-05-23 10:27:08 +02:00
|
|
|
if mode == Mode.DEAD_LETTER:
|
2024-05-23 11:32:45 +02:00
|
|
|
LOG.i("Using DeadLetterEventSource")
|
2024-07-25 17:03:55 +02:00
|
|
|
source = DeadLetterEventSource(max_retries)
|
2024-05-23 10:27:08 +02:00
|
|
|
elif mode == Mode.LISTENER:
|
2024-05-23 11:32:45 +02:00
|
|
|
LOG.i("Using PostgresEventSource")
|
2024-08-20 16:01:48 +02:00
|
|
|
source = PostgresEventSource(EVENT_LISTENER_DB_URI)
|
2024-05-23 10:27:08 +02:00
|
|
|
else:
|
|
|
|
raise ValueError(f"Invalid mode: {mode}")
|
|
|
|
|
|
|
|
if dry_run:
|
2024-05-23 11:32:45 +02:00
|
|
|
LOG.i("Starting with ConsoleEventSink")
|
2024-05-23 10:27:08 +02:00
|
|
|
sink = ConsoleEventSink()
|
|
|
|
else:
|
2024-05-23 11:32:45 +02:00
|
|
|
LOG.i("Starting with HttpEventSink")
|
|
|
|
sink = HttpEventSink()
|
2024-05-23 10:27:08 +02:00
|
|
|
|
|
|
|
runner = Runner(source=source, sink=sink)
|
|
|
|
runner.run()
|
|
|
|
|
|
|
|
|
2024-08-21 12:35:08 +02:00
|
|
|
def debug_event(event_id: str):
|
|
|
|
LOG.i(f"Debugging event {event_id}")
|
|
|
|
try:
|
|
|
|
event_id_int = int(event_id)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(f"Invalid event id: {event_id}")
|
|
|
|
event_debugger.debug_event(event_id_int)
|
|
|
|
|
|
|
|
|
|
|
|
def run_event(event_id: str, delete_on_success: bool):
|
|
|
|
LOG.i(f"Running event {event_id}")
|
|
|
|
try:
|
|
|
|
event_id_int = int(event_id)
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(f"Invalid event id: {event_id}")
|
|
|
|
event_debugger.run_event(event_id_int, delete_on_success)
|
|
|
|
|
|
|
|
|
2024-05-23 10:27:08 +02:00
|
|
|
def args():
|
|
|
|
parser = argparse.ArgumentParser(description="Run event listener")
|
2024-08-21 12:35:08 +02:00
|
|
|
subparsers = parser.add_subparsers(dest="command")
|
|
|
|
|
|
|
|
listener_parser = subparsers.add_parser(Mode.LISTENER.value)
|
|
|
|
listener_parser.add_argument(
|
|
|
|
"--max-retries", type=int, default=_DEFAULT_MAX_RETRIES
|
2024-05-23 10:27:08 +02:00
|
|
|
)
|
2024-08-21 12:35:08 +02:00
|
|
|
listener_parser.add_argument("--dry-run", action="store_true")
|
|
|
|
|
|
|
|
dead_letter_parser = subparsers.add_parser(Mode.DEAD_LETTER.value)
|
|
|
|
dead_letter_parser.add_argument(
|
|
|
|
"--max-retries", type=int, default=_DEFAULT_MAX_RETRIES
|
2024-07-23 16:11:16 +02:00
|
|
|
)
|
2024-08-21 12:35:08 +02:00
|
|
|
dead_letter_parser.add_argument("--dry-run", action="store_true")
|
|
|
|
|
|
|
|
debug_parser = subparsers.add_parser("debug")
|
|
|
|
debug_parser.add_argument("event_id", help="ID of the event to debug")
|
|
|
|
|
|
|
|
run_parser = subparsers.add_parser("run")
|
|
|
|
run_parser.add_argument("event_id", help="ID of the event to run")
|
|
|
|
run_parser.add_argument("--delete-on-success", action="store_true")
|
|
|
|
|
2024-05-23 10:27:08 +02:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(argv) < 2:
|
2024-08-21 12:35:08 +02:00
|
|
|
print("Invalid usage. Pass a valid subcommand as argument")
|
2024-05-23 10:27:08 +02:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
args = args()
|
2024-08-21 12:35:08 +02:00
|
|
|
|
|
|
|
if args.command in [Mode.LISTENER.value, Mode.DEAD_LETTER.value]:
|
|
|
|
main(
|
|
|
|
mode=Mode.from_str(args.command),
|
|
|
|
dry_run=args.dry_run,
|
|
|
|
max_retries=args.max_retries,
|
|
|
|
)
|
|
|
|
elif args.command == "debug":
|
|
|
|
debug_event(args.event_id)
|
|
|
|
elif args.command == "run":
|
|
|
|
run_event(args.event_id, args.delete_on_success)
|
|
|
|
else:
|
|
|
|
print("Invalid command")
|
|
|
|
exit(1)
|