Skip to content

Commit

Permalink
Merge pull request #117 from niolabs/service_name_to_service_id
Browse files Browse the repository at this point in the history
Service name to service id
  • Loading branch information
mattdodge committed Mar 9, 2018
2 parents fbd89bb + 7ac78ab commit fa2c21d
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 24 deletions.
2 changes: 2 additions & 0 deletions nio/block/base.py
Expand Up @@ -88,6 +88,7 @@ def configure(self, context):

self.logger = get_nio_logger(self.label())
self.logger.setLevel(self.log_level())
self._service_id = context.service_id
self._service_name = context.service_name

def start(self):
Expand Down Expand Up @@ -151,6 +152,7 @@ def notify_management_signal(self, signal):
"""
if isinstance(signal, BlockStatusSignal):
# set service block is part of
signal.service_id = self._service_id
signal.service_name = self._service_name
signal.block_id = self.id()
signal.block_name = self.name()
Expand Down
4 changes: 3 additions & 1 deletion nio/block/context.py
Expand Up @@ -4,7 +4,7 @@
class BlockContext(object):

def __init__(self, block_router, properties,
service_name='', command_url=''):
service_id=None, service_name='', command_url=''):
""" Initializes information needed for a Block
This BlockContext will be passed to the `configure` method on each
Expand All @@ -17,6 +17,7 @@ def __init__(self, block_router, properties,
properties (dict): The block properties (metadata) that
will be deserialized and loaded as block properties.
service_id (str): The service this block belongs to
service_name (str): The name of the service this block belongs to
command_url (str): The URL at which this block can be commanded.
This URL will not have host or port information, as that may
Expand All @@ -27,5 +28,6 @@ def __init__(self, block_router, properties,

self.block_router = block_router
self.properties = properties
self.service_id = service_id
self.service_name = service_name
self.command_url = command_url
17 changes: 10 additions & 7 deletions nio/block/tests/test_block_base.py
Expand Up @@ -74,12 +74,12 @@ def test_service_notify_management_signal(self):
def test_notify_signals(self):
"""Test the block can notify signals properly"""
blk = Block()
block_name = 'block1'
service_name = 'service1'
block_id = 'block1'
service_id = 'service1'
blk.configure(BlockContext(
BlockRouter(),
{"id": block_name},
service_name=service_name))
{"id": block_id},
service_id=service_id))

my_sigs = [Signal({"key": "val"})]
with patch.object(blk, '_block_router') as router_patch:
Expand Down Expand Up @@ -166,19 +166,22 @@ def test_populate_block_status_signal(self):
blk = Block()

block_name = 'block1'
service_id = 'service1_id'
service_name = 'service1'
blk.configure(BlockContext(
BlockRouter(),
{"id": "block_id",
"name": block_name},
service_id=service_id,
service_name=service_name))

warning = BlockStatusSignal(
RunnerStatus.warning, message='It just broke...')
self.assertIsNone(warning.service_name)
RunnerStatus.warning, message='It just broke...')
self.assertIsNone(warning.service_id)
self.assertIsNone(warning.block_name)
blk.notify_management_signal(warning)
self.assertIsNotNone(warning.service_name)
self.assertIsNotNone(warning.service_id)
self.assertIsNotNone(warning.block_name)
self.assertEqual(warning.service_id, service_id)
self.assertEqual(warning.service_name, service_name)
self.assertEqual(warning.block_name, block_name)
7 changes: 5 additions & 2 deletions nio/router/context.py
Expand Up @@ -5,7 +5,8 @@ class RouterContext(object):
def __init__(self, execution, blocks, settings=None,
mgmt_signal_handler=None,
instance_id=None,
service_name=None):
service_id=None,
service_name=""):
"""Initializes a router context.
Args:
Expand All @@ -19,11 +20,13 @@ def __init__(self, execution, blocks, settings=None,
mgmt_signal_handler (method): method to use to notify
management signals, receives signal as only parameter
instance_id: Instance the service belongs to
service_name (str): service this router belongs to
service_id (str): service this router belongs to
service_name (str): The name of the service this router belongs to
"""
self.execution = execution
self.blocks = blocks
self.settings = settings or {}
self.mgmt_signal_handler = mgmt_signal_handler
self.instance_id = instance_id
self.service_id = service_id
self.service_name = service_name
3 changes: 3 additions & 0 deletions nio/router/diagnostic.py
Expand Up @@ -16,6 +16,7 @@ def __init__(self):

self._start_time = None
self._instance_id = None
self._service_id = None
self._service_name = None
self._interval = None
self._mgmt_signal_handler = None
Expand All @@ -26,6 +27,7 @@ def __init__(self):

def configure(self, context):
self._instance_id = context.instance_id
self._service_id = context.service_id
self._service_name = context.service_name
self._interval = \
context.settings.get("diagnostic_interval", 3600)
Expand Down Expand Up @@ -85,6 +87,7 @@ def _send_diagnostic(self):
{
"type": "RouterDiagnostic",
"instance_id": self._instance_id,
"service_id": self._service_id,
"service": self._service_name,
"blocks_data": blocks_data,
"start_time": self._start_time,
Expand Down
3 changes: 3 additions & 0 deletions nio/router/tests/test_base_diagnostic.py
Expand Up @@ -47,6 +47,7 @@ def test_diagnostics(self):
""" Checking that router delivers signals and diagnostics """

instance_id = "instance1"
service_id = "service1_id"
service_name = "service1"

block_router = BlockRouter()
Expand Down Expand Up @@ -74,6 +75,7 @@ def test_diagnostics(self):
},
mgmt_signal_handler=signal_handler,
instance_id=instance_id,
service_id=service_id,
service_name=service_name)

block_router.do_configure(router_context)
Expand Down Expand Up @@ -101,6 +103,7 @@ def test_diagnostics(self):
signal = signal_handler.call_args[0][0]
self.assertEqual(signal.type, "RouterDiagnostic")
self.assertEqual(signal.instance_id, instance_id)
self.assertEqual(signal.service_id, service_id)
self.assertEqual(signal.service, service_name)
self.assertEqual(len(signal.blocks_data), 1)
block_data = signal.blocks_data[0]
Expand Down
11 changes: 7 additions & 4 deletions nio/router/tests/test_diagnostic.py
Expand Up @@ -16,7 +16,8 @@ def check_handler_was_called(signal_handler):
return signal_handler.call_count > 0

instance_id1 = "instance_id1"
service1 = "service1"
service1_id = "service1_id"
service1_name = "service1"
source_type = "source_type"
source1 = "source1"
target_type = "target_type"
Expand All @@ -34,7 +35,8 @@ def check_handler_was_called(signal_handler):
},
mgmt_signal_handler=signal_handler,
instance_id=instance_id1,
service_name=service1)
service_id=service1_id,
service_name=service1_name)

dm = DiagnosticManager()
dm.do_configure(router_context)
Expand All @@ -60,7 +62,8 @@ def check_handler_was_called(signal_handler):
# assert signal fields
self.assertEqual(signal.type, "RouterDiagnostic")
self.assertEqual(signal.instance_id, instance_id1)
self.assertEqual(signal.service, service1)
self.assertEqual(signal.service_id, service1_id)
self.assertEqual(signal.service, service1_name)
self.assertLessEqual(signal.start_time, signal.end_time)
for block_data in signal.blocks_data:
self.assertEqual(block_data["source_type"], source_type)
Expand Down Expand Up @@ -92,7 +95,7 @@ def test_times(self):
{},
mgmt_signal_handler=signal_handler,
instance_id=instance_id1,
service_name=service1)
service_id=service1)

dm = DiagnosticManager()
dm.do_configure(router_context)
Expand Down
2 changes: 1 addition & 1 deletion nio/router/tests/test_pool_executor.py
Expand Up @@ -49,7 +49,7 @@ def test_pool_executor(self):
from nio.router.thread_pool_executor import ThreadedPoolExecutorRouter

block_router = ThreadedPoolExecutorRouter()
context = BlockContext(block_router, dict(), "service_name")
context = BlockContext(block_router, dict(), "service_id")

# create blocks
sender_block = SenderBlock()
Expand Down
6 changes: 4 additions & 2 deletions nio/service/base.py
Expand Up @@ -194,6 +194,7 @@ def configure(self, context):
context.router_settings,
context.mgmt_signal_handler,
context.instance_id,
self.id(),
self.name())
self._block_router.do_configure(router_context)
self.mgmt_signal_handler = context.mgmt_signal_handler
Expand All @@ -205,9 +206,10 @@ def _create_block_context(self, block_properties, service_context):
return BlockContext(
self._block_router,
block_properties,
service_context.properties.get('id', ''),
service_context.properties.get('id'),
service_context.properties.get('name', ""),
self._create_commandable_url(service_context.properties,
block_properties.get('id', ''))
block_properties.get('id'))
)

def _create_commandable_url(self, service_properties, block_alias):
Expand Down
14 changes: 11 additions & 3 deletions nio/signal/status.py
Expand Up @@ -29,11 +29,14 @@ class ServiceStatusSignal(StatusSignal):
status (RunnerStatus): The signal status.
message (str): An optional explanation to go with the status change.
service_name (str): The name of the service affected
service_id (str): The identifier of the service affected
"""

def __init__(self, status, message=None, service_name=None, **kwargs):
def __init__(self, status, message=None,
service_id=None, service_name=None, **kwargs):
super().__init__(status, message, **kwargs)
self.service_id = service_id
self.service_name = service_name


Expand All @@ -45,11 +48,16 @@ class BlockStatusSignal(StatusSignal):
status (RunnerStatus): The signal status.
message (str): An optional explanation to go with the status change.
block_name (str): The name of the block affected
service_name (str): The name of the service the block is contained in
block_id (str): The identifier of the block affected
service_id (str): The service the block is contained in
service_name (str): The name of the service affected
"""

def __init__(self, status, message=None,
block_name=None, service_name=None, **kwargs):
block_name=None, block_id=None,
service_id=None, service_name="", **kwargs):
super().__init__(status, message, **kwargs)
self.block_name = block_name
self.block_id = block_id
self.service_id = service_id
self.service_name = service_name
9 changes: 6 additions & 3 deletions nio/signal/tests/test_status_signal.py
Expand Up @@ -10,15 +10,18 @@ def test_service_status_signal(self):
signal = ServiceStatusSignal(
RunnerStatus.warning,
message="just testing",
service_name="MyService")
service_name="MyService", service_id="MyServiceID")
self.assertEqual(signal.service_name, "MyService")
self.assertEqual(signal.service_id, "MyServiceID")

def test_block_status_signal(self):
""" Ensure we can create a status signal for a block """
signal = BlockStatusSignal(
RunnerStatus.warning,
message="just testing",
block_name="MyBlock",
service_name="MyService")
block_id="MyBlockID",
service_id="MyService")
self.assertEqual(signal.block_name, "MyBlock")
self.assertEqual(signal.service_name, "MyService")
self.assertEqual(signal.block_id, "MyBlockID")
self.assertEqual(signal.service_id, "MyService")
2 changes: 1 addition & 1 deletion nio/testing/tests/test_block_test_case.py
Expand Up @@ -22,7 +22,7 @@ def management_signal_notified(self, block, signal):
""" Override a management signal notification handler """
self._management_notified = True
self.assertEqual(signal.block_name, block.name())
self.assertEqual(signal.service_name, block._service_name)
self.assertEqual(signal.service_id, block._service_id)

def test_allows_signal_notify(self):
""" Makes sure a test can assert how many signals were notified """
Expand Down

0 comments on commit fa2c21d

Please sign in to comment.