def __init__(self,conn,parent): self.parent = conn self.exchange=parent.exchange self.strip=parent.strip self.prefix = tuple(parent.prefix) self._direct = parent.shunt self.filter = parent.filter if self._direct: try: i = self.filter.index('*') except ValueError: i = 999 try: i = min(self.filter.index('*'),i) except ValueError: pass if i < self.strip or self.strip and not self.filter: raise RuntimeError("You can't use 'shunt' if you strip elements you can't restore!") for k in self.filter: if hasattr(k,'startswith') and k.startswith('*'): self._simple = False break name = parent.dest if name is None: global _seq _seq += 1 name = SName(conn.name+("f"+str(_seq),)) self.channel = self.parent.conn.channel() self.channel.exchange_declare(exchange=self.exchange, type='topic', auto_delete=False, passive=False) super(EventCallback,self).__init__(name) register_worker(self, self._direct)
def exposed_monitor(self, callback, *args): try: w = EventCallback(self, callback, *args) self.workers.add(w) register_worker(w) return w except Exception as ex: fix_exception(ex) process_failure(ex)
def __setitem__(self,key,val): assert val.name==key, repr(val.name)+" != "+repr(key) onHandlers[val.id] = val try: onHandlers2[val.args].append(val) except KeyError: onHandlers2[val.args] = [val] super(_OnHandlers,self).__setitem__(key,val) register_worker(val)
def exposed_monitor(self,callback,*args): try: w = EventCallback(self,callback,*args) self.workers.add(w) register_worker(w) return w except Exception as ex: fix_exception(ex) process_failure(ex)
def __setitem__(self, key, val): assert val.name == key, repr(val.name) + " != " + repr(key) onHandlers[val.id] = val try: onHandlers2[val.args].append(val) except KeyError: onHandlers2[val.args] = [val] super(_OnHandlers, self).__setitem__(key, val) register_worker(val)
def __init__(self,name, host,port,vhost,app,codec, username,password): self.name = name self.host=host self.port=port self.vhost=vhost self.app=app self.codec=codec self.username=username self.password=password super().__init__() self.server = qbroker.Unit(self.app, amqp=dict(codec=self.codec, server={'host':self.host,'port':self.port,'virtualhost':self.vhost,'login':self.username,'password':self.password}), loop=qbroker.loop) self._rpc_connect() self.evt = EventCallback(self) register_worker(self.evt)
def start_up(): """\ Code to be called first. The main loop is NOT running. """ register_worker(Shutdown_Worker("shutdown handler")) global running if not running: running = True try: process_event(startup_event) except Exception as e: fix_exception(e) process_failure(e)
def __init__(self, conn, parent): self.parent = conn self.exchange = parent.exchange self.strip = parent.strip self.prefix = tuple(parent.prefix) self._direct = parent.shunt self.filter = parent.filter if self._direct: try: i = self.filter.index('*') except ValueError: i = 999 try: i = min(self.filter.index('*'), i) except ValueError: pass if i < self.strip or self.strip and not self.filter: raise RuntimeError( "You can't use 'shunt' if you strip elements you can't restore!" ) for k in self.filter: if hasattr(k, 'startswith') and k.startswith('*'): self._simple = False break name = parent.dest if name is None: global _seq _seq += 1 name = SName(conn.name + ("f" + str(_seq), )) self.channel = self.parent.conn.channel() self.channel.exchange_declare(exchange=self.exchange, type='topic', auto_delete=False, passive=False, durable=True) super(EventCallback, self).__init__(name) register_worker(self, self._direct)
def __init__(self, name, host, port, vhost, app, codec, username, password): self.name = name self.host = host self.port = port self.vhost = vhost self.app = app self.codec = codec self.username = username self.password = password super().__init__() self.server = qbroker.Unit(self.app, amqp=dict(codec=self.codec, server={ 'host': self.host, 'port': self.port, 'virtualhost': self.vhost, 'login': self.username, 'password': self.password }), loop=qbroker.loop) self._rpc_connect() self.evt = EventCallback(self) register_worker(self.evt)
level = a[0] level = LogLevels.get(level, level) if lim > level: return a = (b, ) + a[1:] level = LogNames[level] logger.log(getattr(logging, level, logging.DEBUG), "%s", " ".join(str(x) for x in a)) exc = [] for l in list(Loggers.values()): if not l.ready: continue try: l.log(level, *a) except Exception as e: fix_exception(e) print("LOGGER CRASH 0", file=sys.stderr) print_exception(e, file=sys.stderr) l.delete() exc.append(sys.exc_info()) if exc: for e in exc: log_exc(msg="Logging error", err=e) log_event = LogWorker() register_worker(log_event) log_event = log_event.process
for w in sorted(collections.values(),key=lambda x:x.prio): if not w.can_do("del"): continue for d in w.values(): try: d.delete(event.ctx) except Exception as ex: fix_exception(ex) print_exception(ex) # Logging may not work any more def report(self,*a,**k): return () register_worker(Shutdown_Collections("free all collections")) class ShutdownHandler(Statement): """A command handler to stop the whole thing.""" name="shutdown" doc="stops executing the program." long_doc="""\ shutdown stops executing the program. shutdown now ... but does not wait for active events to terminate. """ def run(self,ctx,**k): event = self.params(ctx) if len(event): if tuple(event) == ("now",): _stop_mainloop() return
b = level level = a[0] level = LogLevels.get(level,level) if lim > level: return a = (b,)+a[1:] level = LogNames[level] logger.log(getattr(logging,level,logging.DEBUG),"%s"," ".join(str(x) for x in a)) exc = [] for l in list(Loggers.values()): if not l.ready: continue try: l.log(level, *a) except Exception as e: fix_exception(e) print("LOGGER CRASH 0", file=sys.stderr) print_exception(e,file=sys.stderr) l.delete() exc.append(sys.exc_info()) if exc: for e in exc: log_exc(msg="Logging error", err=e) log_event = LogWorker() register_worker(log_event) log_event = log_event.process
for w in sorted(collections.values(), key=lambda x: x.prio): if not w.can_do("del"): continue for d in w.values(): try: d.delete(event.ctx) except Exception as ex: fix_exception(ex) print_exception(ex) # Logging may not work any more def report(self, *a, **k): return () register_worker(Shutdown_Collections("free all collections")) class ShutdownHandler(Statement): """A command handler to stop the whole thing.""" name = "shutdown" doc = "stops executing the program." long_doc = """\ shutdown stops executing the program. shutdown now ... but does not wait for active events to terminate. """ def run(self, ctx, **k): event = self.params(ctx) if len(event): if tuple(event) == ("now", ):
class SayMoreWorker(SeqWorker): """A WorkSequence-generating worker which logs something twice.""" prio = 5 def does_event(self,e): return e[0]=="say more" def process(self,event=None,**k): super(SayMoreWorker,self).process(event=event,**k) w = WorkSequence(event,self) w.append(SayWorker("TellOne")) w.append(SayWorker("TellTwo")) return w from test import run_logger run_logger("basic") hello_ev = Event(Context(), "say","hello") hello2_ev = Event(Context(), "say more","greeting") register_worker(SayWorker("TellMe")) register_worker(SayMoreWorker("say something")) def main(): try: process_event(hello_ev) process_event(hello2_ev) finally: shut_down() mainloop(main)