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


Python DaemonContext.files_preserve方法代码示例

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


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

示例1: run

# 需要导入模块: from daemon import DaemonContext [as 别名]
# 或者: from daemon.DaemonContext import files_preserve [as 别名]
 def run(self):
     logger.info("-- starting mandelbrot agent --")
     # execute any privileged subsystem code
     self.privilegedStartService()
     # set the process title
     setproctitle("mandelbrot-agent")
     # construct the daemon context
     daemon = DaemonContext()
     daemon.prevent_core = True
     daemon.chroot_directory = None
     daemon.working_directory = "/"
     if self.pidfile is not None:
         daemon.pidfile = TimeoutPIDLockFile(self.pidfile)
     if os.getuid() == 0:
         daemon.uid = self.uid
         daemon.gid = self.gid
     elif self.uid is not None or self.gid is not None:
         logger.warning("not dropping privileges, process uid is not 0")
     # FIXME: hack to ensure that fds stay open when passed to daemon context
     daemon.files_preserve = [x for x in xrange(64)]
     if self.foreground:
         daemon.detach_process = False
         daemon.stdin = sys.stdin
         daemon.stdout = sys.stdout
         daemon.stderr = sys.stderr
     else:
         daemon.detach_process = True
     with daemon:
         from twisted.internet import reactor
         self.startService()
         reactor.run()
         logger.info("-- stopping mandelbrot agent --")
         self.stopService()
     logger.info("-- stopped mandelbrot agent --")
     return 0
开发者ID:mikefort,项目名称:mandelbrot,代码行数:37,代码来源:agent.py

示例2: handle_daemon

# 需要导入模块: from daemon import DaemonContext [as 别名]
# 或者: from daemon.DaemonContext import files_preserve [as 别名]
    def handle_daemon(self, name, daemon):
        """
        Executes the daemon command.

        :param str name: The name of the daemon.
        :param * daemon: The daemon, i.e. object with main method.
        """
        self.output = EnarkshStyle(self.input, self.output)

        log = logging.getLogger('enarksh')
        log.setLevel(logging.INFO)
        log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

        if self.option('daemonize'):
            config = Config.get()

            log_file_name = os.path.join(C.HOME, config.get_enarksh_log_dir(), name + '.log')
            pid_file_name = os.path.join(C.HOME, config.get_enarksh_lock_dir(), name + '.pid')

            log_handler = logging.handlers.RotatingFileHandler(log_file_name,
                                                               maxBytes=config.get_enarksh_max_log_size(),
                                                               backupCount=config.get_enarksh_log_back())
            log_handler.setLevel(logging.DEBUG)
            log_handler.setFormatter(log_formatter)
            log.addHandler(log_handler)

            output = open(log_file_name, 'ab', 0)

            context = DaemonContext()
            context.working_directory = C.HOME
            context.umask = 0o002
            context.pidfile = PIDLockFile(pid_file_name, False)
            context.stdout = output
            context.stderr = output
            context.files_preserve = [log_handler.stream]

            with context:
                daemon.main()
        else:
            log_handler = logging.StreamHandler(sys.stdout)
            log_handler.setLevel(logging.DEBUG)
            log_handler.setFormatter(log_formatter)
            log.addHandler(log_handler)

            daemon.main()
开发者ID:SetBased,项目名称:py-enarksh,代码行数:47,代码来源:DaemonCommand.py

示例3: run

# 需要导入模块: from daemon import DaemonContext [as 别名]
# 或者: from daemon.DaemonContext import files_preserve [as 别名]
def run():
    if not client_status.is_registered:
        print "Sending info about new client..."
        status, content = api.hi(platform.uname())
        print content
        if status == 200:
            client_status.is_registered = True
            client_status.save()
        else:
            exit("Aborted")

    if os.path.exists(CRASH_PATH):
        crash = os.stat(CRASH_PATH)
        if crash.st_size:
            with open(CRASH_PATH) as f:
                crash_info = f.read()
            status = api.report_crash(crash_info, crash.st_mtime)
            if status == 200:
                log.info("Crash reported")
                os.remove(CRASH_PATH)

    context = DaemonContext(
        pidfile=PIDLockFile(PIDFILE_PATH), signal_map={signal.SIGTERM: on_stop}, stderr=open(CRASH_PATH, "w")
    )
    context.files_preserve = map(lambda h: h.stream, filter(lambda h: isinstance(h, FileHandler), log.logger.handlers))
    print "Starting daemon"
    with context:
        log.info("Daemon started")
        log.info("Build filesystem image")
        basepath = "/"
        root = FSNode(basepath, ignore=IGNORE_PATHS)
        root_d = root.as_dict()
        root_str = json.dumps(root_d)
        h = sha(root_str).hexdigest()
        if not client_status.fshash or client_status.fshash != h:
            status, content = api.set_fs(root_str)
            if status == 200:
                client_status.fshash = h
                client_status.save()
                log.info("Filesystem image updated")
            else:
                log.error("Filesystem image update failed")

        log.info("Create watch manager")
        wm = WatchManager()
        changelog = []
        global notifier
        notifier = ThreadedNotifier(wm, FSEvent(changelog=changelog))
        notifier.start()
        mask = IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO
        log.info("Start watching filesystem changes")
        for item in os.listdir(basepath):
            path = os.path.join(basepath, item)
            if item in IGNORE_PATHS or os.path.islink(path):
                continue
            wm.add_watch(path, mask, rec=True)

        actions = [
            Action(FS_UPLOAD_PERIOD, upload_fs, changelog),
            Action(LOG_UPLOAD_PERIOD, upload_log),
            Action(RESTORE_CHECK_PERIOD, restore),
        ]

        backup_action = lambda: Action(backup.get_next(), make_backup)

        def on_schedule_update(actions=actions):
            actions[-1] = backup_action()

        update_schedule_action = lambda: Action(SCHEDULE_UPDATE_PERIOD, update_schedule, on_update=on_schedule_update)

        if update_schedule() or client_status.schedule:
            actions.append(update_schedule_action())
            actions.append(backup_action())
        else:

            def on_schedule_download(actions=actions):
                actions[-1] = update_schedule_action()
                actions.append(backup_action())

            actions.append(Action(SCHEDULE_UPDATE_PERIOD, update_schedule, on_update=on_schedule_download, on_404=True))

        log.info("Start main loop")
        while True:
            action = min(actions)
            log.info("Next action is %s" % action)
            time.sleep(action.time_left())
            action()
开发者ID:devwaker,项目名称:client,代码行数:89,代码来源:backupd.py


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