Example #1
0
def start_flask_server(daemonize=True, debug=True):
    if Path(PID_PATH).exists():
        pid = int(Path(PID_PATH).read_text())

        if pid in psutil.pids():
            clrc.info(
                "It seems daemon is already running. Use restart command to restart it"
            )
            return
        else:
            clrc.warn(
                "It seems daemon was not stopped correctly the last time. PID file exists, and PID inside it do not match any running process. Please remove PID file manually: {0}"
                .format(PID_PATH))
            clrc.info("After removing PID file daemon should start as usual")
            return

    if daemonize:
        if debug:
            clrc.info("Information:")
            clrc.info("Database is at: {0}".format(DATABASE_PATH))
            clrc.info("Daemon process PID file is at: {0}".format(PID_PATH))
            if LOG_PATH:
                clrc.info(
                    "You specified LOG_PATH. It's at: {0}".format(LOG_PATH))
        if (_can_create_pid_file() and _can_create_logs()):
            clrc.info("Starting daemon...")
            clrc.info(
                "Daemon started successfully. You can access your server at http://{0}:{1}"
                .format(HOST, PORT))
            clrc.info(
                "If you are not able to access the web server and sure this is not a problem with firewall/closed port etc, please check logs here: {0}"
                .format(LOG_PATH))
            logger = logging.getLogger()
            logger.setLevel(logging.DEBUG)
            fh = logging.FileHandler(LOG_PATH)
            logger.addHandler(fh)

            # Fix to work with pyinstaller onefile
            fds_to_myself = []
            if getattr(sys, 'frozen', False):
                fds_to_myself = [
                    of.fd for of in psutil.Process(os.getpid()).open_files()
                    if of.path == sys.executable
                ]

            with daemon.DaemonContext(
                    pidfile=pidlockfile.PIDLockFile(PID_PATH),
                    stdout=fh.stream,
                    stderr=fh.stream,
                    files_preserve=[fh.stream] + fds_to_myself):
                _run_flask_app()

            clrc.success(
                "Daemon started! You can access server at {0}:{1}".format(
                    HOST, PORT))
    else:
        _run_flask_app(debug=True)
Example #2
0
def make_pidlockfile(path):
    """ Make a PIDLockFile instance with the given filesystem path. """
    lockfile = None

    if path is not None:
        if not isinstance(path, basestring):
            error = ValueError("Not a filesystem path: %(path)r" % vars())
            raise error
        if not os.path.isabs(path):
            error = ValueError("Not an absolute path: %(path)r" % vars())
            raise error
        lockfile = pidlockfile.PIDLockFile(path)

    return lockfile
Example #3
0
                               "(default=mongodb://localhost:27017)")
    parser.add_config_argument(
        "--server-host",
        metavar="SERVER_HOST",
        default="localhost",
        help="specify the mongo hostname (default=localhost)")
    parser.add_config_argument("--server-port",
                               metavar="SERVER_PORT",
                               type=int,
                               default=8080,
                               help="specify the mongo port (default=8080)")
    parser.add_argument("command",
                        choices=["start", "stop", "restart"],
                        help="<-- the available actions, choose one")
    args = parser.parse_args()
    pidfile = pidlockfile.PIDLockFile(args.pid)

    if args.command == "start":
        if pidfile.is_locked():
            print("There is already a running process")
            sys.exit(1)

    if args.command == "stop":
        if pidfile.is_locked():
            pid = pidfile.read_pid()
            print("Stopping...")
            os.kill(pid, signal.SIGTERM)
            sys.exit(0)
        else:
            print("There is no running process to stop")
            sys.exit(2)