Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Nicer shutdown #228

Open
klahnakoski opened this issue Aug 6, 2020 · 0 comments
Open

Nicer shutdown #228

klahnakoski opened this issue Aug 6, 2020 · 0 comments

Comments

@klahnakoski
Copy link

mozilla-aws-cli ungracefully exits the process when a browser calls /shutdown, which makes it difficult to work with inside a larger application. A clean shutdown (without exit()) would make using mozilla-aws-cli as yet-another-library easier.

Here is some suggested code

@app.route("/shutdown", methods=["GET"])
def handle_shutdown():
    try:
        return Response(b"", 200)
    finally:
        try:
            Log.note("server shutdown")
            server.shutdown()
        except Exception as cause:
            Log.error("can not shutdown listener", cause=cause)


class ServerThread(threading.Thread):

    def __init__(self, app):
        threading.Thread.__init__(self)
        self.srv = make_server('127.0.0.1', port, app)
        self.ctx = app.app_context()
        self.ctx.push()

    def run(self):
        self.srv.serve_forever()

    def shutdown(self):
        self.srv.shutdown()


def listen(login_):
    global server, login

    login = login_
    server = ServerThread(app)
    server.start()

    os.environ["WERKZEUG_RUN_MAIN"] = "true"
    logging.getLogger("werkzeug").setLevel(logging.ERROR)

    return port
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant