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

clean shutdown of listener #234

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 45 additions & 21 deletions mozilla_aws_cli/listener.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from __future__ import print_function

import errno
import logging
import os.path
import socket
import threading
import time

from flask import Flask, jsonify, request, send_from_directory
from operator import itemgetter

from .utils import exit_sigint, get_alias
from flask import Flask, jsonify, request, send_from_directory
from werkzeug.serving import make_server

from .utils import get_alias

# These ports must be configured in the IdP's allowed callback URL list
# TODO: Move this to the CLI / config section
Expand All @@ -18,6 +20,8 @@

STATIC_DIR = os.path.join(os.path.dirname(
os.path.realpath(__file__)), "static")
is_done = threading.Event()
server = None
app = Flask(__name__)
logger = logging.getLogger(__name__)
login = {
Expand Down Expand Up @@ -113,17 +117,17 @@ def get_heartbeat():
"status_code": 500,
})

start = time.time()
while time.time() - start < 30:
if login.last_state_check is None:
pass
elif (time.time() - login.last_state_check >
login.max_sleep_no_state_check):
logger.error(
"No response from web interface for {} seconds, shutting "
"down.".format(login.max_sleep_no_state_check))
exit_sigint()
time.sleep(0.5)
# start = time.time()
# while time.time() - start < 30:
# if login.last_state_check is None:
# pass
# elif (time.time() - login.last_state_check >
# login.max_sleep_no_state_check):
# logger.error(
# "No response from web interface for {} seconds, shutting "
# "down.".format(login.max_sleep_no_state_check))
# exit_sigint()
# time.sleep(0.5)
Comment on lines -116 to +130
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will require guidance here: This 2 second response time is too strict on my machine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...plus it seems to delay response for 30 seconds

return jsonify({
"result": "heartbeat_done",
"status_code": 200,
Expand Down Expand Up @@ -217,18 +221,37 @@ def handle_oidc_redirect_callback():
@app.route("/shutdown", methods=["GET"])
def handle_shutdown():
logger.debug("Shutting down Flask")
exit_sigint()

# this is down to prevent race conditions
is_done.set()
threading.Timer(2, server.shutdown).start()
Comment on lines +224 to +225
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call shutdown in a couple of seconds. This could be shorter time, if we know more about how long the shutdown response takes to be delivered.

return jsonify({
"result": "shutdown",
"status_code": 200,
})


def listen(login):
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()
logger.debug("Flask done")

def shutdown(self):
try:
self.srv.shutdown()
except Exception:
pass


def listen(login_):
# set the global callback
globals()["login"] = login
global server, login
login = login_

debug = True if logger.level == logging.DEBUG else False

Expand All @@ -237,6 +260,7 @@ def listen(login):
os.environ["WERKZEUG_RUN_MAIN"] = "true"
logging.getLogger("werkzeug").setLevel(logging.ERROR)

app.run(port=port, debug=debug)

server = ServerThread(app)
server.start()
is_done.wait()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

login() will actually return

return port