core_daemon/modules/messages_example.py

49 lines
1.4 KiB
Python
Raw Permalink Normal View History

message_bus = None
def set_message_bus(bus):
global message_bus
message_bus = bus
def do_something_and_notify(args):
# Do something...
result = "Operation completed"
# Notify another module if message bus is available
if message_bus:
message_bus.send_message('notifications', f"Operation result: {result}")
return "Operation completed and notification sent"
else:
return "Operation completed (notification not sent, message bus not available)"
def do_check_notifications(args):
if not message_bus:
return "Message bus not available"
message = message_bus.receive_message('notifications', timeout=1)
if message:
return f"Received notification: {message}"
else:
return "No new notifications"
commands = {
'do_something': do_something_and_notify,
'check_notifications': do_check_notifications,
}
def get_commands():
return commands
def initialize():
global message_bus
try:
from message_bus import message_bus as mb
message_bus = mb
message_bus.create_queue('notifications')
print("Example module initialized with message bus")
except ImportError:
print("Example module initialized without message bus")
def shutdown():
if message_bus:
message_bus.delete_queue('notifications')
print("Example module shut down")