def _watcher(self): res = [] while True: if self.scan or res: try: self.update_all() except Exception as ex: fix_exception(ex) process_failure(ex) resl = len(res) while res: q = res.pop() q.set_exception(ex) else: resl = len(res) while res: q = res.pop() q.set(None) if TESTING: if resl: d = 5 else: d = 30 else: if resl: d = 10 else: d = 300 while True: try: q = self.watch_q.get(timeout=(None if not self.scan else d if not res else 0)) except Empty: break else: res.append(q)
def dataReceived(self, data): self._stop_timer() data = self.dbuf+data while True: xi = len(data)+1 try: pi = data.index(b'\r') except ValueError: pi = xi try: ei = data.index(b'\n') except ValueError: ei = xi if pi==xi and ei==xi: break if pi < ei: self.lbuf = data[:pi].decode('utf-8') data = data[pi+1:] else: msg = data[:ei].decode('utf-8') data = data[ei+1:] if msg == "" and self.lbuf is not None: msg = self.lbuf self.lbuf = None try: self.lineReceived(msg) except Exception as e: log("fs20",ERROR,msg,e) fix_exception(e) process_failure(e) self.dbuf = data self._start_timer()
def __init__(self, name, host,port, socket=None): self.socket = socket self.host = host self.port = port self.name = name storage2 = getattr(self,"storage2",{}) assert (host,port) not in storage2, "already known host/port tuple" super(NetCommonConnector,self).__init__() storage2[(host,port)] = self external = (self.socket is not None) if self.socket is None: try: self._connect() except Exception as ex: fix_exception(ex) if isinstance(ex,EnvironmentError) and ex.errno in (errno.ECONNREFUSED,errno.EHOSTDOWN,errno.EHOSTUNREACH,errno.ENETDOWN,errno.ENETUNREACH,errno.ETIMEDOUT): ex.no_backtrace = True try: del storage2[(host,port)] except KeyError: pass else: super(NetCommonConnector,self).delete() try: self.not_up_event(error=ex) except Exception as ex2: fix_exception(ex2) process_failure(ex2) reraise(ex) self.handshake(False) else: self.handshake(True) self.start_job("job",self._reader)
def _do_measure(self): log("monitor",TRACE,"Start run",self.name) try: self.running.clear() self.started_at = now() self._monitor() if self.send_check_event: simple_event(self.ectx, "monitor","checked",*self.name) if self.new_value is not None: self.last_value = self.value self.value = self.new_value if hasattr(self,"delta"): if self.last_value is not None: val = self.value-self.last_value self._ectx.value_delta = val if val >= 0 or self.delta == 0: simple_event(self.ectx,"monitor","update",*self.name) else: simple_event(self.ectx,"monitor","update",*self.name) except Exception as e: fix_exception(e) process_failure(e) finally: log("monitor",TRACE,"Stop run",self.name) self.running.set() self._schedule()
def error(self,err): """An error occurred.""" if self.result is not None and not self.result.successful: log("onewire",DEBUG,"done error: ",self,err) self.result.set_exception(err) else: process_failure(err)
def error(self, err): """An error occurred.""" if self.result is not None and not self.result.successful: log("onewire", DEBUG, "done error: ", self, err) self.result.set_exception(err) else: process_failure(err)
def _watcher(self): res = [] while True: if self.scan or res: try: self.update_all() except Exception as ex: fix_exception(ex) process_failure(ex) resl = len(res) while res: q = res.pop() q.set_exception(ex) else: resl = len(res) while res: q = res.pop() q.set(None) if TESTING: if resl: d = 5 else: d = 30 else: if resl: d = 10 else: d = 300 while True: try: q = self.watch_q.get(timeout=( None if not self.scan else d if not res else 0)) except Empty: break else: res.append(q)
def __init__(self, name, host, port, vhost, username, password): self.name = name self.host = host self.port = port self.vhost = vhost self.username = username self.password = password self.workers = [] try: self.conn = amqp.connection.Connection(host=self.host, userid=self.username, password=self.password, login_method='AMQPLAIN', login_response=None, virtual_host=self.vhost) except Exception as ex: simple_event("amqp", "error", *name, error=str(ex), deprecated=True) simple_event("amqp", "state", *name, error=str(ex), state="error") fix_exception(ex) process_failure(ex) else: super(AMQPclient, self).__init__() simple_event("amqp", "connect", *name, deprecated=True) simple_event("amqp", "state", *name, state="connect")
def process(self, event, **k): super(OnEventExec, self).process(**k) try: self.fn(event) except Exception as ex: fix_exception(ex) process_failure(ex)
def dataReceived(self, data): self._stop_timer() data = self.dbuf + data while True: xi = len(data) + 1 try: pi = data.index(b'\r') except ValueError: pi = xi try: ei = data.index(b'\n') except ValueError: ei = xi if pi == xi and ei == xi: break if pi < ei: self.lbuf = data[:pi].decode('utf-8') data = data[pi + 1:] else: msg = data[:ei].decode('utf-8') data = data[ei + 1:] if msg == "" and self.lbuf is not None: msg = self.lbuf self.lbuf = None try: self.lineReceived(msg) except Exception as e: log("fs20", ERROR, msg, e) fix_exception(e) process_failure(e) self.dbuf = data self._start_timer()
def process(self, event,**k): super(OnEventExec,self).process(**k) try: self.fn(event) except Exception as ex: fix_exception(ex) process_failure(ex)
def do_timed_switch(self): self.timer = None try: self.do_switch() except Exception as e: fix_exception(e) process_failure(e)
def _start(self): reported = False while True: sleep(self.freq) try: self.time_start = now() self.old_seen = self.seen.copy() # log(DEBUG,"SCAN",self.path,"IN",self.bus) self.bus.dir(path=self.path+('alarm',), proc=self._reporter, cached=False) for id in self.old_seen: simple_event("onewire","alarm","state",id, bus=self.bus.bus.name, path=self.path, id=id, state="off") self.seen.remove(id) except Exception as e: self.last_error = e if not reported: reported = True fix_exception(e) process_failure(e) self.time_len = now()-self.time_start sleep(self.freq*10) else: reported = False self.time_len = now()-self.time_start for x in self.simul: x[0] += 1 if x[0] >= x[1]: x[0] = 0 self.bus.set(self.path+('simultaneous',x[2]),x[3])
def err(e): try: err = e.get() except BaseException as e: err = e from moat.run import process_failure fix_exception(err) process_failure(err)
def go_down(self, _=None): if not self.is_up: return self.is_up = False if _ is not None: process_failure(_) simple_event("onewire","down",typ=self.typ,id=self.id,bus=self.bus.name,path=self.path, deprecated=True) simple_event("onewire","device","state",self.id, typ=self.typ,id=self.id,bus=self.bus.name,path=self.path, state="down")
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 _run(self,ctx,**k): try: super(TryStatement,self).run(ctx,**k) except Exception as err: fix_exception(err) if self.catch_do: self.catch_do.run(ctx(error_=err), **k) else: process_failure(err)
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 _run(self, ctx, **k): try: super(TryStatement, self).run(ctx, **k) except Exception as err: fix_exception(err) if self.catch_do: self.catch_do.run(ctx(error_=err), **k) else: process_failure(err)
def _shut_down(): """\ Code to be called last. The main loop is running and will be stopped when all events have progressed. """ try: process_event(shutdown_event) except Exception as e: fix_exception(e) process_failure(e)
def update_all(self): try: simple_event("onewire","scanning",*self.name, deprecated=True) simple_event("onewire","scan",*self.name, run="running") self._update_all() except Exception as e: fix_exception(e) process_failure(e) # error only; success below simple_event("onewire","scan",*self.name, run="error", error=str(e))
def _get_typ(self): try: t = self.get("type") self._setattr(t,"typ") except Exception as ex: del self.typ del devices[self.id] fix_exception(ex) process_failure(ex) else: self.go_up()
def _get_typ(self): try: t = self.get("type") self._setattr(t, "typ") except Exception as ex: del self.typ del devices[self.id] fix_exception(ex) process_failure(ex) else: self.go_up()
def delete(self,ctx=None): if self.timer: self.timer.cancel() self.timer = None try: if self.state: simple_event("pwm","change",*self.name, value=0) except Exception as ex: fix_exception(ex) process_failure(ex) finally: super(CommonPM,self).delete()
def run(p, *a, **k): try: p(*a, **k) except TrySomethingElse: pass except Exception as exc: from moat.logging import log_exc from moat.run import process_failure print_exc() fix_exception(exc) log_exc(msg="Unhandled exception", err=exc) process_failure(exc)
def run(p,*a,**k): try: p(*a,**k) except TrySomethingElse: pass except Exception as exc: from moat.logging import log_exc from moat.run import process_failure print_exc() fix_exception(exc) log_exc(msg="Unhandled exception", err=exc) process_failure(exc)
def exposed_cmd_list(self, *args): # don't call this 'exposed_list'! c = get_collect(args, allow_collection=True) try: if c is None: for m in all_collect(skip=False): yield m.name, elif isinstance(c, Collection): if args[-1] == "*": for n, m in c.items(): yield n, m return for n, m in c.items(): try: m = m.info except AttributeError: m = m.name else: if callable(m): m = m() if isinstance(m, six.string_types): m = m.split("\n")[0].strip() if m is not None: yield (n, m) else: yield n, else: for p, t in flatten((c, )): if isinstance(t, datetime): if TESTING: if t.year != 2003: t = "%s" % (humandelta(t - now(t.year != 2003)), ) else: t = "%s (%s)" % ( humandelta(t - now(t.year != 2003)), t) ti = t.rfind('.') if ti > 0 and len(t) - ti > 3 and len( t) - ti < 9: # limit to msec t = t[:ti + 3] + ")" # otherwise transmit the datetime as-is elif not isinstance(t, (date, time, timedelta)): t = six.text_type(t) yield p, t except Exception as e: fix_exception(e) yield "* ERROR *", repr(e) process_failure(e)
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 process(self, **k): super(WorkSequence,self).process(**k) assert self.work,"empty workqueue in "+repr(self) self.in_step = step = 0 self.in_worker = None res = None from moat.logging import log_run,log_halted try: event = self.event except Exception as ex: fix_exception(ex) event = ex excepting = False for w in self.work: step += 1 self.in_step = step self.in_worker = w r = None try: if not excepting or isinstance(w,ExcWorker): log_run(self,w,step) r = w.process(event=self.event, queue=self) except TrySomethingElse: pass except Exception as ex: fix_exception(ex) r = ex if isinstance(r,Exception): excepting = True if not hasattr(r,"within"): r.within=[w] r.within.append(self) if res is None: res = r elif isinstance(res,Exception): from moat.logging import log_exc log_exc(msg="Unhandled nested exception", err=res) from moat.run import process_failure process_failure(res) res = r if isinstance(res,Exception): reraise(res)
def process(self, event=None, **k): super(EventCallback, self).process(**k) # This is an event monitor. Failures will not be tolerated. try: msg = getattr(event.ctx, 'raw', None) codec = "application/binary" if msg is None: codec = json.CODEC for x, y in event.ctx: if x == 'event': continue if isinstance( y, six.string_types + six.integer_types + (bool, float, list, tuple)): d[x] = y elif hasattr(y, 'name'): d[x] = y.name if 'timestamp' not in d: d['timestamp'] = now() try: msg = json.encode(dict(event=list(event), **d)) except (TypeError, UnicodeDecodeError) as e: msg = json.encode( dict(data=repr(event) + "|" + repr(d) + "|" + repr(e))) elif isinstance(msg, six.integer_types + (float, )): msg = str(msg) codec = "text/plain" elif isinstance(msg, six.string_types): msg = msg.encode("utf-8") codec = "text/plain" global _mseq _mseq += 1 msg = amqp.Message(body=msg, content_type=codec, message_id=base_mseq + str(_mseq)) self.channel.basic_publish( msg=msg, exchange=self.exchange, routing_key=".".join( str(x) for x in self.prefix + tuple(event)[self.strip:])) except Exception as ex: fix_exception(ex) process_failure(ex) try: self.cancel() except Exception as ex: fix_exception(ex) process_failure(ex) raise TrySomethingElse
def process(self, **k): super(WorkSequence, self).process(**k) assert self.work, "empty workqueue in " + repr(self) self.in_step = step = 0 self.in_worker = None res = None from moat.logging import log_run, log_halted try: event = self.event except Exception as ex: fix_exception(ex) event = ex excepting = False for w in self.work: step += 1 self.in_step = step self.in_worker = w r = None try: if not excepting or isinstance(w, ExcWorker): log_run(self, w, step) r = w.process(event=self.event, queue=self) except TrySomethingElse: pass except Exception as ex: fix_exception(ex) r = ex if isinstance(r, Exception): excepting = True if not hasattr(r, "within"): r.within = [w] r.within.append(self) if res is None: res = r elif isinstance(res, Exception): from moat.logging import log_exc log_exc(msg="Unhandled nested exception", err=res) from moat.run import process_failure process_failure(res) res = r if isinstance(res, Exception): reraise(res)
def process(self, **k): super(EventCallback, self).process(**k) # This is an event monitor. Failures will not be tolerated. try: self.run_callback(**k) except Exception as ex: fix_exception(ex) process_failure(ex) try: self.cancel() except Exception as ex: fix_exception(ex) process_failure(ex) raise TrySomethingElse
def cont(self, _=None): while self.waiting: try: msg = self.waiting.pop(0) log("fs20", DEBUG, msg) d = self._dataReceived(msg) except Exception as e: fix_exception(e) process_failure(e) else: if d: d.addCallback(self.cont) return self.waiting = None self._start_timer()
def cont(self, _=None): while self.waiting: try: msg = self.waiting.pop(0) log("fs20",DEBUG,msg) d = self._dataReceived(msg) except Exception as e: fix_exception(e) process_failure(e) else: if d: d.addCallback(self.cont) return self.waiting = None self._start_timer()
def update_all(self): try: simple_event("onewire", "scanning", *self.name, deprecated=True) simple_event("onewire", "scan", *self.name, run="running") self._update_all() except Exception as e: fix_exception(e) process_failure(e) # error only; success below simple_event("onewire", "scan", *self.name, run="error", error=str(e))
def process(self, **k): super(EventCallback,self).process(**k) # This is an event monitor. Failures will not be tolerated. try: self.run_callback(**k) except Exception as ex: fix_exception(ex) process_failure(ex) try: self.cancel() except Exception as ex: fix_exception(ex) process_failure(ex) raise TrySomethingElse
def _worker(self): dx = 0.1 while True: if not self.alarmed: return try: src = self['alarm/sources'] if not src: self.alarmed = False return for s in src.split(','): si = self['alarm/' + s] if not si: continue # somebody else beat us to it if isinstance(si, six.integer_types): si = (str(si), ) else: si = si.split(',') for i in si: if s == "console": if i != 1: simple_event('moat', 'alarm', self.name, 'error', 'console', i) cons = self['console'] if cons != "": simple_event('moat', self.name, 'console', data=cons) elif s == "status": simple_event('moat', self.name, 'status', i, value=self['status/' + i]) else: self.tell(s, i) except (DisconnectedError, TimedOut) as e: fix_exception(e) process_failure(e) dx = 30 sleep(dx) dx *= 1.5 if dx > 3: dx = 3
def exposed_cmd_list(self,*args): # don't call this 'exposed_list'! c = get_collect(args, allow_collection=True) try: if c is None: for m in all_collect(skip=False): yield m.name, elif isinstance(c,Collection): if args[-1] == "*": for n,m in c.items(): yield n,m return for n,m in c.items(): try: m = m.info except AttributeError: m = m.name else: if callable(m): m = m() if isinstance(m,six.string_types): m = m.split("\n")[0].strip() if m is not None: yield (n,m) else: yield n, else: for p,t in flatten((c,)): if isinstance(t,datetime): if TESTING: if t.year != 2003: t = "%s" % (humandelta(t-now(t.year != 2003)),) else: t = "%s (%s)" % (humandelta(t-now(t.year != 2003)),t) ti = t.rfind('.') if ti>0 and len(t)-ti > 3 and len(t)-ti<9: # limit to msec t= t[:ti+3]+")" # otherwise transmit the datetime as-is elif not isinstance(t,(date,time,timedelta)): t = six.text_type(t) yield p,t except Exception as e: fix_exception(e) yield "* ERROR *",repr(e) process_failure(e)
def exposed_command(self,*args,**kwargs): try: sub = kwargs.get("sub",()) if sub: cmd = CommandProcessor(parent=self,ctx=self.ctx) proc = cmd.complex_statement(args) for s in sub: proc.simple_statement(s) proc.done() cmd.run() else: return CommandProcessor(parent=self,ctx=self.ctx).simple_statement(args) except Exception as e: fix_exception(e) process_failure(e) reraise(e)
def exposed_command(self, *args, **kwargs): try: sub = kwargs.get("sub", ()) if sub: cmd = CommandProcessor(parent=self, ctx=self.ctx) proc = cmd.complex_statement(args) for s in sub: proc.simple_statement(s) proc.done() cmd.run() else: return CommandProcessor(parent=self, ctx=self.ctx).simple_statement(args) except Exception as e: fix_exception(e) process_failure(e) reraise(e)
def dataReceived(self,val): buffer = self.buffer + val data = [] while True: i = buffer.find(self.delimiter) if i < 0: break data.append(buffer[:i]) buffer = buffer[i+len(self.delimiter):] self.buffer = buffer for d in data: try: self.lineReceived(d.decode('utf-8')) except Exception as e: fix_exception(e) process_failure(e)
def dataReceived(self, val): buffer = self.buffer + val data = [] while True: i = buffer.find(self.delimiter) if i < 0: break data.append(buffer[:i]) buffer = buffer[i + len(self.delimiter):] self.buffer = buffer for d in data: try: self.lineReceived(d.decode('utf-8')) except Exception as e: fix_exception(e) process_failure(e)
def _command(self, args=(), sub=(), **kwargs): ctx = Context(**kwargs) ctx.out = Reporter() ctx.words = global_words() # self.ctx) try: if sub: cmd = CommandProcessor(parent=self, ctx=ctx) proc = cmd.complex_statement(args) for s in sub: proc.simple_statement(s) proc.done() cmd.run() else: CommandProcessor(parent=self, ctx=ctx).simple_statement(args) except Exception as e: fix_exception(e) process_failure(e) reraise(e) return ctx.out.data
def _command(self, args=(), sub=(),**kwargs): ctx = Context(**kwargs) ctx.out = Reporter() ctx.words = global_words() # self.ctx) try: if sub: cmd = CommandProcessor(parent=self,ctx=ctx) proc = cmd.complex_statement(args) for s in sub: proc.simple_statement(s) proc.done() cmd.run() else: CommandProcessor(parent=self,ctx=ctx).simple_statement(args) except Exception as e: fix_exception(e) process_failure(e) reraise(e) return ctx.out.data
def __init__(self,name,host,port, vhost,username,password): self.name = name self.host=host self.port=port self.vhost=vhost self.username=username self.password=password self.workers = [] try: self.conn=amqp.connection.Connection(host=self.host,userid=self.username,password=self.password,login_method='AMQPLAIN', login_response=None, virtual_host=self.vhost) except Exception as ex: simple_event("amqp","error",*name, error=str(ex), deprecated=True) simple_event("amqp","state",*name, error=str(ex), state="error") fix_exception(ex) process_failure(ex) else: super(AMQPclient,self).__init__() simple_event("amqp","connect",*name, deprecated=True) simple_event("amqp","state",*name, state="connect")
def _writer(self): errs = 0 for r in self.q: try: if r is FlushMe: self._flush() else: self._log(*r) except Exception as ex: errs += 1 fix_exception(ex) from moat.run import process_failure process_failure(ex) if errs > 10: reraise(ex) else: if errs: errs -= 1 finally: self.q.task_done() self.q.task_done() # for the StopIter
def do_switch(self): """Click""" if self.state: self.state = 0 tn = self.t_off else: self.state = 1 tn = self.t_on simple_event("pwm","change",*self.name, value=self.state) try: self.last = self.next if tn is not None: self.next = self.last + dt.timedelta(0,tn) self.timer = callLater(False,self.next,self.do_timed_switch) else: self.next = None except Exception as e: fix_exception(e) process_failure(e) simple_event("pwm","error",*self.name, error=e)
def _worker(self): dx = 0.1 while True: if not self.alarmed: return try: src = self['alarm/sources'] if not src: self.alarmed = False return for s in src.split(','): si = self['alarm/'+s] if not si: continue # somebody else beat us to it if isinstance(si,six.integer_types): si = (str(si),) else: si = si.split(',') for i in si: if s == "console": if i != 1: simple_event('moat','alarm',self.name,'error','console',i) cons = self['console'] if cons != "": simple_event('moat',self.name,'console', data=cons) elif s == "status": simple_event('moat',self.name,'status', i, value=self['status/'+i]) else: self.tell(s,i) except (DisconnectedError,TimedOut) as e: fix_exception(e) process_failure(e) dx = 30 sleep(dx) dx *= 1.5 if dx > 3: dx = 3
def go_down(self, _=None): if not self.is_up: return self.is_up = False if _ is not None: process_failure(_) simple_event("onewire", "down", typ=self.typ, id=self.id, bus=self.bus.name, path=self.path, deprecated=True) simple_event("onewire", "device", "state", self.id, typ=self.typ, id=self.id, bus=self.bus.name, path=self.path, state="down")
def _reader(self): try: while self.socket is not None: try: if self.socket.closed: return r = self.socket.recv(4096) if not r: return except Exception as e: fix_exception(e) process_failure(e) return try: self.dataReceived(r) except Exception as e: fix_exception(e) process_failure(e) finally: if self.socket: self.socket.close() self.socket = None self.down_event(True)
def _start(self): reported = False while True: sleep(self.freq) try: self.time_start = now() self.old_seen = self.seen.copy() # log(DEBUG,"SCAN",self.path,"IN",self.bus) self.bus.dir(path=self.path + ('alarm', ), proc=self._reporter, cached=False) for id in self.old_seen: simple_event("onewire", "alarm", "state", id, bus=self.bus.bus.name, path=self.path, id=id, state="off") self.seen.remove(id) except Exception as e: self.last_error = e if not reported: reported = True fix_exception(e) process_failure(e) self.time_len = now() - self.time_start sleep(self.freq * 10) else: reported = False self.time_len = now() - self.time_start for x in self.simul: x[0] += 1 if x[0] >= x[1]: x[0] = 0 self.bus.set(self.path + ('simultaneous', x[2]), x[3])
def __init__(self, name, host, port, socket=None): self.socket = socket self.host = host self.port = port self.name = name storage2 = getattr(self, "storage2", {}) assert (host, port) not in storage2, "already known host/port tuple" super(NetCommonConnector, self).__init__() storage2[(host, port)] = self external = (self.socket is not None) if self.socket is None: try: self._connect() except Exception as ex: fix_exception(ex) if isinstance(ex, EnvironmentError) and ex.errno in ( errno.ECONNREFUSED, errno.EHOSTDOWN, errno.EHOSTUNREACH, errno.ENETDOWN, errno.ENETUNREACH, errno.ETIMEDOUT): ex.no_backtrace = True try: del storage2[(host, port)] except KeyError: pass else: super(NetCommonConnector, self).delete() try: self.not_up_event(error=ex) except Exception as ex2: fix_exception(ex2) process_failure(ex2) reraise(ex) self.handshake(False) else: self.handshake(True) self.start_job("job", self._reader)