async def _updateport(self, key): unload_matcher = ModuleLoadStateChanged.createMatcher( self.target, ModuleLoadStateChanged.UNLOADING) async def updateinner(): self._reqid += 1 reqid = ('testobjectdb', self._reqid) with request_context(reqid, self.apiroutine): portobj = await call_api(self.apiroutine, 'objectdb', 'get', { 'key': key, 'requestid': reqid }) if portobj is not None: @updater def write_status(portobj): if portobj is None: raise ValueError('Already deleted') if not hasattr(portobj, 'owner'): portobj.owner = self._ownerid portobj.status = 'READY' return [portobj] else: raise ValueError('Already managed') try: await call_api(self.apiroutine, 'objectdb', 'transact', { 'keys': [portobj.getkey()], 'updater': write_status }) except ValueError: pass else: await portobj.waitif( self.apiroutine, lambda x: x.isdeleted() or hasattr(x, 'owner')) self._logger.info('Port managed: %r', dump(portobj)) while True: await portobj.waitif(self.apiroutine, lambda x: True, True) if portobj.isdeleted(): self._logger.info('Port deleted: %r', dump(portobj)) break else: self._logger.info('Port updated: %r', dump(portobj)) try: await self.apiroutine.withException(updateinner(), unload_matcher) except RoutineException: pass
def _updateport(self, key): unload_matcher = ModuleLoadStateChanged.createMatcher(self.target, ModuleLoadStateChanged.UNLOADING) def updateinner(): self._reqid += 1 reqid = ('testobjectdb', self._reqid) for m in callAPI(self.apiroutine, 'objectdb', 'get', {'key': key, 'requestid': reqid}): yield m portobj = self.apiroutine.retvalue with watch_context([key], [portobj], reqid, self.apiroutine): if portobj is not None: @updater def write_status(portobj): if portobj is None: raise ValueError('Already deleted') if not hasattr(portobj, 'owner'): portobj.owner = self._ownerid portobj.status = 'READY' return [portobj] else: raise ValueError('Already managed') try: for m in callAPI(self.apiroutine, 'objectdb', 'transact', {'keys': [portobj.getkey()], 'updater': write_status}): yield m except ValueError: pass else: for m in portobj.waitif(self.apiroutine, lambda x: x.isdeleted() or hasattr(x, 'owner')): yield m self._logger.info('Port managed: %r', dump(portobj)) while True: for m in portobj.waitif(self.apiroutine, lambda x: True, True): yield m if portobj.isdeleted(): self._logger.info('Port deleted: %r', dump(portobj)) break else: self._logger.info('Port updated: %r', dump(portobj)) try: for m in self.apiroutine.withException(updateinner(), unload_matcher): yield m except RoutineException: pass
def testproc(): yield (ModuleLoadStateChanged.createMatcher(),) for m in callAPI(r, "testmodule1", "method1", {}): yield m apiResults.append(r.retvalue) for m in callAPI(r, "testmodule1", "method2", {'a' : 1, 'b' : 2}): yield m apiResults.append(r.retvalue) try: for m in callAPI(r, "testmodule1", "method4", {}): yield m apiResults.append(None) except ValueError as exc: apiResults.append(exc.args[0]) from .gensrc.testmodule2 import ModuleTestEvent2 matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2(): for m in callAPI(r, "testmodule1", "method3", {'a' : 1, 'b' : 2}): yield m def callback(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2(), callback, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) for m in callAPI(r, "testmodule1", "discover", {}): yield m apiResults.append(r.retvalue) with open(os.path.join(basedir, 'testmodule1.py'), 'wb') as f: f.write(module1v2) for m in s.moduleloader.delegate(s.moduleloader.reloadModules(['tests.gensrc.testmodule1.TestModule1'])): yield m for m in callAPI(r, "testmodule1", "method1", {}): yield m apiResults.append(r.retvalue) matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2_2(): for m in callAPI(r, "testmodule1", "method3", {'a' : 1, 'b' : 2}): yield m def callback_2(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2_2(), callback_2, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) with open(os.path.join(basedir, 'testmodule2.py'), 'wb') as f: f.write(module2v2) for m in s.moduleloader.delegate(s.moduleloader.reloadModules(['tests.gensrc.testmodule2.TestModule2'])): yield m matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2_3(): for m in callAPI(r, "testmodule1", "method3", {'a' : 1, 'b' : 2}): yield m def callback_3(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2_3(), callback_3, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) with open(os.path.join(basedir, 'testmodule1.py'), 'wb') as f: f.write(module1v3) with open(os.path.join(basedir, 'testmodule2.py'), 'wb') as f: f.write(module2v3) for m in s.moduleloader.delegate(s.moduleloader.reloadModules(['tests.gensrc.testmodule1.TestModule1','tests.gensrc.testmodule2.TestModule2'])): yield m for m in callAPI(r, "testmodule1", "method1", {}): yield m apiResults.append(r.retvalue) matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2_4(): for m in callAPI(r, "testmodule1", "method3", {'a' : 1, 'b' : 2}): yield m def callback_4(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2_4(), callback_4, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) try: for m in r.executeWithTimeout(1.0, callAPI(r, "testmodule1", "notexists", {})): yield m except ValueError: apiResults.append(True) except: apiResults.append(False) else: apiResults.append(False) for m in s.moduleloader.delegate(s.moduleloader.unloadByPath("tests.gensrc.testmodule1.TestModule1")): yield m
def main(self): try: timestamp = '%012x' % (int(time() * 1000),) + '-' transactno = 1 for m in callAPI(self, 'redisdb', 'getclient', {'vhost':self.vhostbind}): yield m client, encoder, decoder = self.retvalue try: for m in client.subscribe(self, self.prefix): yield m except Exception: _no_connection_start = True else: _no_connection_start = False self._matchers[b''] = self.retvalue[0] if self._deflate: oldencoder = encoder olddecoder = decoder def encoder(x): return compress(oldencoder(x)) def decoder(x): try: return olddecoder(decompress(x)) except zlib_error: return olddecoder(x) if not _no_connection_start: self.subroutine(self._modifier(client), True, "modifierroutine") listen_modify = ModifyListen.createMatcher(self, ModifyListen.LISTEN) connection_down = client.subscribe_state_matcher(self, False) connection_up = client.subscribe_state_matcher(self, True) module_loaded = ModuleLoadStateChanged.createMatcher(state = ModuleLoadStateChanged.LOADED, _ismatch = lambda x: x._instance.getServiceName() == 'redisdb') matchers = tuple(self._matchers.values()) + (listen_modify, connection_down) last_transact = None while True: if not _no_connection_start: yield matchers if not _no_connection_start and self.matcher is listen_modify: matchers = tuple(self._matchers.values()) + (listen_modify, connection_down) elif _no_connection_start or self.matcher is connection_down: # Connection is down, wait for restore # The module may be reloaded if _no_connection_start: recreate_matchers = True else: recreate_matchers = False last_transact = None while True: yield (connection_up, module_loaded) if self.matcher is module_loaded: self.terminate(self.modifierroutine) for m in callAPI(self, 'redisdb', 'getclient', {'vhost':self.vhostbind}): yield m client, encoder, decoder = self.retvalue if self._deflate: oldencoder = encoder olddecoder = decoder def encoder(x): return compress(oldencoder(x)) def decoder(x): try: return olddecoder(decompress(x)) except zlib_error: return olddecoder(x) # Recreate listeners connection_down = client.subscribe_state_matcher(self, False) connection_up = client.subscribe_state_matcher(self, True) try: for m in client.subscribe(self, *tuple(self._matchers.keys())): yield m except Exception: recreate_matchers = True continue else: self._matchers = dict(zip(self._matchers.keys(), self.retvalue)) self.subroutine(self._modifier(client), True, "modifierroutine") matchers = tuple(self._matchers.values()) + (listen_modify, connection_down) break else: if recreate_matchers: try: for m in client.subscribe(self, *[self.prefix + k for k in self._matchers.keys()]): yield m except Exception: recreate_matchers = True continue else: self._matchers = dict(zip(self._matchers.keys(), self.retvalue)) self.subroutine(self._modifier(client), True, "modifierroutine") matchers = tuple(self._matchers.values()) + (listen_modify, connection_down) break else: matchers = tuple(self._matchers.values()) + (listen_modify, connection_down) break if self._publish_wait: self.subroutine(self.publish()) transactid = '%s%016x' % (timestamp, transactno) transactno += 1 def send_restore_notify(transactid): if self._matchadd_wait or self._matchremove_wait: # Wait for next subscribe success for m in self.waitWithTimeout(1, ModifyListen.createMatcher(self, ModifyListen.LISTEN)): yield m for m in self.waitForSend( UpdateNotification(self, transactid, tuple(self._matchers.keys()), UpdateNotification.RESTORED, False, extrainfo = None)): yield m self.subroutine(send_restore_notify(transactid), False) else: transact = decoder(self.event.message) if transact['id'] == last_transact: # Ignore duplicated updates continue last_transact = transact['id'] pubkey, sep, pubno = last_transact.partition('-') fromself = (sep and pubkey == self._publishkey) transactid = '%s%016x' % (timestamp, transactno) transactno += 1 self.subroutine(self.waitForSend( UpdateNotification(self, transactid, tuple(_bytes(k) for k in transact['keys']), UpdateNotification.UPDATED, fromself, extrainfo = transact.get('extrainfo')) ), False) finally: if hasattr(self ,'modifierroutine') and self.modifierroutine: self.terminate(self.modifierroutine)
def main(self): try: timestamp = '%012x' % (int(time() * 1000), ) + '-' transactno = 1 for m in callAPI(self, 'redisdb', 'getclient', {'vhost': self.vhostbind}): yield m client, encoder, decoder = self.retvalue try: for m in client.subscribe(self, self.prefix): yield m except Exception: _no_connection_start = True else: _no_connection_start = False self._matchers[b''] = self.retvalue[0] if self._deflate: oldencoder = encoder olddecoder = decoder def encoder(x): return compress(oldencoder(x)) def decoder(x): try: return olddecoder(decompress(x)) except zlib_error: return olddecoder(x) if not _no_connection_start: self.subroutine(self._modifier(client), True, "modifierroutine") listen_modify = ModifyListen.createMatcher(self, ModifyListen.LISTEN) connection_down = client.subscribe_state_matcher(self, False) connection_up = client.subscribe_state_matcher(self, True) module_loaded = ModuleLoadStateChanged.createMatcher( state=ModuleLoadStateChanged.LOADED, _ismatch=lambda x: x._instance.getServiceName() == 'redisdb') matchers = tuple( self._matchers.values()) + (listen_modify, connection_down) last_transact = None while True: if not _no_connection_start: yield matchers if not _no_connection_start and self.matcher is listen_modify: matchers = tuple(self._matchers.values()) + ( listen_modify, connection_down) elif _no_connection_start or self.matcher is connection_down: # Connection is down, wait for restore # The module may be reloaded if _no_connection_start: recreate_matchers = True else: recreate_matchers = False last_transact = None while True: yield (connection_up, module_loaded) if self.matcher is module_loaded: self.terminate(self.modifierroutine) for m in callAPI(self, 'redisdb', 'getclient', {'vhost': self.vhostbind}): yield m client, encoder, decoder = self.retvalue if self._deflate: oldencoder = encoder olddecoder = decoder def encoder(x): return compress(oldencoder(x)) def decoder(x): try: return olddecoder(decompress(x)) except zlib_error: return olddecoder(x) # Recreate listeners connection_down = client.subscribe_state_matcher( self, False) connection_up = client.subscribe_state_matcher( self, True) try: for m in client.subscribe( self, *tuple(self._matchers.keys())): yield m except Exception: recreate_matchers = True continue else: self._matchers = dict( zip(self._matchers.keys(), self.retvalue)) self.subroutine(self._modifier(client), True, "modifierroutine") matchers = tuple(self._matchers.values()) + ( listen_modify, connection_down) break else: if recreate_matchers: try: for m in client.subscribe( self, *[ self.prefix + k for k in self._matchers.keys() ]): yield m except Exception: recreate_matchers = True continue else: self._matchers = dict( zip(self._matchers.keys(), self.retvalue)) self.subroutine(self._modifier(client), True, "modifierroutine") matchers = tuple( self._matchers.values()) + ( listen_modify, connection_down) break else: matchers = tuple(self._matchers.values()) + ( listen_modify, connection_down) break if self._publish_wait: self.subroutine(self.publish()) transactid = '%s%016x' % (timestamp, transactno) transactno += 1 def send_restore_notify(transactid): if self._matchadd_wait or self._matchremove_wait: # Wait for next subscribe success for m in self.waitWithTimeout( 1, ModifyListen.createMatcher( self, ModifyListen.LISTEN)): yield m for m in self.waitForSend( UpdateNotification(self, transactid, tuple( self._matchers.keys()), UpdateNotification.RESTORED, False, extrainfo=None)): yield m self.subroutine(send_restore_notify(transactid), False) else: transact = decoder(self.event.message) if transact['id'] == last_transact: # Ignore duplicated updates continue last_transact = transact['id'] pubkey, sep, pubno = last_transact.partition('-') fromself = (sep and pubkey == self._publishkey) transactid = '%s%016x' % (timestamp, transactno) transactno += 1 self.subroutine( self.waitForSend( UpdateNotification( self, transactid, tuple(_bytes(k) for k in transact['keys']), UpdateNotification.UPDATED, fromself, extrainfo=transact.get('extrainfo'))), False) finally: if hasattr(self, 'modifierroutine') and self.modifierroutine: self.terminate(self.modifierroutine)
def testproc(): yield (ModuleLoadStateChanged.createMatcher(), ) for m in callAPI(r, "testmodule1", "method1", {}): yield m apiResults.append(r.retvalue) for m in callAPI(r, "testmodule1", "method2", {'a': 1, 'b': 2}): yield m apiResults.append(r.retvalue) try: for m in callAPI(r, "testmodule1", "method4", {}): yield m apiResults.append(None) except ValueError as exc: apiResults.append(exc.args[0]) from .gensrc.testmodule2 import ModuleTestEvent2 matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2(): for m in callAPI(r, "testmodule1", "method3", { 'a': 1, 'b': 2 }): yield m def callback(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2(), callback, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) for m in callAPI(r, "testmodule1", "discover", {}): yield m apiResults.append(r.retvalue) with open(os.path.join(basedir, 'testmodule1.py'), 'wb') as f: f.write(module1v2) for m in s.moduleloader.delegate( s.moduleloader.reloadModules( ['tests.gensrc.testmodule1.TestModule1'])): yield m for m in callAPI(r, "testmodule1", "method1", {}): yield m apiResults.append(r.retvalue) matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2_2(): for m in callAPI(r, "testmodule1", "method3", { 'a': 1, 'b': 2 }): yield m def callback_2(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2_2(), callback_2, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) with open(os.path.join(basedir, 'testmodule2.py'), 'wb') as f: f.write(module2v2) for m in s.moduleloader.delegate( s.moduleloader.reloadModules( ['tests.gensrc.testmodule2.TestModule2'])): yield m matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2_3(): for m in callAPI(r, "testmodule1", "method3", { 'a': 1, 'b': 2 }): yield m def callback_3(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2_3(), callback_3, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) with open(os.path.join(basedir, 'testmodule1.py'), 'wb') as f: f.write(module1v3) with open(os.path.join(basedir, 'testmodule2.py'), 'wb') as f: f.write(module2v3) for m in s.moduleloader.delegate( s.moduleloader.reloadModules([ 'tests.gensrc.testmodule1.TestModule1', 'tests.gensrc.testmodule2.TestModule2' ])): yield m for m in callAPI(r, "testmodule1", "method1", {}): yield m apiResults.append(r.retvalue) matcher = ModuleTestEvent2.createMatcher() self.event = False def proc2_4(): for m in callAPI(r, "testmodule1", "method3", { 'a': 1, 'b': 2 }): yield m def callback_4(event, matcher): self.event = event if False: yield for m in r.withCallback(proc2_4(), callback_4, matcher): yield m if not self.event: for m in r.waitWithTimeout(0.1, matcher): yield m if not r.timeout: self.event = r.event if self.event: apiResults.append((self.event.result, self.event.version)) else: apiResults.append(False) try: for m in r.executeWithTimeout( 1.0, callAPI(r, "testmodule1", "notexists", {})): yield m except ValueError: apiResults.append(True) except Exception: apiResults.append(False) else: apiResults.append(False) for m in s.moduleloader.delegate( s.moduleloader.unloadByPath( "tests.gensrc.testmodule1.TestModule1")): yield m