-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pablo Panero
committed
Feb 4, 2022
1 parent
76274aa
commit f1335f5
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2022 CERN. | ||
# | ||
# Invenio-Records-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Module for event driven actions support.""" | ||
|
||
from .bus import EventBus | ||
from .events import Event | ||
from .handlers import EventHandler | ||
|
||
__all__ = ( | ||
"Event", | ||
"EventHandler", | ||
"EventBus" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2022 CERN. | ||
# | ||
# Invenio-Records-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Message bus module.""" | ||
|
||
from flask import current_app | ||
|
||
|
||
class EventBus: | ||
"""Event bus.""" | ||
|
||
def __init__(self, handlers, queue): | ||
"""Constructor.""" | ||
self._handlers = handlers | ||
self._queue = queue | ||
|
||
def publish(self, event): | ||
"""Publish an event to the bus.""" | ||
return self._queue.publish(event) | ||
|
||
def handle_events(self, uow): | ||
"""Handle a list of events.""" | ||
for event in self._queue.consume(): | ||
try: | ||
handlers = self._handlers[event] | ||
for handler in handlers: | ||
handler.handle(event, uow) | ||
except KeyError: | ||
current_app.logger.error(f"No handler for event {event}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2022 CERN. | ||
# | ||
# Invenio-Records-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Events module.""" | ||
|
||
from dataclasses import dataclass | ||
from datetime import datetime | ||
|
||
|
||
@dataclass | ||
class Event: | ||
"""Base event.""" | ||
|
||
created: datetime | ||
|
||
|
||
@dataclass | ||
class RecordEvent(Event): | ||
"""Record related events.""" | ||
|
||
recid: str | ||
# FIXME: should be an enum (created, deleted, published) | ||
# or splitted in many events | ||
action: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2022 CERN. | ||
# | ||
# Invenio-Records-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Event handlers module.""" | ||
|
||
from abc import ABC, abstractmethod | ||
|
||
|
||
class EventHandler(ABC): | ||
"""Abstract event handler class.""" | ||
|
||
@abstractmethod | ||
def handle(self, event, uow): | ||
"""Handle an event.""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2022 CERN. | ||
# | ||
# Invenio-Records-Resources is free software; you can redistribute it and/or | ||
# modify it under the terms of the MIT License; see LICENSE file for more | ||
# details. | ||
|
||
"""Event queue module.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from queue import Empty, SimpleQueue | ||
|
||
|
||
class Queue(ABC): | ||
"""Base queue.""" | ||
|
||
@abstractmethod | ||
def publish(self, event): | ||
"""Publish and event to the queue.""" | ||
|
||
@abstractmethod | ||
def consume(self): | ||
"""Consume an event from the queue.""" | ||
|
||
|
||
class MemoryQueue(Queue): | ||
"""In memory queue.""" | ||
|
||
def __init__(self): | ||
"""Constructor.""" | ||
self._events = SimpleQueue() | ||
|
||
def publish(self, event): | ||
"""Publish and event to the queue.""" | ||
return self._events.put(event) | ||
|
||
def consume(self): | ||
"""Consume an event from the queue.""" | ||
try: | ||
yield self._events.get(block=False) | ||
except Empty: | ||
yield None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters