Beispiel #1
0
    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
        detach_process = None
        try:
            detach_process = app.detach_process
        except AttributeError:
            pass
        self.daemon_context = DaemonContext(detach_process=detach_process)
        if hasattr(app, 'stdin_path'):
            self.daemon_context.stdin = open(app.stdin_path, 'r')
        else:
            self.daemon_context.stdin = sys.stdin

        if hasattr(app, 'stdout_path'):
            self.daemon_context.stdout = open(app.stdout_path, 'w+')
        else:
            self.daemon_context.stdout = sys.stdout

        if hasattr(app, 'stderr_path'):
            self.daemon_context.stderr = open(app.stderr_path,
                                              'w+',
                                              buffering=0)
        else:
            self.daemon_context.stderr = sys.stderr

        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