-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_redis.py
62 lines (46 loc) · 1.77 KB
/
demo_redis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import redis
from event_source import RedisStreamsSource
import asyncio
from notification_sink import RGBSink
import argparse
import signal
import os
# Example consumer function
async def example_consumer(event: dict):
print(f"Received event from {event}")
async def main(rules):
# Create a Redis client
#redis_client = redis.from_url("redis://localhost", decode_responses=True)
stream_source = RedisStreamsSource()
rgb_sink = RGBSink()
# Register the example consumer
stream_source.register_consumer(rgb_sink.consume)
stream_source.register_consumer(example_consumer)
await stream_source.connection()
await asyncio.gather(
stream_source.start(),
rgb_sink.start(rules)
)
# Wait indefinitely until a SIGTERM signal is received
stop_event = asyncio.Event()
loop = asyncio.get_running_loop()
def handle_sigterm():
print("SIGTERM received, shutting down...")
stream_source.stop()
stop_event.set()
loop.add_signal_handler(signal.SIGTERM, handle_sigterm)
await stop_event.wait()
# Close the Redis client connection
await redis.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Script that displays events in matrix")
parser.add_argument("--profile", required=True, help="Yaml Rule file")
#parser.add_argument("--events", required=True, help="File with json events")
#parser.add_argument("--host", required=True, help="Redis host")
#parser.add_argument("--port", required=True, help="Redis port")
#parser.add_argument("--password", required=True, help="Redis password")
args = parser.parse_args()
profile_yaml = args.profile
#events = args.events
# Run the main function
asyncio.run(main(profile_yaml))