-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from walkIT-nl/131-implementEventPublication
#131 Add event publication for NATS, RabbitMQ and Kafka; asyncio support and function composition
- Loading branch information
Showing
27 changed files
with
681 additions
and
56 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
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
FROM osimis/orthanc:22.9.0-full | ||
|
||
RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y openssl | ||
RUN apt-get update && ACCEPT_EULA=Y apt-get dist-upgrade -y && apt-get install -y openssl | ||
|
||
COPY server_cert.cnf . | ||
RUN openssl req -nodes -new -x509 -days 3650 -keyout /etc/ssl/private/server.key -out /etc/ssl/certs/server.pem -config server_cert.cnf | ||
RUN mkdir -p /ssl && cat /etc/ssl/private/server.key /etc/ssl/certs/server.pem > /ssl/keyAndCert.pem | ||
|
||
RUN pip3 install httpx # does not get picked up in setup.py | ||
COPY orthanc_ext /python/orthanc_ext | ||
WORKDIR /python | ||
COPY setup.py README.rst HISTORY.rst ./ | ||
RUN pip3 install httpx .[nats-event-publisher] # does not get picked up in setup.py | ||
RUN python3 setup.py install | ||
COPY tests/entry_point.py /python/entry_point.py |
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
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
__author__ = """WalkIT""" | ||
__email__ = 'code@walkit.nl' | ||
__version__ = '3.2.12' | ||
__version__ = '3.3.0' |
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
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
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 @@ | ||
import dataclasses | ||
|
||
from cloudevents.conversion import to_structured, from_http | ||
from cloudevents.http import CloudEvent | ||
|
||
|
||
def create_valid_orthanc_cloud_event(evt): | ||
return CloudEvent.create({ | ||
'type': 'orthanc-server-extensions.change-event', | ||
'source': 'https://orthanc-server-identifer' | ||
}, | ||
data=dataclasses.asdict(evt)) | ||
|
||
|
||
def convert_change_event_to_message(evt) -> tuple: | ||
return to_structured(create_valid_orthanc_cloud_event(evt)) | ||
|
||
|
||
def convert_message_to_change_event(headers: dict, data: bytes): | ||
return from_http(CloudEvent, headers, data=data) |
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,31 @@ | ||
from dataclasses import dataclass | ||
|
||
from aiokafka import AIOKafkaProducer | ||
from kafka.admin import KafkaAdminClient, NewTopic | ||
|
||
from orthanc_ext.scripts.event_publisher import convert_change_event_to_message | ||
|
||
|
||
@dataclass | ||
class KafkaConfig: | ||
bootstrap_server: str | ||
topic: str = 'orthanc-events' | ||
|
||
|
||
async def publish_to_kafka(kafka_config: KafkaConfig, evt, _): | ||
producer = AIOKafkaProducer( | ||
security_protocol='PLAINTEXT', bootstrap_servers=kafka_config.bootstrap_server) | ||
await producer.start() | ||
try: | ||
_, event = convert_change_event_to_message(evt) | ||
await producer.send_and_wait(kafka_config.topic, event) | ||
|
||
finally: | ||
await producer.stop() | ||
|
||
|
||
def create_stream(kafka_config: KafkaConfig, *_): | ||
admin_client = KafkaAdminClient(bootstrap_servers=kafka_config.bootstrap_server) | ||
admin_client.create_topics( | ||
new_topics=[NewTopic(name=kafka_config.topic, num_partitions=1, replication_factor=1)], | ||
validate_only=False) |
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,31 @@ | ||
from dataclasses import dataclass | ||
|
||
import nats | ||
|
||
from orthanc_ext.scripts.event_publisher import convert_change_event_to_message | ||
|
||
|
||
@dataclass | ||
class NatsConfig: | ||
url: str | ||
stream_name: str = 'orthanc-events' | ||
subject = 'onchange' | ||
|
||
|
||
async def create_stream(nats_config: NatsConfig, *_): | ||
nc = await nats.connect(nats_config.url) | ||
try: | ||
js = nc.jetstream() | ||
await js.add_stream(name=nats_config.stream_name, subjects=[nats_config.subject]) | ||
finally: | ||
await nc.close() | ||
|
||
|
||
async def publish_to_nats(nats_config: NatsConfig, evt, *_): | ||
nc = await nats.connect(nats_config.url) | ||
try: | ||
js = nc.jetstream() | ||
_, message = convert_change_event_to_message(evt) | ||
return await js.publish(nats_config.subject, message, stream=nats_config.stream_name) | ||
finally: | ||
await nc.close() |
Oops, something went wrong.