Skip to content

Commit

Permalink
Allow user to kill editor process from browser (#135)
Browse files Browse the repository at this point in the history
* Check for terminal

* Added kill functionality in local server, but it doesn't work.

* Made it work.

* Moved button, made it less prominent.

* whoops
  • Loading branch information
rahularya50 authored and kavigupta committed Apr 6, 2019
1 parent ed8b20c commit d04f807
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
24 changes: 21 additions & 3 deletions editor/local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def handle_post_thread(self, data, path):
if b"code[]" not in data:
data[b"code[]"] = [b""]


if path == "/cancel":
self.cancellation_event.set()
self.send_response(HTTPStatus.OK, 'test')
Expand Down Expand Up @@ -166,6 +165,11 @@ def handle_post_thread(self, data, path):
query = data.get(b"query", [b""])[0].decode("utf-8")
self.wfile.write(bytes(json.dumps(search(query)), "utf-8"))

elif path == "/kill":
# This is (only) fine because we're in a different thread than the actual server
self.server.shutdown()
self.server.socket.close()

def do_GET(self):
self.send_response(HTTPStatus.OK, 'test')
path = "editor/static/" + urllib.parse.unquote(self.path)[1:]
Expand Down Expand Up @@ -284,9 +288,23 @@ def start(file_args, port, open_browser):
main_files = file_args
global PORT
PORT = port
print(f"http://localhost:{PORT}")
url = f"http://localhost:{PORT}"
socketserver.TCPServer.allow_reuse_address = True
httpd = ThreadedHTTPServer(("localhost", PORT), Handler)
try:
httpd = ThreadedHTTPServer(("localhost", PORT), Handler)
except OSError:
if supports_color():
print("\033[91m", end="")
print(f"Port {PORT} is already in use, likely for another instance of the editor.")
print("To open a second instance of the editor, specify a different port using --port.")
print(f"To replace the previous editor instance with a new one:\n"
f" 1. Go to {url}\n"
f" 2. Press \"Stop Editor\" at the top\n"
f" 3. Run `python3 editor` again")
if supports_color():
print("\033[0m", end="")
return
print(url)
if open_browser:
webbrowser.open(f"http://localhost:{PORT}", new=0, autoraise=True)
try:
Expand Down
9 changes: 7 additions & 2 deletions editor/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<a class="nav-link" href="#" id="settings-btn">Settings</a>
</li>
</ul>
<button type="button" class="btn btn-outline-danger" data-toggle="tooltip" id="stop-editor-btn"
title="Stop the current editor process"
style="margin-right: 15px">Stop Editor
</button>
<br>
<form class="documentation-form form-inline my-2 my-lg-0">
<input id="documentation-search" class="form-control mr-sm-2" type="search" aria-label="Search"
placeholder="Search">
Expand Down Expand Up @@ -201,7 +206,7 @@ <h5 class="modal-title" id="disconnectedModalLabel">Process Disconnected</h5>
<p>
Warning! The <code>editor</code> process is no longer responding to queries from the front end. <b>You have likely stopped the process in the terminal.</b>
<p>
Run <code>python editor -nb</code> in a terminal, then reconnect.
Run <code>python editor -nb</code> in a terminal, then refresh / reconnect.
</p>
<p>
Remember to run <code>python ok</code> (to unlock or submit tests) in a <b>separate</b> terminal window, so that you don't have to stop the editor process.
Expand All @@ -218,7 +223,7 @@ <h5 class="modal-title" id="disconnectedModalLabel">Process Disconnected</h5>
</div>

<div class="modal fade" id="formatFailModal" tabindex="-1" role="dialog"
aria-labelledby="disconnectedModalLabel" aria-hidden="true">
aria-labelledby="formatFailModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
Expand Down
6 changes: 6 additions & 0 deletions editor/static/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function init() {
$('#settingsModal').on('hide.bs.modal', function (e) {
saveState();
});
$("#stop-editor-btn").on("click", () => {
$.post("/kill");
$("#disconnectedModal").modal("show");
$("#reconnect-button").hide();
});

$.post("./load_settings").done((data) => {
setAllSettings($.parseJSON(data));
});
Expand Down

0 comments on commit d04f807

Please sign in to comment.