-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Run Aleph VMs in reaction to Aleph messages
- Loading branch information
Showing
10 changed files
with
245 additions
and
12 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
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,73 @@ | ||
import asyncio | ||
import logging | ||
from typing import List, Dict, Coroutine | ||
|
||
from aleph_message.models import Message, ProgramMessage | ||
from vm_supervisor.pubsub import PubSub | ||
from vm_supervisor.run import run_code_on_event | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def is_equal_or_includes(value, compare_to) -> bool: | ||
if isinstance(value, str): | ||
return value == compare_to | ||
elif isinstance(value, dict): | ||
for subkey, subvalue in value.items(): | ||
if not hasattr(compare_to, subkey): | ||
return False | ||
if not is_equal_or_includes(subvalue, getattr(compare_to, subkey)): | ||
return False | ||
return True | ||
else: | ||
raise ValueError("Unsupported value") | ||
|
||
|
||
def subscription_matches(subscription: Dict, message: ProgramMessage) -> bool: | ||
if not subscription: | ||
# Require at least one value to match | ||
return False | ||
for key, value in subscription.items(): | ||
if not is_equal_or_includes(value, getattr(message, key)): | ||
return False | ||
return True | ||
|
||
|
||
class Reactor: | ||
|
||
pubsub: PubSub | ||
listeners: List[ProgramMessage] | ||
|
||
def __init__(self, pubsub: PubSub): | ||
self.pubsub = pubsub | ||
self.listeners = [] | ||
|
||
async def trigger(self, message: Message): | ||
coroutines: List[Coroutine] = [] | ||
|
||
for listener in self.listeners: | ||
if not listener.content.on.message: | ||
logger.warning("Program with no subscription was registered in reactor listeners: " | ||
f"{listener.item_hash}") | ||
continue | ||
|
||
for subscription in listener.content.on.message: | ||
if subscription_matches(subscription, message): | ||
vm_hash = listener.item_hash | ||
event = message.json() | ||
# Register the listener in the list of coroutines to run asynchronously: | ||
coroutines.append(run_code_on_event(vm_hash, event, self.pubsub)) | ||
break | ||
|
||
# Call all listeners asynchronously from the event loop: | ||
loop = asyncio.get_event_loop() | ||
for coroutine in coroutines: | ||
loop.create_task(coroutine) | ||
|
||
def register(self, message: ProgramMessage): | ||
if message.content.on.message: | ||
self.listeners.append(message) | ||
else: | ||
logger.debug( | ||
"Program with no subscription cannot be registered in reactor listeners: " | ||
f"{message.item_hash}") |
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
Oops, something went wrong.