This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
clean shutdown of listener #234
Open
klahnakoski
wants to merge
2
commits into
mozilla-iam:master
Choose a base branch
from
klahnakoski:dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 = { | ||
|
@@ -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) | ||
return jsonify({ | ||
"result": "heartbeat_done", | ||
"status_code": 200, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return port |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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