2024-05-23 10:27:08 +02:00
|
|
|
import argparse
|
|
|
|
from enum import Enum
|
|
|
|
from sys import argv, exit
|
|
|
|
|
|
|
|
from app.config import DB_URI
|
2024-05-23 11:32:45 +02:00
|
|
|
from app.log import LOG
|
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-07-23 16:11:16 +02:00
|
|
|
_DEFAULT_MAX_RETRIES = 100
|
|
|
|
|
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-05-23 10:27:08 +02:00
|
|
|
source = DeadLetterEventSource()
|
|
|
|
elif mode == Mode.LISTENER:
|
2024-05-23 11:32:45 +02:00
|
|
|
LOG.i("Using PostgresEventSource")
|
2024-05-23 10:27:08 +02:00
|
|
|
source = PostgresEventSource(DB_URI)
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
def args():
|
|
|
|
parser = argparse.ArgumentParser(description="Run event listener")
|
|
|
|
parser.add_argument(
|
|
|
|
"mode",
|
|
|
|
help="Mode to run",
|
|
|
|
choices=[Mode.DEAD_LETTER.value, Mode.LISTENER.value],
|
|
|
|
)
|
2024-07-23 16:11:16 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"max_retries",
|
|
|
|
help="Max retries to consider an event as error and not try to process it again",
|
|
|
|
type=int,
|
|
|
|
required=False,
|
|
|
|
default=_DEFAULT_MAX_RETRIES,
|
|
|
|
)
|
2024-05-23 10:27:08 +02:00
|
|
|
parser.add_argument("--dry-run", help="Dry run mode", action="store_true")
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if len(argv) < 2:
|
|
|
|
print("Invalid usage. Pass 'listener' or 'dead_letter' as argument")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
args = args()
|
2024-07-23 16:11:16 +02:00
|
|
|
main(
|
|
|
|
mode=Mode.from_str(args.mode),
|
|
|
|
dry_run=args.dry_run,
|
|
|
|
max_retries=args.max_retries,
|
|
|
|
)
|