def __init__(self): env = Environment.getInstance() self.env = env self.__dr = DBusRunner.get_instance() self.__bus = None # Register for resume events zope.event.subscribers.append(self.__handle_events)
def __init__(self): self.log = logging.getLogger(__name__) # Get a dbus proxy and check if theres a service registered called 'org.clacks' # if not, then we can skip all further processing. (The clacks-dbus seems not to be running) self.__dr = DBusRunner.get_instance() self.bus = self.__dr.get_system_bus() self.methods = {}
def __init__(self): env = Environment.getInstance() self.env = env self.log = logging.getLogger(__name__) # Register ourselfs for bus changes on org.clacks dr = DBusRunner.get_instance() self.bus = dr.get_system_bus() self.bus.watch_name_owner("org.clacks", self.__dbus_proxy_monitor)
def __init__(self): env = Environment.getInstance() self.env = env self.log = logging.getLogger(__name__) # Register ourselfs for bus changes on org.clacks dr = DBusRunner.get_instance() self.bus = dr.get_system_bus() self.bus.watch_name_owner("org.clacks", self.__dbus_proxy_monitor) # Create icon cache directory self.spool = env.config.get("client.spool", default="/var/spool/clacks")
def publish(self): """ Start publishing the service. """ dr = DBusRunner.get_instance() bus = dr.get_system_bus() server = dbus.Interface(bus.get_object(avahi.DBUS_NAME, avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER) g = dbus.Interface(bus.get_object(avahi.DBUS_NAME, server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP) g.AddService( avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, self.domain, self.host, dbus.UInt16(self.port), self.text, ) g.Commit() self.group = g
def __init__(self, callback=None): self.__callback = callback self.log = getLogger(__name__) self.__running = False self.__thread = None self.log.info("Initializing network state monitor") # Initialize DBUS dr = DBusRunner.get_instance() self.__bus = dr.get_system_bus() # Register actions to detect the network state self.__upower_actions() self.__network_actions() # Get current state try: proxy = self.__bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager') iface = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties') version = str(iface.Get("org.freedesktop.NetworkManager", "Version")) if tuple(version.split(".")) < ("0", "9"): self.log.warning("network-manager is too old: defaulting to state 'online'") self.__state = True else: # Register actions to detect the network state self.__upower_actions() self.__network_actions() self.__state = iface.Get("org.freedesktop.NetworkManager", "State") in [NM_STATE_CONNECTED_SITE, NM_STATE_CONNECTED_GLOBAL] except: self.log.warning("no network-manager detected: defaulting to state 'online'") self.__state = True
def mainLoop(env): """ Main event loop which will process all registerd threads in a loop. It will run as long env.active is set to True.""" global dr # Enable DBus runner dr = DBusRunner() dr.start() log = logging.getLogger(__name__) try: while True: # Load plugins oreg = ObjectRegistry.getInstance() #@UnusedVariable pr = PluginRegistry() #@UnusedVariable cr = PluginRegistry.getInstance("CommandRegistry") amqp = PluginRegistry.getInstance("AMQPHandler") index = PluginRegistry.getInstance("ObjectIndex") wait = 2 notifyInterval = 10 interval = notifyInterval loadAvg = SystemLoad() # Sleep and slice while True: # Threading doesn't seem to work well with python... for p in env.threads: # Bail out if we're active in the meanwhile if not env.active: break # Check if we reached the notification interval interval += wait if interval > notifyInterval: interval = 0 load = loadAvg.getLoad() latency = 0 workers = len(env.threads) log.debug("load %s, latency %s, workers %s" % (load, latency, workers)) e = EventMaker() status = e.Event( e.NodeStatus( e.Id(env.id), e.Load(str(load)), e.Latency(str(latency)), e.Workers(str(workers)), e.Indexed("true" if index.index_active() else "false"), ) ) amqp.sendEvent(status) # Disable potentially dead nodes cr.updateNodes() p.join(wait) # No break, go to main loop else: continue # Leave the thread loop break # Break, leave main loop if not env.reset_requested: break # Wait for threads to shut down for t in env.threads: t.join(wait) if hasattr(t, 'stop'): t.stop() # Lets do an environment reset now PluginRegistry.shutdown() # Make us active and loop from the beginning env.reset_requested = False env.active = True # Catchall, pylint: disable=W0703 except Exception as detail: log.critical("unexpected error in mainLoop") log.exception(detail) except KeyboardInterrupt: log.info("console requested shutdown") finally: shutdown()
def mainLoop(env): global netstate, dr # Enable DBus runner dr = DBusRunner() dr.start() # Do network monitoring nm = Monitor(netactivity) netstate = nm.is_online() """ Main event loop which will process all registerd threads in a loop. It will run as long env.active is set to True.""" try: log = logging.getLogger(__name__) while True: # Check netstate and wait until we're back online if not netstate: log.info("waiting for network connectivity") while not netstate: time.sleep(1) # Load plugins PluginRegistry(component='client.module') # Sleep and slice wait = 2 while True: # Threading doesn't seem to work well with python... for p in env.threads: # Bail out if we're active in the meanwhile if not env.active: break p.join(wait) # No break, go to main loop else: continue # Break, leave main loop break # Break, leave main loop if not env.reset_requested: break # Wait for threads to shut down for t in env.threads: if hasattr(t, 'stop'): t.stop() if hasattr(t, 'cancel'): t.cancel() t.join(wait) #TODO: remove me if t.isAlive(): try: t._Thread__stop() except: print(str(t.getName()) + ' could not be terminated') # Lets do an environment reset now PluginRegistry.shutdown() # Make us active and loop from the beginning env.reset_requested = False env.active = True if not netstate: log.info("waiting for network connectivity") while not netstate: time.sleep(1) sleep = randint(30, 60) env.log.info("waiting %s seconds to try an AMQP connection recovery" % sleep) time.sleep(sleep) except Exception as detail: log.critical("unexpected error in mainLoop") log.exception(detail) log.debug(traceback.format_exc()) except KeyboardInterrupt: log.info("console requested shutdown") finally: shutdown()