Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions ipsframework/_internal/bridges/portal_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from ipsframework import Component

MAX_RETRIES = 10
MAX_RETRIES = 3


_portal_logger = logging.getLogger('ipsframework.bridges.portal_bridge')
Expand All @@ -36,7 +36,9 @@ def send_post(conn: Connection, stop: EventType, url: str):
fail_count = 0

http = PoolManager(
retries=Urllib3Retry(total=MAX_RETRIES, backoff_factor=1, respect_retry_after_header=True),
retries=Urllib3Retry(
total=MAX_RETRIES, backoff_factor=0.25, respect_retry_after_header=True
),
headers={'Content-Type': 'application/json'},
)

Expand Down Expand Up @@ -68,7 +70,9 @@ def send_jupyter_notebook(conn: Connection, stop: EventType, url: str, api_key:
fail_count = 0

http = PoolManager(
retries=Urllib3Retry(total=MAX_RETRIES, backoff_factor=1, respect_retry_after_header=True)
retries=Urllib3Retry(
total=MAX_RETRIES, backoff_factor=0.25, respect_retry_after_header=True
)
)

while True:
Expand Down Expand Up @@ -129,7 +133,9 @@ def send_jupyter_notebook_data(
fail_count = 0

http = PoolManager(
retries=Urllib3Retry(total=MAX_RETRIES, backoff_factor=1, respect_retry_after_header=True)
retries=Urllib3Retry(
total=MAX_RETRIES, backoff_factor=0.25, respect_retry_after_header=True
)
)

while True:
Expand Down Expand Up @@ -195,7 +201,9 @@ def send_ensemble_variables(
fail_count = 0

http = PoolManager(
retries=Urllib3Retry(total=MAX_RETRIES, backoff_factor=1, respect_retry_after_header=True)
retries=Urllib3Retry(
total=MAX_RETRIES, backoff_factor=0.25, respect_retry_after_header=True
)
)

while True:
Expand Down
2 changes: 1 addition & 1 deletion ipsframework/component_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def add_entry(
print(
'Error creating component registry entry for ', key, ' : ', str(e), file=sys.stderr
)
raise e
raise

def remove_entry(self, component_id):
key = component_id.get_serialization()
Expand Down
48 changes: 14 additions & 34 deletions ipsframework/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,22 @@
variable 'IPSES_DEBUG' is defined.
"""

import logging
import os

_logger = logging.getLogger(__name__)

class Debug: # pragma: no cover
def __init__(self):
self.file = None
if 'IPSES_DEBUG' in os.environ:
self.file = open('debug.out', 'w')
if 'IPSES_DEBUG' in os.environ:
_logger.setLevel(logging.DEBUG)
_logger.addHandler(logging.FileHandler('debug.out', mode='w'))
else:
_logger.setLevel(logging.WARNING)

def output(self, s: str, id1=0, id2=0):
if self.file:
tmp = ''
if id1 != 0:
""" one subscriber/listener """
if id2 == 0:
tmp += ', id = ' + str(id1)
else:
tmp += ', listenerid = ' + str(id1) + ', subscriberid = ' + str(id2)

self.file.write(s + tmp + '\n')

def msg(self, s1: str, ret=99, s2=''):
if self.file:
if s2 == '':
if ret != 99:
self.file.write(s1 + ' ' + str(ret) + '\n')
else:
self.file.write(s1 + '\n')
elif ret != 99:
self.file.write(s1 + ' ' + str(ret) + ' ' + s2 + '\n')
else:
self.file.write(s1 + ' ' + s2 + '\n')

def __del__(self):
if self.file:
self.file.close()


debug = Debug()
def output(s: str, id1=0, id2=0):
if id1 != 0:
if id2 == 0:
s += ', id = ' + str(id1)
else:
s += ', listenerid = ' + str(id1) + ', subscriberid = ' + str(id2)
_logger.debug(s)
58 changes: 29 additions & 29 deletions ipsframework/event_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from .cca_es_spec import Event, EventServiceError, Topic
from .debug import debug
from .debug import output as debug_output
from .topic_manager import TopicManager


Expand Down Expand Up @@ -74,27 +74,27 @@ def process_service_request(self, msg):
method = getattr(self, msg.target_method)
return method(*msg.args)

"""""" """PublisherEventService methods start here""" """"""
"""PublisherEventService methods start here"""

def get_topic(self, topic_name):
"""Add an entry to the topicDirectory for a new topic."""
if topic_name not in self.topicDirectory:
debug.output('get_topic %s' % topic_name)
debug_output('get_topic %s' % topic_name)
self.topicDirectory[topic_name] = TopicManager()
return Topic(topic_name)

def exists_topic(self, topic_name):
return topic_name in self.topicDirectory

"""""" """PublisherEventService methods end here""" """"""
"""PublisherEventService methods end here"""

"""""" """SubscriberEventService methods start here""" """"""
"""SubscriberEventService methods start here"""

def register_subscriber(self):
self.numSubscribers += 1
subscriberid = self.numSubscribers
self.subscriberDirectory[subscriberid] = {}
debug.output('Subscriber registered', subscriberid)
debug_output('Subscriber registered', subscriberid)
return subscriberid

"""
Expand All @@ -108,7 +108,7 @@ def register_subscriber(self):
def unregister_subscriber(self, subscriberid):
listener_list = []
if subscriberid in self.subscriberDirectory:
debug.output('\n\n------Subscriber is unregistering', subscriberid)
debug_output('\n\n------Subscriber is unregistering', subscriberid)

"""
Step through all the listeners for the subscriber in turn,
Expand All @@ -120,7 +120,7 @@ def unregister_subscriber(self, subscriberid):
listenerid = self.subscriberDirectory[subscriberid][subscription_name][
listener_key
]
debug.output(
debug_output(
'Unregistering listener on listener_key %s, subscription %s'
% (listener_key, subscription_name),
listenerid,
Expand All @@ -131,7 +131,7 @@ def unregister_subscriber(self, subscriberid):
)
for topic_name in topic_list:
self.topicDirectory[topic_name].unregister_listener(listenerid)
debug.output(
debug_output(
'Listener on listener_key %s, subscription %s unregistered'
% (listener_key, subscription_name),
listenerid,
Expand All @@ -140,7 +140,7 @@ def unregister_subscriber(self, subscriberid):
listener_list.append(listenerid)
""" Remove the subscriber entry in subscriberDirectory. """
del self.subscriberDirectory[subscriberid]
debug.output('Subscriber unregistered', subscriberid)
debug_output('Subscriber unregistered', subscriberid)
else:
raise EventServiceError('Subscriber not recognized.')
return listener_list
Expand All @@ -157,7 +157,7 @@ def get_subscription(self, subscriberid, subscription_name):

if subscription_name not in self.subscriberDirectory[subscriberid]:
self.subscriberDirectory[subscriberid][subscription_name] = {}
debug.output('Subscriber subscribed to %s' % subscription_name, subscriberid)
debug_output('Subscriber subscribed to %s' % subscription_name, subscriberid)

"""
A Subscription object cannot be safely returned without screwing
Expand Down Expand Up @@ -205,9 +205,9 @@ def process_events(self, subscriberid):
raise EventServiceError('Subscriber not recognized.')
return event_list

"""""" """SubscriberEventService methods end here""" """"""
"""SubscriberEventService methods end here"""

"""""" """Topic methods start here""" """"""
"""Topic methods start here"""

"""
send_event adds an event to the topic's TopicManager object.
Expand All @@ -218,24 +218,24 @@ def send_event(self, topic_name, event_name, event_body):
event_header = {}
event_header[event_name] = event_name
the_event = Event(event_header, event_body)
debug.output('Event %s sent to topic %s' % (the_event, topic_name))
debug_output('Event %s sent to topic %s' % (the_event, topic_name))
self.topicDirectory[topic_name].send_event(the_event)
else:
raise EventServiceError('Topic not recognized.')

"""""" """Topic methods end here""" """"""
"""Topic methods end here"""

"""""" """EventListener methods start here""" """"""
"""EventListener methods start here"""

def create_listener(self):
self.numListeners += 1
listenerid = self.numListeners
debug.output('Listener created', listenerid)
debug_output('Listener created', listenerid)
return listenerid

"""""" """EventListener methods end here""" """"""
"""EventListener methods end here"""

"""""" """Subscription methods start here""" """"""
"""Subscription methods start here"""

"""
register_event_listener adds a listener to its subscriber's subscriberDirectory
Expand All @@ -255,7 +255,7 @@ def register_event_listener(self, subscriberid, subscription_name, listener_key,
listener_key
not in self.subscriberDirectory[subscriberid][subscription_name]
):
debug.output(
debug_output(
'Registering listener on listener_key %s, subscription %s'
% (listener_key, subscription_name),
listenerid,
Expand Down Expand Up @@ -296,7 +296,7 @@ def unregister_event_listener(self, subscriberid, subscription_name, listener_ke
listenerid = self.subscriberDirectory[subscriberid][subscription_name][
listener_key
]
debug.output(
debug_output(
'Unregistering listener on listener_key %s, subscription %s'
% (listener_key, subscription_name),
listenerid,
Expand All @@ -308,7 +308,7 @@ def unregister_event_listener(self, subscriberid, subscription_name, listener_ke
for topic_name in topic_list:
self.topicDirectory[topic_name].unregister_listener(listenerid)
del self.subscriberDirectory[subscriberid][subscription_name][listener_key]
debug.output(
debug_output(
'Listener on listener_key %s, subscription %s unregistered'
% (listener_key, subscription_name),
listenerid,
Expand All @@ -335,7 +335,7 @@ def remove_subscription(self, subscriberid, subscription_name):
listener_list = []
if subscriberid in self.subscriberDirectory:
if subscription_name in self.subscriberDirectory[subscriberid]:
debug.output(
debug_output(
"\n\n------Subscriber's subscription to %s is being removed"
% subscription_name,
subscriberid,
Expand All @@ -344,7 +344,7 @@ def remove_subscription(self, subscriberid, subscription_name):
listenerid = self.subscriberDirectory[subscriberid][subscription_name][
listener_key
]
debug.output(
debug_output(
'Unregistering listener on listener_key %s, subscription %s'
% (listener_key, subscription_name),
listenerid,
Expand All @@ -355,15 +355,15 @@ def remove_subscription(self, subscriberid, subscription_name):
)
for topic_name in topic_list:
self.topicDirectory[topic_name].unregister_listener(listenerid)
debug.output(
debug_output(
'Listener on listener_key %s, subscription %s unregistered'
% (listener_key, subscription_name),
listenerid,
subscriberid,
)
listener_list.append(listenerid)
del self.subscriberDirectory[subscriberid][subscription_name]
debug.output(
debug_output(
"Subscriber's subscription to %s removed" % subscription_name, subscriberid
)
"""
Expand All @@ -374,9 +374,9 @@ def remove_subscription(self, subscriberid, subscription_name):
"""
return listener_list

"""""" """Subscription methods end here""" """"""
"""Subscription methods end here"""

"""""" """Methods internal to the event service start here""" """"""
"""Methods internal to the event service start here"""

"""
A listener_key may specify a bunch of topics using wildcarding.
Expand All @@ -389,4 +389,4 @@ def _map_listener_key_to_topic_list(self, subscription_name, listener_key):
topic_list.append(listener_key)
return topic_list

"""""" """Methods internal to the event service end here""" """"""
"""Methods internal to the event service end here"""
4 changes: 2 additions & 2 deletions ipsframework/ips.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
from ipsframework.resource_manager import ResourceManager
from ipsframework.task_manager import TaskManager

if sys.version_info[0] != 3 or sys.version_info[1] < 9:
print('IPS is only compatible with Python 3.9 or higher', file=sys.stderr)
if sys.version_info < (3, 10):
print('IPS is only compatible with Python 3.10 or higher', file=sys.stderr)
sys.exit(1)


Expand Down
26 changes: 10 additions & 16 deletions ipsframework/ips_es_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ class EventManager:
def __init__(self, obj_ref):
self.obj_ref = obj_ref
self.objcache = {}
self.publisher = 'self.publisher'
self.subscriber = 'self.subscriber'
self._publisher_svc = None
self._subscriber_svc = None

def publish(self, topic_name, event_name, event_body):
if self.publisher in self.objcache:
pub = self.objcache[self.publisher]
else:
pub = PublisherEventService()
self.objcache[self.publisher] = pub
if self._publisher_svc is None:
self._publisher_svc = PublisherEventService()

topic = pub.get_topic(topic_name)
topic = self._publisher_svc.get_topic(topic_name)
topic.send_event(event_name, event_body)

def subscribe(self, topic_name, callback):
Expand All @@ -41,19 +38,16 @@ def subscribe(self, topic_name, callback):
# throw an exception?
return

if self.subscriber in self.objcache:
sub = self.objcache[self.subscriber]
else:
sub = SubscriberEventService()
self.objcache[self.subscriber] = sub
if self._subscriber_svc is None:
self._subscriber_svc = SubscriberEventService()

if topic_name in self.objcache:
# TODO: do we notify the client to do an unsubscribe before
# re-subscribing to the same topic? the event service
# currently throws an exception in this scenario...
scp = self.objcache[topic_name]
else:
scp = sub.get_subscription(topic_name)
scp = self._subscriber_svc.get_subscription(topic_name)
self.objcache[topic_name] = scp

evl = MyEventListener(callback_method)
Expand All @@ -68,8 +62,8 @@ def unsubscribe(self, topic_name):
# throw an exception?

def process_events(self):
if self.subscriber in self.objcache:
self.objcache[self.subscriber].process_events()
if self._subscriber_svc is not None:
self._subscriber_svc.process_events()
# else:
# TODO: do we notify the client to do a subscribe before processing?
# throw an exception?
Loading
Loading