コード例 #1
0
    def main(self, args, fd=None, cfgfile=None, syslog=False):
        try:
            with self._cond_var:
                # initialize runtime
                self._signal_handler = Ice.CtrlCHandler()
                self._signal_handler.setCallback(self.interruptCallback)

                init_data = Ice.InitializationData()
                init_data.properties = Ice.createProperties([args[0]])

                if cfgfile:
                    init_data.properties.load(cfgfile)

                args = init_data.properties.parseIceCommandLineOptions(args)

                if syslog:
                    init_data.properties.setProperty("Ice.UseSyslog", "1")


                self._communicator = Ice.initialize([args[0]], init_data)

                Ice.setProcessLogger(self._communicator.getLogger())

                # initialize daemon instance
                self.instance = self.daemon(self._communicator, args)

                # report success
                _report_result(fd, 0, "%d" % os.getpid())

        except Exception:
            return _report_exception(fd)

        try:
            # run the daemon
            status =  self.instance.run()

            #cleanup
            with self._cond_var:
                while self._communicator is not None:
                    self._cond_var.wait()
                self._signal_handler.destroy()
                self._signal_handler = None
                return status
        except Exception:
            return _report_exception()
コード例 #2
0
    def main(self, args: list, config_file: str, init_data_list: list, logger):
        """The main entry point for the Application class.

        :param args:
            The arguments are an argument list (such as sys.argv), 最高优先级
        :param config_file:
            The file path of an Ice configuration file,次优先级
        :param init_data_list:
            InitializationData properties 参数,最低优先级
            [('Ice.Default.Host', '127.0.0.1'), ('Ice.Warn.Connections', '1'), ... ]
        :param logger:
            python 的标准库 logger 对象
        :return:
            This method does not return until after the completion of the run method.
            The return value is an integer representing the exit status.
        """
        if Application._communicator:
            Ice.getProcessLogger().error(
                args[0] +
                ": only one instance of the Application class can be used")
            return 1

        Ice.setProcessLogger(_ApplicationLoggerI(logger))

        #
        # We parse the properties here to extract Ice.ProgramName.
        #
        init_data = self.generate_init_data(config_file, init_data_list, args)

        #
        # Install our handler for the signals we are interested in. We assume main() is called from the main thread.
        #
        if Application._signalPolicy == Application.HandleSignals:
            Application._ctrlCHandler = Ice.CtrlCHandler()

        try:
            Application._interrupted = False
            Application._app_name = \
                init_data.properties.getPropertyWithDefault("Ice.ProgramName", args[0])
            Application._application = self

            #
            # Used by _destroy_on_interrupt_callback and _shutdown_on_interrupt_callback.
            #
            Application._nohup = init_data.properties.getPropertyAsInt(
                "Ice.Nohup") > 0

            #
            # The default is to destroy when a signal is received.
            #
            if Application._signalPolicy == Application.HandleSignals:
                Application.destroy_on_interrupt()

            status = self.do_main(args, init_data)
        except Exception as e:
            Ice.getProcessLogger().error('main loop exception {}\n{}'.format(
                e, traceback.format_exc()))
            status = 1

        #
        # Set _ctrlCHandler to 0 only once communicator.destroy() has completed.
        #
        if Application._signalPolicy == Application.HandleSignals:
            Application._ctrlCHandler.destroy()
            Application._ctrlCHandler = None

        return status