def __init__(self): ''' Initialize database and xPL connection ''' XplPlugin.__init__(self, 'dbmgr') MQRep.__init__(self, zmq.Context(), 'dbmgr') self.log.debug("Init database_manager instance") # Check for database connexion self._db = DbHelper() nb_test = 0 db_ok = False while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY: nb_test += 1 try: self._db.list_user_accounts() db_ok = True except: msg = "The database is not responding. Check your configuration of if the database is up. Test %s/%s" % ( nb_test, DATABASE_CONNECTION_NUM_TRY) print(msg) self.log.error(msg) msg = "Waiting for %s seconds" % DATABASE_CONNECTION_WAIT print(msg) self.log.info(msg) time.sleep(DATABASE_CONNECTION_WAIT) if nb_test >= DATABASE_CONNECTION_NUM_TRY: msg = "Exiting dbmgr!" print(msg) self.log.error(msg) self.force_leave() return msg = "Connected to the database" print(msg) self.log.info(msg) try: self._engine = self._db.get_engine() except: self.log.error("Error while starting database engine : %s" % traceback.format_exc()) self.force_leave() return Listener(self._request_config_cb, self.myxpl, { 'schema': 'domogik.config', 'xpltype': 'xpl-cmnd' }) self.enable_hbeat() IOLoop.instance().start()
def __init__(self): ''' Initialize database and xPL connection ''' XplPlugin.__init__(self, 'dbmgr') MQRep.__init__(self, zmq.Context(), 'dbmgr') self.log.debug("Init database_manager instance") # Check for database connexion self._db = DbHelper() nb_test = 0 db_ok = False while not db_ok and nb_test < DATABASE_CONNECTION_NUM_TRY: nb_test += 1 try: self._db.list_user_accounts() db_ok = True except: msg = "The database is not responding. Check your configuration of if the database is up. Test %s/%s" % (nb_test, DATABASE_CONNECTION_NUM_TRY) print(msg) self.log.error(msg) msg = "Waiting for %s seconds" % DATABASE_CONNECTION_WAIT print(msg) self.log.info(msg) time.sleep(DATABASE_CONNECTION_WAIT) if nb_test >= DATABASE_CONNECTION_NUM_TRY: msg = "Exiting dbmgr!" print(msg) self.log.error(msg) self.force_leave() return msg = "Connected to the database" print(msg) self.log.info(msg) try: self._engine = self._db.get_engine() except: self.log.error("Error while starting database engine : %s" % traceback.format_exc()) self.force_leave() return Listener(self._request_config_cb, self.myxpl, {'schema': 'domogik.config', 'xpltype': 'xpl-cmnd'}) self.enable_hbeat() IOLoop.instance().start()
def __init__(self, name, stop_cb = None, is_manager = False, parser = None, daemonize = True, test = False): ''' Create XplPlugin instance, which defines system handlers @param name : The name of the current plugin @param stop_cb : Additionnal method to call when a stop request is received @param is_manager : Must be True if the child script is a Domogik Manager process You should never need to set it to True unless you develop your own manager @param parser : An instance of ArgumentParser. If you want to add extra options to the generic option parser, create your own ArgumentParser instance, use parser.add_argument and then pass your parser instance as parameter. Your options/params will then be available on self.options and self.args @param daemonize : If set to False, force the instance *not* to daemonize, even if '-f' is not passed on the command line. If set to True (default), will check if -f was added. ''' BasePlugin.__init__(self, name, stop_cb, parser, daemonize) Watcher(self) self.log.info(u"----------------------------------") self.log.info(u"Starting plugin '%s' (new manager instance)" % name) self.log.info(u"Python version is {0}".format(sys.version_info)) if self.options.test_option: self.log.info(u"The plugin is starting in TEST mode. Test option is {0}".format(self.options.test_option)) self._name = name self._test = test # flag used to avoid loading json in test mode ''' Calculate the MQ name - For a core component this is just its component name (self._name) - For a plugin this is plugin-<self._name>-self.hostname The reason is that the core components need a fixed name on the mq network, if a plugin starts up it needs to request the config on the network, and it needs to know the worker (core component) to ask the config from. Because of the above reason, every item in the core_component list can only run once ''' if self._name in CORE_COMPONENTS: self._mq_name = self._name else: self._mq_name = "plugin-{0}.{1}".format(self._name, self.get_sanitized_hostname()) # MQ publisher and REP self.zmq = zmq.Context() self._pub = MQPub(self.zmq, self._mq_name) self._set_status(STATUS_STARTING) # MQ : start the thread which sends the status each N seconds thr_send_status = threading.Thread(None, self._send_status_loop, "send_status_loop", (), {}) thr_send_status.start() ### MQ # for stop requests MQRep.__init__(self, self.zmq, self._mq_name) self.helpers = {} self._is_manager = is_manager cfg = Loader('domogik') my_conf = cfg.load() self._config_files = CONFIG_FILE self.config = dict(my_conf[1]) self.libraries_directory = self.config['libraries_path'] self.packages_directory = "{0}/{1}".format(self.config['libraries_path'], PACKAGES_DIR) self.resources_directory = "{0}/{1}".format(self.config['libraries_path'], RESOURCES_DIR) self.products_directory = "{0}/{1}_{2}/{3}".format(self.packages_directory, "plugin", self._name, PRODUCTS_DIR) # Get pid and write it in a file self._pid_dir_path = self.config['pid_dir_path'] self._get_pid() if len(self.get_sanitized_hostname()) > 16: self.log.error(u"You must use 16 char max hostnames ! %s is %s long" % (self.get_sanitized_hostname(), len(self.get_sanitized_hostname()))) self.force_leave() return # Create object which get process informations (cpu, memory, etc) # TODO : activate # TODO : use something else that xPL ????????? #self._process_info = ProcessInfo(os.getpid(), # TIME_BETWEEN_EACH_PROCESS_STATUS, # self._send_process_info, # self.log, # self.myxpl) #self._process_info.start() self.dont_run_ready = False # for all no core elements, load the json # TODO find a way to do it nicer ?? if self._name not in CORE_COMPONENTS and self._test == False: self._load_json() # init an empty devices list self.devices = [] # init an empty 'new' devices list self.new_devices = [] # check for products pictures if self._name not in CORE_COMPONENTS and self._test == False: self.check_for_pictures() # init finished self.log.info(u"End init of the global Plugin part")
def __init__(self, name, stop_cb=None, is_manager=False, parser=None, daemonize=True, test=False): ''' Create XplPlugin instance, which defines system handlers @param name : The name of the current plugin @param stop_cb : Additionnal method to call when a stop request is received @param is_manager : Must be True if the child script is a Domogik Manager process You should never need to set it to True unless you develop your own manager @param parser : An instance of ArgumentParser. If you want to add extra options to the generic option parser, create your own ArgumentParser instance, use parser.add_argument and then pass your parser instance as parameter. Your options/params will then be available on self.options and self.args @param daemonize : If set to False, force the instance *not* to daemonize, even if '-f' is not passed on the command line. If set to True (default), will check if -f was added. ''' BasePlugin.__init__(self, name, stop_cb, parser, daemonize) Watcher(self) self.log.info(u"----------------------------------") self.log.info(u"Starting plugin '%s' (new manager instance)" % name) self.log.info(u"Python version is {0}".format(sys.version_info)) if self.options.test_option: self.log.info( u"The plugin is starting in TEST mode. Test option is {0}". format(self.options.test_option)) self._name = name self._test = test # flag used to avoid loading json in test mode ''' Calculate the MQ name - For a core component this is just its component name (self._name) - For a plugin this is plugin-<self._name>-self.hostname The reason is that the core components need a fixed name on the mq network, if a plugin starts up it needs to request the config on the network, and it needs to know the worker (core component) to ask the config from. Because of the above reason, every item in the core_component list can only run once ''' if self._name in CORE_COMPONENTS: self._mq_name = self._name else: self._mq_name = "plugin-{0}.{1}".format( self._name, self.get_sanitized_hostname()) # MQ publisher and REP self.zmq = zmq.Context() self._pub = MQPub(self.zmq, self._mq_name) self._set_status(STATUS_STARTING) # MQ : start the thread which sends the status each N seconds thr_send_status = threading.Thread(None, self._send_status_loop, "send_status_loop", (), {}) thr_send_status.start() ### MQ # for stop requests MQRep.__init__(self, self.zmq, self._mq_name) self.helpers = {} self._is_manager = is_manager cfg = Loader('domogik') my_conf = cfg.load() self._config_files = CONFIG_FILE self.config = dict(my_conf[1]) self.libraries_directory = self.config['libraries_path'] self.packages_directory = "{0}/{1}".format( self.config['libraries_path'], PACKAGES_DIR) self.resources_directory = "{0}/{1}".format( self.config['libraries_path'], RESOURCES_DIR) self.products_directory = "{0}/{1}_{2}/{3}".format( self.packages_directory, "plugin", self._name, PRODUCTS_DIR) # Get pid and write it in a file self._pid_dir_path = self.config['pid_dir_path'] self._get_pid() if len(self.get_sanitized_hostname()) > 16: self.log.error( u"You must use 16 char max hostnames ! %s is %s long" % (self.get_sanitized_hostname(), len(self.get_sanitized_hostname()))) self.force_leave() return # Create object which get process informations (cpu, memory, etc) # TODO : activate # TODO : use something else that xPL ????????? #self._process_info = ProcessInfo(os.getpid(), # TIME_BETWEEN_EACH_PROCESS_STATUS, # self._send_process_info, # self.log, # self.myxpl) #self._process_info.start() self.dont_run_ready = False # for all no core elements, load the json # TODO find a way to do it nicer ?? if self._name not in CORE_COMPONENTS and self._test == False: self._load_json() # init an empty devices list self.devices = [] # init an empty 'new' devices list self.new_devices = [] # check for products pictures if self._name not in CORE_COMPONENTS and self._test == False: self.check_for_pictures() # init finished self.log.info(u"End init of the global Plugin part")