Пример #1
0
    def _run(self, syn):
        syn.get()
        try:
            while True:
                # allow tasks which kill this one to run
                gevent.sleep(0)
                l = self.input.readline()
                if not l:
                    self.add_line(".")
                    break
                self.add_line(l)

        except BaseException as e:
            fix_exception(e)
            return e
        finally:
            job = gevent.spawn(self.endConnection, kill=False)

            def dead(e):
                fix_exception(e)
                process_failure(e)

            job.link_exception(dead)
            self.input.close()
            self.input = None
        return "Bla"
Пример #2
0
	def simple_statement(self,args):
		fn = self.lookup(args)
		try:
			fn.run(self.ctx)
		except Exception as ex:
			fix_exception(ex)
			self.error(self,ex)
Пример #3
0
	def recv(self,msg):
		if msg.type is MT_MULTILINE:
			for x in msg.msg:
				if x == "":
					continue
				m = _num.match(x)
				if m is None:
					continue
				mon = int(m.group(0))
				self.data[mon]=x[m.end():]
				recvs,self.queue.receivers = self.queue.receivers,[]
				for r in recvs:
					mid = getattr(r,"msgid",None)
					if mid is None or mid in self.data:
						log("wago",TRACE,"found monitor",r)
						self.queue.receivers.append(r)
					else:
						try:
							raise DroppedMonitor(mid)
						except DroppedMonitor as ex:
							fix_exception(ex)
							res = r.error(ex)
							if res is SEND_AGAIN:
								log("wago",TRACE,"retry monitor",r)
								self.queue.enqueue(r)
							else:
								log("wago",TRACE,"drop monitor",r)
								assert res is None or res is False, "%s.error returned %s"%(repr(r),repr(res))

			return MINE
		if msg.type is MT_NAK or msg.type is MT_ERROR:
			simple_event("wago","monitor","error", msg=msg.msg)
			return MINE
		return NOT_MINE
Пример #4
0
    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)
Пример #5
0
	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)
Пример #6
0
	def do_timed_switch(self):
		self.timer = None
		try:
			self.do_switch()
		except Exception as e:
			fix_exception(e)
			process_failure(e)
Пример #7
0
    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")
Пример #8
0
	def _connect(self):
		e = None
		for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM):
			af, socktype, proto, canonname, sa = res
			try:
				s = socket.socket(af, socktype, proto)
			except socket.error as err:
				fix_exception(err)
				if e is None:
					e = err
				s = None
				continue
			try:
				s.connect(sa)
			except socket.error as err:
				fix_exception(err)
				if e is None:
					e = err
				s.close()
				s = None
				continue
			break
		if s is None:
			reraise(e)
		self.socket = s
Пример #9
0
 def _connect(self):
     e = None
     for res in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC,
                                   socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa = res
         try:
             s = socket.socket(af, socktype, proto)
         except socket.error as err:
             fix_exception(err)
             if e is None:
                 e = err
             s = None
             continue
         try:
             s.connect(sa)
         except socket.error as err:
             fix_exception(err)
             if e is None:
                 e = err
             s.close()
             s = None
             continue
         break
     if s is None:
         reraise(e)
     self.socket = s
Пример #10
0
	def process(self, event,**k):
		super(OnEventExec,self).process(**k)
		try:
			self.fn(event)
		except Exception as ex:
			fix_exception(ex)
			process_failure(ex)
Пример #11
0
 def process(self, event, **k):
     super(OnEventExec, self).process(**k)
     try:
         self.fn(event)
     except Exception as ex:
         fix_exception(ex)
         process_failure(ex)
Пример #12
0
    def get(self, key):
        if not self.bus:
            raise DisconnectedDeviceError(self.id)
        if not isinstance(key, tuple):
            key = Name(key)

        p = self.path
        if self.bus_id is not None:
            p += (self.bus_id, )
        p += key

        msg = ATTRgetmsg(p)
        msg.queue(self.bus)

        try:
            res = msg.result.get()
        except Exception as ex:
            fix_exception(ex)
            self.go_down(ex)
            raise

        if isinstance(res, bytes):
            res = res.decode('utf-8')

        try:
            res = int(res)
        except (ValueError, TypeError):
            try:
                res = float(res)
            except (ValueError, TypeError):
                pass

        return res
Пример #13
0
    def recv(self, msg):
        if msg.type is MT_MULTILINE:
            for x in msg.msg:
                if x == "":
                    continue
                m = _num.match(x)
                if m is None:
                    continue
                mon = int(m.group(0))
                self.data[mon] = x[m.end():]
                recvs, self.queue.receivers = self.queue.receivers, []
                for r in recvs:
                    mid = getattr(r, "msgid", None)
                    if mid is None or mid in self.data:
                        log("wago", TRACE, "found monitor", r)
                        self.queue.receivers.append(r)
                    else:
                        try:
                            raise DroppedMonitor(mid)
                        except DroppedMonitor as ex:
                            fix_exception(ex)
                            res = r.error(ex)
                            if res is SEND_AGAIN:
                                log("wago", TRACE, "retry monitor", r)
                                self.queue.enqueue(r)
                            else:
                                log("wago", TRACE, "drop monitor", r)
                                assert res is None or res is False, "%s.error returned %s" % (
                                    repr(r), repr(res))

            return MINE
        if msg.type is MT_NAK or msg.type is MT_ERROR:
            simple_event("wago", "monitor", "error", msg=msg.msg)
            return MINE
        return NOT_MINE
Пример #14
0
	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()
Пример #15
0
	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)
Пример #16
0
	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])
Пример #17
0
	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()
Пример #18
0
    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()
Пример #19
0
	def get(self,key):
		if not self.bus:
			raise DisconnectedDeviceError(self.id)
		if not isinstance(key,tuple):
			key = Name(key)

		p = self.path
		if self.bus_id is not None:
			p += (self.bus_id,)
		p += key

		msg = ATTRgetmsg(p)
		msg.queue(self.bus)

		try:
			res = msg.result.get()
		except Exception as ex:
			fix_exception(ex)
			self.go_down(ex)
			raise

		if isinstance(res,bytes):
			res = res.decode('utf-8')

		try:
			res = int(res)
		except (ValueError,TypeError):
			try:
				res = float(res)
			except (ValueError,TypeError):
				pass

		return res
Пример #20
0
def load_module(*m):
    md = dict()
    mod = None
    p = None
    for d in ModuleDirs:
        p = os.path.join(d, m[-1]) + ".py"
        try:
            c = compile(open(p, "r").read(), p, "exec", 0, True)
        except (OSError, IOError):
            md = None
            continue
        else:
            eval(c, md)
            break

    try:
        if not md:
            if TESTING:
                if os.path.isdir("modules"):
                    p = "modules"
                else:
                    p = os.path.join(os.pardir, "modules")
                p = os.path.join(p, m[-1]) + ".py"
                c = compile(open(p, "r").read(), p, "exec", 0, True)
            else:
                from pkg_resources import resource_string
                p = "moat.modules." + m[-1] + ".py"
                c = compile(resource_string("moat.modules", m[-1] + ".py"),
                            os.path.join('moat', 'modules', m[-1] + ".py"),
                            "exec", 0, True)
            eval(c, md)

        mod = md["init"]
        if callable(mod):
            mod = mod(*m)
        elif len(event) > 1:
            raise RuntimeError("You cannot parameterize this module.")
        if not hasattr(mod, "load"):
            mod.load = md["load"]
            mod.unload = md["unload"]

        try:
            mod.load()
        except BaseException as e:
            fix_exception(e)
            try:
                mod.unload()
            except Exception:
                reraise(e)
            else:
                reraise(e)
            # do not use finally: here
        else:
            mod.path = p
    except BaseException:
        if mod is not None and hasattr(mod, "name") and mod.name in Modules:
            del Modules[mod.name]
        raise
    return mod
Пример #21
0
 def report(self, verbose=False):
     try:
         yield u"END: " + six.text_type(Name(self.name[1:]))
     except Exception as e:
         fix_exception(e)
         print("LOGGER CRASH 4", file=sys.stderr)
         print_exception(e, file=sys.stderr)
         yield "END: REPORT_ERROR: " + repr(self.name[1:])
Пример #22
0
	def sendMsg(self,conn, typ,data, rlen=0):
		# messages are not tagged, so process received messages in strict order
		self.prio = PRIO_STANDARD
		try:
			conn.sendMsg(typ,data,rlen)
		except Exception as ex:
			fix_exception(ex)
			self.error(ex)
Пример #23
0
 def sendMsg(self, conn, typ, data, rlen=0):
     # messages are not tagged, so process received messages in strict order
     self.prio = PRIO_STANDARD
     try:
         conn.sendMsg(typ, data, rlen)
     except Exception as ex:
         fix_exception(ex)
         self.error(ex)
Пример #24
0
	def report(self, verbose=False):
		try:
			yield  u"END: "+six.text_type(Name(self.name[1:]))
		except Exception as e:
			fix_exception(e)
			print("LOGGER CRASH 4", file=sys.stderr)
			print_exception(e,file=sys.stderr)
			yield  "END: REPORT_ERROR: "+repr(self.name[1:])
Пример #25
0
	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)
Пример #26
0
def load_module(*m):
	md = dict()
	mod = None
	p = None
	for d in ModuleDirs:
		p = os.path.join(d,m[-1])+".py"
		try:
			c = compile(open(p,"r").read(), p, "exec",0,True)
		except (OSError,IOError):
			md = None
			continue
		else:
			eval(c,md)
			break

	try:
		if not md:
			if TESTING:
				if os.path.isdir("modules"):
					p = "modules"
				else:
					p = os.path.join(os.pardir,"modules")
				p = os.path.join(p,m[-1])+".py"
				c = compile(open(p,"r").read(), p, "exec",0,True)
			else:
				from pkg_resources import resource_string
				p = "moat.modules."+m[-1]+".py"
				c = compile(resource_string("moat.modules", m[-1]+".py"), os.path.join('moat','modules',m[-1]+".py"), "exec",0,True)
			eval(c,md)
	
		mod = md["init"]
		if callable(mod):
			mod = mod(*m)
		elif len(event) > 1:
			raise RuntimeError("You cannot parameterize this module.")
		if not hasattr(mod,"load"):
			mod.load = md["load"]
			mod.unload = md["unload"]
	
		try:
			mod.load()
		except BaseException as e:
			fix_exception(e)
			try:
				mod.unload()
			except Exception:
				reraise(e)
			else:
				reraise(e)
			# do not use finally: here
		else:
			mod.path = p
	except BaseException:
		if mod is not None and hasattr(mod,"name") and mod.name in Modules:
			del Modules[mod.name]
		raise
	return mod
Пример #27
0
 def _timeout(self):
     self._timer = None
     try:
         self.parent.write(self.val)
     except Exception as ex:
         fix_exception(ex)
         self.q.set(ex)
     else:
         self.q.set(None)
Пример #28
0
	def _timeout(self):
		self._timer = None
		try:
			self.parent.write(self.val)
		except Exception as ex:
			fix_exception(ex)
			self.q.set(ex)
		else:
			self.q.set(None)
Пример #29
0
 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)
Пример #30
0
	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)
Пример #31
0
 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)
Пример #32
0
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)
Пример #33
0
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)
Пример #34
0
	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))
Пример #35
0
 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()
Пример #36
0
	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()
Пример #37
0
 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)
Пример #38
0
	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()
Пример #39
0
		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)
Пример #40
0
	def _slog(self,level,txt):
		while True:
			try:
				self.socket.send((u"<%d>MoaT: %s%s" % (
				                  self.facility | local_levels[LogLevels[level]],
								  txt,
				                  "\0" if not TESTING else "\n")).encode('utf-8'))
			except socket.error as err:
				fix_exception(err)
				if err.args[0] != errno.EINTR:
					raise
			else:
				break
Пример #41
0
	def process(self, event, **k):
		from moat.collect import collections
		super(Shutdown_Collections,self).process(**k)

		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)
Пример #42
0
    def process(self, event, **k):
        from moat.collect import collections
        super(Shutdown_Collections, self).process(**k)

        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)
Пример #43
0
 def _slog(self, level, txt):
     while True:
         try:
             self.socket.send(
                 (u"<%d>MoaT: %s%s" %
                  (self.facility | local_levels[LogLevels[level]], txt,
                   "\0" if not TESTING else "\n")).encode('utf-8'))
         except socket.error as err:
             fix_exception(err)
             if err.args[0] != errno.EINTR:
                 raise
         else:
             break
Пример #44
0
    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)
Пример #45
0
	def _main():
		global ht
		parse_ctx = Context(filename=name)
		#parse_ctx.logger=parse_logger
		try:
			parse(input, interpreter(Context(out=logwrite(logger))),parse_ctx)
		except Exception as e:
			fix_exception(e)
			print_exception(e,file=sys.stderr)
		finally:
			shut_down()
			if ht is not None:
				ht.try_exit()
				ht = None
Пример #46
0
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)
Пример #47
0
 def _main():
     global ht
     parse_ctx = Context(filename=name)
     #parse_ctx.logger=parse_logger
     try:
         parse(input, interpreter(Context(out=logwrite(logger))), parse_ctx)
     except Exception as e:
         fix_exception(e)
         print_exception(e, file=sys.stderr)
     finally:
         shut_down()
         if ht is not None:
             ht.try_exit()
             ht = None
Пример #48
0
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)
Пример #49
0
    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)
Пример #50
0
    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
Пример #51
0
	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)
Пример #52
0
 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()
Пример #53
0
	def run(self,ctx,**k):
		event = self.params(ctx)
		if len(event) < 2:
			raise SyntaxError(u'Usage: set rrd ‹value› ‹name…›')
		s = RRDs[Name(*event[1:])]
		# Using "N:" may run into a RRD bug
		# if we're really close to the next minute
		try:
			rrdtool.update(s.upath, str("-t"),s.udataset, str(now().strftime("%s")+":"+six.text_type(event[0])).encode("utf-8"))
		except Exception as e:
			fix_exception(e)
			if "minimum one second step" in str(e):
				pass
			else:
				raise
Пример #54
0
	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
Пример #55
0
    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))
Пример #56
0
	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()
Пример #57
0
    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