当前位置: 首页>>代码示例>>Python>>正文


Python daemon.DaemonContext类代码示例

本文整理汇总了Python中daemon.DaemonContext的典型用法代码示例。如果您正苦于以下问题:Python DaemonContext类的具体用法?Python DaemonContext怎么用?Python DaemonContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DaemonContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: open

    def open(self):
        # Call super
        DaemonContext.open(self)

        # Needfuls.doit()
        self.logger.info('Initializing...')

        # RPC connection
        self.connection = Connection(self.config['rpchost'])

        self.logger.info('Loading plugins...')
        # Import and create plugin objects
        self.plugins = [
            plugin(self.connection, self.config, self.logger.handler)
            for plugin in [
                getattr(
                    __import__(
                        'rpcdaemon.plugins.' + module.lower(),
                        fromlist=[module]
                    ),
                    module)
                for module in self.config['plugins'].split()
            ]
        ]

        # Setup worker with plugins and crank it up
        self.logger.info('Starting worker...')
        self.worker = Worker(self.connection, self.plugins,
                             handler=self.logger.handler)
        self.worker.daemon = True  # Daemon thread
        self.worker.start()
        self.logger.info('Started.')
开发者ID:Apsu,项目名称:rpcdaemon,代码行数:32,代码来源:__init__.py

示例2: daemonise

def daemonise():
    from daemon import DaemonContext
    from pidfile import PidFile
    daemon = DaemonContext(pidfile=PidFile("/var/run/doorbot.pid"))
    daemon.open()

    logging.info('Daemonised doorbot')
开发者ID:nuaays,项目名称:Doorbot,代码行数:7,代码来源:doorbot.py

示例3: transfer

def transfer(app, transfer_job_id):
    transfer_job = app.get_transfer_job(transfer_job_id)
    if transfer_job is None:
        log.error('Invalid transfer job ID: %s' % transfer_job_id)
        return False
    port_range = app.config.get('app:main', 'transfer_worker_port_range')
    try:
        port_range = [int(p) for p in port_range.split('-')]
    except Exception as e:
        log.error('Invalid port range set in transfer_worker_port_range: %s: %s' % (port_range, str(e)))
        return False
    protocol = transfer_job.params['protocol']
    if protocol not in ('http', 'https', 'scp'):
        log.error('Unsupported protocol: %s' % protocol)
        return False
    state_result = StateResult(result=dict(state=transfer_job.states.RUNNING, info='Transfer process starting up.'))
    listener_server = ListenerServer(range(port_range[0], port_range[1] + 1), ListenerRequestHandler, app, transfer_job, state_result)
    # daemonize here (if desired)
    if not debug:
        daemon_context = DaemonContext(files_preserve=[listener_server.fileno()], working_directory=os.getcwd())
        daemon_context.open()
        # If this fails, it'll never be detected.  Hopefully it won't fail since it succeeded once.
        app.connect_database()  # daemon closed the database fd
        transfer_job = app.get_transfer_job(transfer_job_id)
    listener_thread = threading.Thread(target=listener_server.serve_forever)
    listener_thread.setDaemon(True)
    listener_thread.start()
    # Store this process' pid so unhandled deaths can be handled by the restarter
    transfer_job.pid = os.getpid()
    app.sa_session.add(transfer_job)
    app.sa_session.flush()
    terminal_state = None
    if protocol in ['http', 'https']:
        for transfer_result_dict in http_transfer(transfer_job):
            state_result.result = transfer_result_dict
            if transfer_result_dict['state'] in transfer_job.terminal_states:
                terminal_state = transfer_result_dict
    elif protocol in ['scp']:
        # Transfer the file using scp
        transfer_result_dict = scp_transfer(transfer_job)
        # Handle the state of the transfer
        state = transfer_result_dict['state']
        state_result.result = transfer_result_dict
        if state in transfer_job.terminal_states:
            terminal_state = transfer_result_dict
    if terminal_state is not None:
        transfer_job.state = terminal_state['state']
        for name in ['info', 'path']:
            if name in terminal_state:
                transfer_job.__setattr__(name, terminal_state[name])
    else:
        transfer_job.state = transfer_job.states.ERROR
        transfer_job.info = 'Unknown error encountered by transfer worker.'
    app.sa_session.add(transfer_job)
    app.sa_session.flush()
    return True
开发者ID:bwlang,项目名称:galaxy,代码行数:56,代码来源:transfer.py

示例4: actionStartFCGI

    def actionStartFCGI(self):
        from flup.server.fcgi import WSGIServer
        from daemon import DaemonContext
        from lockfile import FileLock
        from os import getpid

        ctx = DaemonContext(working_directory=".", pidfile=FileLock("/tmp/wl-fcgi"))
        ctx.stderr = open("error.log", "w+")
        with ctx:
            open("wl-fcgi.pid", "w").write(str(getpid()))
            WSGIServer(app, bindAddress="fcgi.sock", umask=0000).run()
开发者ID:SpicyHorse,项目名称:delivery,代码行数:11,代码来源:manage.py

示例5: close

    def close(self):
        # We might get called more than once, or before worker exists
        if self.is_open and self.worker and self.worker.is_alive():
            self.logger.info('Stopping worker...')
            self.worker.should_stop = True
            self.worker.join(5)  # Wait up to 5 seconds
            if self.worker.is_alive():
                self.logger.warn(
                    'Error stopping worker. Shutting down uncleanly.'
                )
            self.logger.info('Stopped.')

        DaemonContext.close(self)
开发者ID:Apsu,项目名称:rpcdaemon,代码行数:13,代码来源:__init__.py

示例6: open

    def open( self ):

        self.files_preserve =\
            list( tuple(self.files_preserve) + tuple( logger.handler.stream for logger in self.loggers ) )
        _DaemonContext.open( self )
        gevent.reinit()

        ## not reliable w/ gevent, unfortunateley .. use stderr redirect instead
        #log = logging.getLogger('UNHANDLED')
        #sys.excepthook = lambda tp, value, tb:\
        #    log.error( ''.join( traceback.format_exception( tp, value, tb ) ) )

        gevent.signal(signal.SIGTERM, self.run_exit_hooks, signal.SIGTERM, None )
开发者ID:doncatnip,项目名称:csgi,代码行数:13,代码来源:daemonize.py

示例7: _daemonize

    def _daemonize(self):
        from daemon import DaemonContext
        try:
            from daemon.pidfile import TimeoutPIDLockFile as PIDLockFile
        except:
            from daemon.pidlockfile import PIDLockFile

        pidlock = PIDLockFile('/var/run/fedmsg/%s.pid' % self.name)
        output = file('/var/log/fedmsg/%s.log' % self.name, 'a')
        daemon = DaemonContext(pidfile=pidlock, stdout=output, stderr=output)
        daemon.terminate = self._handle_signal

        with daemon:
            return self.run()
开发者ID:HousewifeHacker,项目名称:fedmsg,代码行数:14,代码来源:__init__.py

示例8: _daemonize

    def _daemonize(self, func, config):
        from daemon import DaemonContext

        try:
            from daemon.pidfile import TimeoutPIDLockFile as PIDLockFile
        except:
            from daemon.pidlockfile import PIDLockFile

        pidlock = PIDLockFile("/var/run/fedmsg/%s.pid" % self.name)
        output = file("/var/log/fedmsg/%s.log" % self.name, "a")
        daemon = DaemonContext(pidfile=pidlock, stdout=output, stderr=output)
        daemon.terminate = self._handle_signal

        with daemon:
            return func(**config)
开发者ID:silverrain,项目名称:fedmsg,代码行数:15,代码来源:__init__.py

示例9: __init__

    def __init__(self, app):
        """ Set up the parameters of a new runner.

            The `app` argument must have the following attributes:

            * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem
              paths to open and replace the existing `sys.stdin`,
              `sys.stdout`, `sys.stderr`.

            * `pidfile_path`: Absolute filesystem path to a file that
              will be used as the PID file for the daemon. If
              ``None``, no PID file will be used.

            * `pidfile_timeout`: Used as the default acquisition
              timeout value supplied to the runner's PID lock file.

            * `run`: Callable that will be invoked when the daemon is
              started.
            
            """
        self.parse_args()
        self.app = app
        self.daemon_context = DaemonContext()
        self.daemon_context.stdin = open(app.stdin_path, 'r')
        self.daemon_context.stdout = open(app.stdout_path, 'w+')
        self.daemon_context.stderr = open(
            app.stderr_path, 'w+', buffering=0)

        self.pidfile = None
        if app.pidfile_path is not None:
            self.pidfile = make_pidlockfile(
                app.pidfile_path, app.pidfile_timeout)
        self.daemon_context.pidfile = self.pidfile
开发者ID:WirelessThings,项目名称:WirelessThings-LaunchPad,代码行数:33,代码来源:runner.py

示例10: __init__

    def __init__(self, app):
        """ Set up the parameters of a new runner.

            The `app` argument must have the following attributes:

            * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem
              paths to open and replace the existing `sys.stdin`,
              `sys.stdout`, `sys.stderr`.

            * `pidfile_path`: Filesystem path to a file that will be
              used as the PID file for the daemon.

            * `run`: Callable that will be invoked when the daemon is
              started.
            
            """
        self.parse_args()
        self.app = app
        self.daemon_context = DaemonContext()
        self.daemon_context.stdin = open(app.stdin_path, 'r')
        self.daemon_context.stdout = open(app.stdout_path, 'w+')
        self.daemon_context.stderr = open(
            app.stderr_path, 'w+', buffering=0)

        self.pidfile = make_pidlockfile(app.pidfile_path)
        self.daemon_context.pidfile = self.pidfile
开发者ID:DhruvRelwani,项目名称:GamesmanClassic,代码行数:26,代码来源:runner.py

示例11: start_server

def start_server(config):
    weblogger = logging.getLogger('plight_httpd')
    weblogger.setLevel(config['web_log_level'])
    if weblogger.handlers == []:
        weblogging_handler = RotatingFileHandler(config['web_log_file'],
                                              mode='a',
                                              maxBytes=config['web_log_filesize'],
                                              backupCount=config['web_log_rotation_count'])
        weblogger.addHandler(weblogging_handler)

    applogger = logging.getLogger('plight')
    applogger.setLevel(config['log_level'])
    if applogger.handlers == []:
        applogging_handler = RotatingFileHandler(config['log_file'],
                                              mode='a',
                                              maxBytes=config['log_filesize'],
                                              backupCount=config['log_rotation_count'])
        applogger.addHandler(applogging_handler)

    pidfile = PIDLockFile(PID_FILE)
    context = DaemonContext(pidfile=pidfile,
                            uid=pwd.getpwnam(config['user']).pw_uid,
                            gid=grp.getgrnam(config['group']).gr_gid,
                            files_preserve = [
                                weblogging_handler.stream,
                                applogging_handler.stream,
                            ],)
    context.stdout = applogging_handler.stream
    context.stderr = applogging_handler.stream
    context.open()
    os.umask(0022)

    try:
        try:
            log_message('Plight is starting...')
            node_status = plight.NodeStatus(config['state_file'])
            server_class = BaseHTTPServer.HTTPServer
            http = server_class((config['host'],
                                 config['port']),
                                 plight.StatusHTTPRequestHandler)
            http.serve_forever()
        except SystemExit, sysexit:
            log_message("Stopping... " + str(sysexit))
        except Exception, ex:
            log_message("ERROR: " + str(ex))
开发者ID:nickbales,项目名称:plight,代码行数:45,代码来源:util.py

示例12: run_as_daemon

def run_as_daemon(ssm_inst):
    """
    Given an SSM object, start it as a daemon process.
    """
    log.info("The SSM will run as a daemon.")
    # We need to preserve the file descriptor for any log files.
    log_files = [x.stream for x in log.handlers]
    dc = DaemonContext(files_preserve=log_files)
    
    try:
        # Note: because we need to be compatible with python 2.4, we can't use
        # with dc:
        # here - we need to call the open() and close() methods 
        # manually.
        dc.open()
        try:
            ssm_inst.startup()
        except Exception, err:
            print err
            print type(err)
            print dir(err)
            log.info("SSM failed to start: " + str(err))
            raise
        
        # Only an exception will break this loop.
        # A SystemExit exception will be raised if the process is killed.
        while True:
            
            if ssm_inst.is_dead():
                raise ssm_inst.get_death_exception()
                
            # Process all the messages one at a time before continuing
            try:
                while ssm_inst.process_outgoing():
                    pass
            except ssm.SsmException, err:
                # SsmException if the message is rejected by the consumer.
                # We can wait and try again.
                log.error('Error in message processing: '+str(err))
            except EncryptException, err:
                # EncryptException if something went wrong trying to encrypt
                # or sign. Give up.
                log.error("Failed to encrypt or sign:" + str(err))
                raise
开发者ID:djw8605,项目名称:Gratia,代码行数:44,代码来源:ssm_master.py

示例13: __init__

    def __init__(self, redirect_output = '/tmp/ocsp_responder.log',
                 chroot = None,
                 detach = True,
                 pidfile = '/tmp/ocsp_responder.pid'):

        self.redirect_output = file(redirect_output, 'a')
        self.chroot = chroot
        self.detach = detach
        self.pidfile = pidfile

        if self.pidfile:
            self.pidfile = LockFile(pidfile)
        if not self.detach:
            self.redirect_output = sys.stdout

        DaemonContext.__init__(self,
                               stdout = self.redirect_output,
                               stderr = self.redirect_output,
                               detach_process = self.detach,
                               pidfile = self.pidfile)
开发者ID:boohyung,项目名称:PyOCSP,代码行数:20,代码来源:daemonize.py

示例14: _daemonize

    def _daemonize(self):
        import psutil
        from daemon import DaemonContext
        try:
            from daemon.pidfile import TimeoutPIDLockFile as PIDLockFile
        except:
            from daemon.pidlockfile import PIDLockFile

        pidlock = PIDLockFile('/var/run/fedmsg/%s.pid' % self.name)

        pid = pidlock.read_pid()
        if pid and not psutil.pid_exists(pid):
            self.log.warn("PID file exists but with no proc:  coup d'etat!")
            pidlock.break_lock()

        output = file('/var/log/fedmsg/%s.log' % self.name, 'a')
        daemon = DaemonContext(pidfile=pidlock, stdout=output, stderr=output)
        daemon.terminate = self._handle_signal

        with daemon:
            return self.run()
开发者ID:AdamWill,项目名称:fedmsg,代码行数:21,代码来源:__init__.py

示例15: __init__

    def __init__(self, args=None):
        # Parse args
        if args is None:
            args = {}

        options, _ = getopt.getopt(sys.argv[1:], 'c:d')
        options = dict(options)

        config_file = options.get('-c', '/usr/local/etc/rpcdaemon.conf')
        daemonize = '-d' not in options

        # Parse config
        self.config = Config(config_file, 'Daemon')

        # Initialize logger
        self.logger = Logger(
            name='rpcdaemon',
            level = self.config['loglevel'],
            path = self.config['logfile'] if daemonize else None,
            handler = None if daemonize else logging.StreamHandler()
        )

        self.pidfile = PIDFile(self.config['pidfile']) if daemonize else None;

        # TOOD: plugin.check thread pool?
        self.timeout = int(self.config.get('check_interval', 1))

        # Clamp in case we exit before worker exists
        self.worker = None

        # Initialize daemon
        DaemonContext.__init__(
            self,
            detach_process=daemonize,
            files_preserve=[self.logger.handler.stream.fileno()],
            pidfile=self.pidfile,
            stdout=self.logger.handler.stream,
            stderr=self.logger.handler.stream
        )
开发者ID:Apsu,项目名称:rpcdaemon,代码行数:39,代码来源:__init__.py


注:本文中的daemon.DaemonContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。