def _start(name, f, log): import daemonize pidfile = var.pidfile(name) if os.path.exists(pidfile): raise Running(name, pidfile) util.ensure_dir(os.path.dirname(pidfile)) # Save original log level to workaround issue with daemonization # (see note in _run). log_level = log.getEffectiveLevel() daemon = daemonize.Daemonize(app=name, action=lambda: _run(f, log, log_level), pid=pidfile, keep_fds=_log_fds(log)) daemon.start()
def status(name): pidfile = var.pidfile(name) if os.path.exists(pidfile): try: pid = _read_pid(pidfile) except Exception as e: raise PidfileError(pidfile, e) else: try: proc = psutil.Process(pid) except psutil.NoSuchProcess: raise OrphanedProcess(pid, pidfile) else: return Status(proc.is_running(), pid) else: return Status(False)
def stop(name, title=None): log = logging.getLogger("guild") title = title or name pidfile = var.pidfile(name) if not os.path.exists(pidfile): raise NotRunning(name) try: pid = _read_pid(pidfile) except Exception: if log.getEffectiveLevel() <= logging.DEBUG: log.exception("reading %s", pidfile) log.info("%s has an invalid pidfile (%s) - deleting", title, pidfile) util.ensure_deleted(pidfile) else: try: proc = psutil.Process(pid) except psutil.NoSuchProcess: log.info("%s did not shut down cleanly - cleaning up", title) util.ensure_deleted(pidfile) else: log.info("Stopping %s (pid %i)", title, proc.pid) proc.terminate()