def test_message_during_init(self): child2 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') pid2 = yield self.test_sup.spawn_child(child2, init=False) proc2 = self._get_procinstance(pid2) proc2.plc_init = proc2.plc_noinit self.assertEquals(proc2.proc_state, 'NEW') msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type': 'worker', 'args': {'scope': 'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc2.proc_name, msgName) extraRec.handle(proc2.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) yield self.test_sup.send(pid2, 'init', {}, {'quiet': True}) logging.info('Sent init to process 1') yield pu.asleep(0.5) self.assertEquals(proc2.proc_state, 'INIT') (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init') yield pu.asleep(2) self.assertEquals(proc2.proc_state, 'ACTIVE') (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly after init')
def test_spawn_child(self): child1 = ProcessDesc(name='echo', module='ion.core.test.test_baseprocess') self.assertEquals(child1.proc_state, 'DEFINED') pid1 = yield self.test_sup.spawn_child(child1) self.assertEquals(child1.proc_state, 'INIT_OK') proc = self._get_procinstance(pid1) self.assertEquals( str(proc.__class__), "<class 'ion.core.test.test_baseprocess.EchoProcess'>") self.assertEquals(pid1, proc.receiver.spawned.id) logging.info('Process 1 spawned and initd correctly') (cont, hdrs, msg) = yield self.test_sup.rpc_send(pid1, 'echo', 'content123') self.assertEquals(cont['value'], 'content123') logging.info('Process 1 responsive correctly') # The following tests the process attaching a second receiver msgName = self.test_sup.get_scoped_name('global', pu.create_guid()) messaging = {'name_type': 'worker', 'args': {'scope': 'global'}} yield Container.configure_messaging(msgName, messaging) extraRec = Receiver(proc.proc_name, msgName) extraRec.handle(proc.receive) extraid = yield spawn(extraRec) logging.info('Created new receiver %s with pid %s' % (msgName, extraid)) (cont, hdrs, msg) = yield self.test_sup.rpc_send(msgName, 'echo', 'content456') self.assertEquals(cont['value'], 'content456') logging.info('Process 1 responsive correctly on second receiver')
def plc_init(self): # Init self and container annName = 'cc_announce' self.ann_name = self.get_scoped_name('system', annName) self.start_time = pu.currenttime_ms() self.containers = {} self.contalive = {} self.last_identify = 0 # Declare CC announcement name messaging = {'name_type': 'fanout', 'args': {'scope': 'system'}} yield Container.configure_messaging(self.ann_name, messaging) logging.info("Declared CC anounce name: " + str(self.ann_name)) # Attach to CC announcement name annReceiver = Receiver(annName + '.' + self.receiver.label, self.ann_name) annReceiver.group = self.receiver.group self.ann_receiver = annReceiver self.ann_receiver.handle(self.receive) self.add_receiver(self.ann_receiver) annid = yield spawn(self.ann_receiver) logging.info("Listening to CC anouncements: " + str(annid)) # Start with an identify request. Will lead to an announce by myself #@TODO - Can not send a message to a base process which is not initialized! yield self.send(self.ann_name, 'identify', 'started', {'quiet': True}) # Convenience HACK: Add a few functions to container shell self._augment_shell()
def __init__(self, receiver=None, spawnArgs=None): """ Initializes base service. The default service name is taken from the service declaration, a different service name can be provided in the spawnargs using the 'servicename' attribute. The service name, in its qualified form prefixed by the system name is the public name of the service inbound queue that is shared among all service processes with the same name """ BaseProcess.__init__(self, receiver, spawnArgs) # Determine public service messaging name either from spawn args or # use default name from service declaration #default_svcname = self.declare['name'] + '_' + self.declare['version'] default_svcname = self.declare['name'] self.svc_name = self.spawn_args.get('servicename', default_svcname) assert self.svc_name, "Service must have a declare with a valid name" # Scope (prefix) the service name with the system name msgName = self.get_scoped_name('system', self.svc_name) # Create a receiver (inbound queue consumer) for service name svcReceiver = Receiver(self.svc_name + '.' + self.receiver.label, msgName) if hasattr(self.receiver, 'group'): svcReceiver.group = self.receiver.group self.svc_receiver = svcReceiver self.svc_receiver.handle(self.receive) self.add_receiver(self.svc_receiver)
def attach(self, topic_name): """ Attach to the given topic name """ yield self.init() self.dataReceiver = Receiver(__name__, topic_name) self.dataReceiver.handle(self.receive) self.dr_id = yield spawn(self.dataReceiver) self.receive_cnt = 0 self.received_msg = [] self.ondata = None
def create_process_instance(self, svc_mod, className): """Given a class name and a loaded module, instantiate the class with a receiver. """ svc_class = pu.get_class(className, svc_mod) #if not issubclass(svc_class,BaseProcess): # raise RuntimeError("class is not a BaseProcess") receiver = Receiver(svc_mod.__name__) serviceInstance = svc_class(receiver) logging.info('create_process_instance: created service instance '+str(serviceInstance)) return receiver
def slc_init(self): msg_name = self.spawn_args['service-name'] scope = self.spawn_args['scope'] logging.info("slc_init name received:" + msg_name) msg_name1 = self.get_scoped_name(scope, msg_name) logging.info("slc_init name used:" + msg_name1) workReceiver = Receiver(__name__, msg_name1) self.workReceiver = workReceiver self.workReceiver.handle(self.receive) logging.info("slc_init worker receiver spawning") id = yield spawn(workReceiver) logging.info("slc_init worker receiver spawned:" + str(id))
def attach(self, queue): #@Note - I tried to put a try/except here, but it did not catch the error from magnet # Check and make sure it is not already attached? dataReceiver = Receiver(__name__, str(queue)) dataReceiver.handle(self.receive) dr_id = yield spawn(dataReceiver) #print dr_id, dataReceiver.name self.dataReceivers[dataReceiver.name] = dataReceiver self.receive_cnt[queue]=0 logging.info("DataConsumer.attach "+str(dr_id)+" to topic "+str(queue)) defer.returnValue(dr_id)
def test_process_basics(self): p1 = BaseProcess() self.assertEquals(p1.id, None) self.assertTrue(p1.receiver) self.assertEquals(p1.receiver.spawned, None) self.assertEquals(p1.proc_state, "NEW") pid1 = yield p1.spawn() self.assertEquals(pid1, p1.receiver.spawned.id) # Note: this tests init without actually sending a message. self.assertEquals(p1.proc_state, "ACTIVE") yield p1.op_init(None, None, None) self.assertEquals(p1.proc_state, "ERROR") rec = Receiver("myname") p2 = BaseProcess(rec) args = {'arg1': 'value1', 'arg2': {}} p3 = BaseProcess(None, args) self.assertEquals(p3.spawn_args, args)