def run_as_daemon(loader, interval): """ Given a loader object, start it as a daemon process. """ log.info("The loader will run as a daemon.") # We need to preserve the file descriptor for any log files. rootlogger = logging.getLogger() log_files = [x.stream for x in rootlogger.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() loader.startup() # every <interval> seconds, process the records in the incoming # directory while True: loader.load_all_msgs() time.sleep(interval) except SystemExit, e: log.info("Received the shutdown signal: " + str(e)) loader.shutdown() dc.close()
def open(self): self._setup_logging() DaemonContext.open(self) if self.logger: sys.stdout = FileLikeLogger(self.logger) sys.stderr = FileLikeLogger(self.logger)
def _start(self): if self.conf.logdir is not None: self.access_log = open('{}/{}'.format( self.conf.logdir, 'access.log'), 'ab') sys.stderr.write('Starting server on {}\n'.format(self.url)) #### Daemonize if self.conf.daemonize: assert self.access_log is not None if self.pidlockfile.is_locked(): exit('PID file {} already locked'.format( self.pidlockfile.path)) daemon = DaemonContext( working_directory=os.getcwd(), umask=0o077, pidfile=self.pidlockfile, signal_map={SIGTERM: None}, # let me handle signals stderr=self.access_log) daemon.open() #### Setup logging log_dest = { 'REQUEST': [], 'DEBUG': self.conf.debug_log, 'INFO': self.conf.log, 'ERROR': self.conf.log} if self.conf.logdir is not None: log_dest['INFO'].append([__name__, 'event.log']) if self.conf.request_log is not None: log_dest['REQUEST'].append( [MY_PKG_NAME, self.conf.request_log]) self.loggers = get_loggers(log_dest, logdir=self.conf.logdir, fmt=self.log_fmt) #### Connect to the databases # This has to be done after daemonization because the sockets # may be closed for name, d in self.db_bases.items(): base = d['base'] url = getattr(self.conf, '{}_dburl'.format(name)) session_kargs = d.get('session_args', {}) engine_kargs = d.get('engine_args', {}) cache = d.get('cache', False) DBConnection(base, url, session_kargs=session_kargs, engine_kargs=engine_kargs) if cache: # ETag support self.reqhandler.enable_client_cache(name, base) #### Load users if self.conf.userfile is not None: self.reqhandler.load_users_from_file( self.conf.userfile, plaintext=not self.conf.userfile_hashed) if self._delete_tmp_userfile: os.remove(self.conf.userfile) #### Create the server # This has to be done after daemonization because it binds to # the listening port at creation time srv_cls = HTTPServer if self.conf.multithread: srv_cls = ThreadingHTTPServer self.httpd = srv_cls((self.conf.address, self.conf.port), self.reqhandler) if self.conf.ssl: self.httpd.socket = ssl.wrap_socket( self.httpd.socket, keyfile=self.conf.keyfile, certfile=self.conf.certfile, server_side=True) #### Setup signal handlers signal(SIGTERM, self._term_sighandler) signal(SIGINT, self._term_sighandler) #### Redirect stderr to access.log if self.conf.logdir is not None and not self.conf.daemonize: # running in foreground, but logging to logdir, redirect # stderr to access.log as http.server writes to stderr os.dup2(self.access_log.fileno(), sys.stderr.fileno()) #### Change working directory and run os.chdir(self.conf.webroot) self._log_event('Starting server on {}'.format(self.url)) self.httpd.serve_forever()
class Daemon(object): """ Controller for a callable running in a separate background process. """ start_message = u"{1} started with pid {0}" def __init__(self, app, pidfile=None, stdout=sys.stdout, stderr=sys.stderr, **options): """ Set up the parameters of a new runner. The `app` argument must have the following attributes: * `run`: Callable that will be invoked when the daemon is started. """ self.app = app if pidfile: self.pidfile = make_pidlockfile(pidfile, 5) else: self.pidfile = None self.daemon_context = DaemonContext( stdout=stdout, stderr=stderr, pidfile=self.pidfile, **options ) def start(self): """ Open the daemon context and run the application. """ if self.pidfile and is_pidfile_stale(self.pidfile): self.pidfile.break_lock() try: self.daemon_context.open() except lockfile.AlreadyLocked: raise DaemonRunnerStartFailureError( u"PID file %(pidfile_path)r already locked".format( self.pidfile.path)) pid = os.getpid() message = self.start_message.format(pid, self.app.name) emit_message(message) signal.signal(signal.SIGHUP, self.restart) self.app.run() def stop(self): """ Exit the daemon process specified in the current PID file. """ if not self.pidfile: self.daemon_context.close() if not self.pidfile.is_locked(): raise DaemonRunnerStopFailureError( u"PID file {0} not locked".format(self.pidfile.path)) if is_pidfile_stale(self.pidfile): self.pidfile.break_lock() else: self._terminate_daemon_process() def restart(self, *args): """ Stop, then start. """ self.stop() self.start() def _terminate_daemon_process(self): """ Terminate the daemon process specified in the current PID file. """ pid = self.pidfile.read_pid() try: os.kill(pid, signal.SIGTERM) except OSError, exc: raise DaemonRunnerStopFailureError( u"Failed to terminate {0}: {1}".format(pid, exc))
class DaemonRunner: """ Controller for a callable running in a separate background process. The first command-line argument is the action to take: * 'start': Become a daemon and call `app.run()`. * 'stop': Exit the daemon process specified in the PID file. * 'restart': Stop, then start. """ __metaclass__ = type start_message = "started with pid {pid:d}" def __init__(self, app): """ Set up the parameters of a new runner. :param app: The application instance; see below. :return: ``None``. 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.app = app if 'start' in app.cmd: self.action = 'start' elif 'stop' in app.cmd: self.action = 'stop' elif 'restart' in app.cmd: self.action = 'restart' elif 'status' in app.cmd: self.action = 'status' self.daemon_context = DaemonContext() self.daemon_context.stdin = open(app.stdin_path, 'rt') self.daemon_context.stdout = open(app.stdout_path, 'wb+', buffering=0) self.daemon_context.stderr = open(app.stderr_path, 'wb+', 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 def _start(self): """ Open the daemon context and run the application. :return: ``None``. :raises DaemonRunnerStartFailureError: If the PID file cannot be locked by this process. """ if is_pidfile_stale(self.pidfile): self.pidfile.break_lock() try: self.daemon_context.open() except lockfile.AlreadyLocked: error = DaemonRunnerStartFailureError( "PID file {pidfile.path!r} already locked".format( pidfile=self.pidfile)) raise error pid = os.getpid() message = self.start_message.format(pid=pid) emit_message(message) self.app.run() def _terminate_daemon_process(self): """ Terminate the daemon process specified in the current PID file. :return: ``None``. :raises DaemonRunnerStopFailureError: If terminating the daemon fails with an OS error. """ pid = self.pidfile.read_pid() try: os.kill(pid, signal.SIGTERM) except OSError as exc: error = DaemonRunnerStopFailureError( "Failed to terminate {pid:d}: {exc}".format(pid=pid, exc=exc)) raise error def _status(self): """ Print the daemon process status to stdout. :return: ``None``. """ if is_pidfile_stale(self.pidfile): emit_message('rephone not runnig, but PID File is stale.') elif self.pidfile.is_locked(): emit_message('rephone is running PID {pid:d}'.format( pid=self.pidfile.read_pid())) else: emit_message('rephone is not running') def _stop(self): """ Exit the daemon process specified in the current PID file. :return: ``None``. :raises DaemonRunnerStopFailureError: If the PID file is not already locked. """ if not self.pidfile.is_locked(): emit_message('no PID file found') exit(0) if is_pidfile_stale(self.pidfile): self.pidfile.break_lock() else: self._terminate_daemon_process() def _restart(self): """ Stop, then start. """ self._stop() self._start() action_funcs = { 'start': _start, 'stop': _stop, 'restart': _restart, 'status': _status } def _get_action_func(self): """ Get the function for the specified action. :return: The function object corresponding to the specified action. :raises DaemonRunnerInvalidActionError: if the action is unknown. The action is specified by the `action` attribute, which is set during `parse_args`. """ try: func = self.action_funcs[self.action] except KeyError: error = DaemonRunnerInvalidActionError( "Unknown action: {action!r}".format(action=self.action)) raise error return func def do_action(self): """ Perform the requested action. :return: ``None``. The action is specified by the `action` attribute, which is set during `parse_args`. """ func = self._get_action_func() func(self)
class DaemonRunner: """ Controller for a callable running in a separate background process. The first command-line argument is the action to take: * 'start': Become a daemon and call `app.run()`. * 'stop': Exit the daemon process specified in the PID file. * 'restart': Stop, then start. """ __metaclass__ = type start_message = "started with pid {pid:d}" def __init__(self, app): """ Set up the parameters of a new runner. :param app: The application instance; see below. :return: ``None``. 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.app = app if 'start' in app.cmd: self.action = 'start' elif 'stop' in app.cmd: self.action = 'stop' elif 'restart' in app.cmd: self.action = 'restart' elif 'status' in app.cmd: self.action = 'status' self.daemon_context = DaemonContext() self.daemon_context.stdin = open(app.stdin_path, 'rt') self.daemon_context.stdout = open(app.stdout_path, 'wb+', buffering=0) self.daemon_context.stderr = open(app.stderr_path, 'wb+', 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 def _start(self): """ Open the daemon context and run the application. :return: ``None``. :raises DaemonRunnerStartFailureError: If the PID file cannot be locked by this process. """ if is_pidfile_stale(self.pidfile): self.pidfile.break_lock() try: self.daemon_context.open() except lockfile.AlreadyLocked: error = DaemonRunnerStartFailureError("PID file {pidfile.path!r} already locked".format(pidfile=self.pidfile)) raise error pid = os.getpid() message = self.start_message.format(pid=pid) emit_message(message) self.app.run() def _terminate_daemon_process(self): """ Terminate the daemon process specified in the current PID file. :return: ``None``. :raises DaemonRunnerStopFailureError: If terminating the daemon fails with an OS error. """ pid = self.pidfile.read_pid() try: os.kill(pid, signal.SIGTERM) except OSError as exc: error = DaemonRunnerStopFailureError("Failed to terminate {pid:d}: {exc}".format(pid=pid, exc=exc)) raise error def _status(self): """ Print the daemon process status to stdout. :return: ``None``. """ if is_pidfile_stale(self.pidfile): emit_message('rephone not runnig, but PID File is stale.') elif self.pidfile.is_locked(): emit_message('rephone is running PID {pid:d}'.format(pid=self.pidfile.read_pid())) else: emit_message('rephone is not running') def _stop(self): """ Exit the daemon process specified in the current PID file. :return: ``None``. :raises DaemonRunnerStopFailureError: If the PID file is not already locked. """ if not self.pidfile.is_locked(): emit_message('no PID file found') exit(0) if is_pidfile_stale(self.pidfile): self.pidfile.break_lock() else: self._terminate_daemon_process() def _restart(self): """ Stop, then start. """ self._stop() self._start() action_funcs = {'start': _start, 'stop': _stop, 'restart': _restart, 'status': _status} def _get_action_func(self): """ Get the function for the specified action. :return: The function object corresponding to the specified action. :raises DaemonRunnerInvalidActionError: if the action is unknown. The action is specified by the `action` attribute, which is set during `parse_args`. """ try: func = self.action_funcs[self.action] except KeyError: error = DaemonRunnerInvalidActionError("Unknown action: {action!r}".format(action=self.action)) raise error return func def do_action(self): """ Perform the requested action. :return: ``None``. The action is specified by the `action` attribute, which is set during `parse_args`. """ func = self._get_action_func() func(self)