class DataConsumer(BaseProcess): """ A class for spawning as a separate process to consume the responses from the instrument. """ @defer.inlineCallbacks 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 @defer.inlineCallbacks def op_data(self, content, headers, msg): """ Data has been received. Increment the receive_cnt """ self.receive_cnt += 1 self.received_msg.append(content)
class DataConsumer(BaseProcess): def set_ondata(self, ondata): self.ondata = ondata @defer.inlineCallbacks def attach(self, topic_name): self.dataReceiver = Receiver(__name__, topic_name) self.dataReceiver.handle(self.receive) self.dr_id = yield spawn(self.dataReceiver) logging.info("DataConsumer.attach " + str(self.dr_id) + " to topic " + str(topic_name)) self.receive_cnt = 0 self.received_msg = [] self.ondata = None @defer.inlineCallbacks def op_data(self, content, headers, msg): logging.info("Data message received: " + repr(content)) self.receive_cnt += 1 self.received_msg.append(content) if hasattr(self, "ondata") and self.ondata: logging.info("op_data: Executing data process") res = self.ondata(content, headers) logging.info("op_data: Finished data process") if res: for (topic, msg) in res: yield self.send(self.get_scoped_name("system", topic), "data", msg, {})
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 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 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 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 __init__(self, receiver=None, spawnArgs=None): """ Initialize process using an optional receiver and optional spawn args @param receiver instance of a Receiver for process control @param spawnArgs standard and additional spawn arguments """ #logging.debug('BaseProcess.__init__()') self.procState = "UNINITIALIZED" spawnArgs = spawnArgs.copy() if spawnArgs else {} self.spawnArgs = spawnArgs self.init_time = pu.currenttime_ms() # Name (human readable label) of this process. self.procName = self.spawnArgs.get('proc-name', __name__) # The system unique ID; propagates from root supv to all child procs sysname = ioninit.cont_args.get('sysname', Container.id) self.sysName = self.spawnArgs.get('sys-name', sysname) # The process ID of the supervisor process self.procSupId = pu.get_process_id(self.spawnArgs.get('sup-id', None)) if not receiver: receiver = Receiver(self.procName) self.receiver = receiver receiver.handle(self.receive) # Dict of all receivers of this process. Key is the name self.receivers = {} self.add_receiver(self.receiver) # Dict of converations. # @todo: make sure this is garbage collected once in a while self.conversations = {} # Conversations by conv-id for currently outstanding RPCs self.rpc_conv = {} # List of ProcessDesc instances of defined and spawned child processes self.child_procs = [] logging.info("Process init'd: proc-name=%s, sup-id=%s, sys-name=%s" % ( self.procName, self.procSupId, self.sysName))
class TestSensorAggregator(BaseProcess): """Class for the client accessing the object store. """ def __init__(self, queue_name_events, *args): BaseProcess.__init__(self, *args) self.queue_name_events = queue_name_events self.message_count = 0 @defer.inlineCallbacks def plc_init(self): # create new receiver ('listener'), for the events (just the receiver object) self.event_receiver = Receiver("event_receiver", self.queue_name_events) # set BaseProcesses receive method as the callback for the new receiver that was just created. self.event_receiver.handle(self.receive) # actually create queue consumer: receiver_id = yield spawn(self.event_receiver) def op_event(self, content, headers, msg): self.message_count = int(content["messages"])