Skip to content

Commit

Permalink
Merge pull request #692 from NextCenturyCorporation/MCS-1817-closeUni…
Browse files Browse the repository at this point in the history
…tyWithBrowser

MCS-1817 Close unity process when browser closes
  • Loading branch information
rartiss55 authored Sep 7, 2023
2 parents 66534c3 + ef40bfb commit dee7f72
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ opencv-python==4.5.4.60; python_version>="3.10"
pep8-naming==0.13.3
pre-commit==2.21.0; python_version<"3.8"
pre-commit==3.2.2; python_version>="3.8"
psutil==5.9.5
pydantic==1.10.7
recommonmark==0.7.1
requests==2.31.0
Expand Down
3 changes: 3 additions & 0 deletions webenabled/mcs_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ def get_goal_info(self, scene_filename):
self.logger.exception("Exception in reading goal from json file")
return {}

def get_controller_pid(self):
return self.pid

def simplify_action_list(self, default_action_list):
"""The action list looks something like:
[('CloseObject', {}), ('DropObject', {}), ('MoveAhead', {}), ...
Expand Down
43 changes: 43 additions & 0 deletions webenabled/mcsweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import string

import psutil
from flask import (Flask, jsonify, make_response, render_template, request,
session)
# See: https://www.geeksforgeeks.org/how-to-use-flask-session-in-python-flask/
Expand Down Expand Up @@ -131,6 +132,48 @@ def handle_keypress():
return resp


@app.route("/exit_unity", methods=["POST"])
def exit_unity():
app.logger.info("=" * 30)
mcs_interface, unique_id = get_mcs_interface(request, "Exit Unity")
if mcs_interface is None:
app.logger.warn("Cannot load MCS interface")
return

controller_pid = mcs_interface.get_controller_pid()

app.logger.info(
"Attempting to clean up processes after browser has been closed.")

for p in psutil.process_iter(['pid']):
if p.info['pid'] == controller_pid:
children = p.children(recursive=True)
for c_process in children:
app.logger.info(
f"Found child process of controller: {c_process}, "
f"will attempt to end.")
c_process.kill()

app.logger.info(
f"Found controller process: {p}, will attempt to end.")
p.kill()

if (unique_id is None):
unique_id = request.cookies.get("uniq_id")

app.logger.info(
f"Clear user session for: {unique_id}")
del session[unique_id]

resp = jsonify(
ended_controller_process=controller_pid,
ended_session=unique_id
)
resp.delete_cookie('uniq_id')

return resp


@app.route("/scene_selection", methods=["POST"])
def handle_scene_selection():
app.logger.info("=" * 30)
Expand Down
1 change: 1 addition & 0 deletions webenabled/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ flake8==5.0.4; python_version<"3.8"
flake8==6.0.0; python_version>="3.8"
isort==5.11.5; python_version<"3.8"
isort==5.12.0; python_version>="3.8"
psutil==5.9.5
pytest==7.3.0
9 changes: 9 additions & 0 deletions webenabled/templates/mcs_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ <h1>Machine Common Sense</h1>
updateImageCoords(e)
}


function cleanup(e) {
resp = fetch("{{url_for('exit_unity')}}",
{method: 'POST', body: JSON.stringify(this.innerHTML)})
.then(parseJsonResponse)
}

window.addEventListener('beforeunload', this.cleanup, false);

// ---------------------------------------------------------
// Handle Key strokes (navigation)
window.addEventListener('keydown', this.process_key, false);
Expand Down

0 comments on commit dee7f72

Please sign in to comment.