diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-11-09 23:38:28 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-11-09 23:38:28 +0800 |
commit | 20a0b986c84ccddd5e71e278e2d0b9be81f60f50 (patch) | |
tree | 38dcd910ee868a17250ab77368587527f835ec1e | |
parent | e5a05c8e3c77aa292b7dd9bc4a2682148be873ac (diff) | |
download | fg21sim-20a0b986c84ccddd5e71e278e2d0b9be81f60f50.tar.bz2 |
webui: Add the foreground simulation task to ConsoleHandler
* The original sleep test task "_task()" renamed to "_task_test()"
* Also add the new "startServerTaskTest()" to trigger the "_task_test()"
NOTE/XXX:
The foregrounds simulation requires the configurations, which is
currently obtained from "self.websocket.configs", which I think is a
HACK. How to better solve this??
-rw-r--r-- | fg21sim/webui/consolehandler.py | 78 | ||||
-rw-r--r-- | fg21sim/webui/static/js/console.js | 13 |
2 files changed, 89 insertions, 2 deletions
diff --git a/fg21sim/webui/consolehandler.py b/fg21sim/webui/consolehandler.py index 5c7da00..54d4305 100644 --- a/fg21sim/webui/consolehandler.py +++ b/fg21sim/webui/consolehandler.py @@ -79,6 +79,12 @@ class ConsoleHandler: "type: {0}, action: {1}".format(msg_type, msg_action)) if msg_action == "start": # FIXME/XXX: This task should be asynchronous! + success, error = self._start() + response["success"] = success + if not success: + response["error"] = error + elif msg_action == "start_test": + # FIXME/XXX: This task should be asynchronous! success, error = self._start(msg["time"]) response["success"] = success if not success: @@ -103,6 +109,35 @@ class ConsoleHandler: # FIXME/XXX: # * How to call this task asynchronously ?? + def _start_test(self, *args, **kwargs): + """ + Start the task by submitting it to the executor + + Returns + ------- + success : bool + Whether success without any errors + error : str + Detail of the error if not succeed + + """ + if self.onetask_only and self.status["running"]: + logger.warning("Task already running, and only one task allowed") + success = False + error = "already running and only one task allowed" + else: + logger.info("Start the task on the executor ...") + self.status["running"] = True + self.status["finished"] = False + # Also push the logging messages to the client + self._add_wsloghandler() + future = self.executor.submit(self._task_test, *args, **kwargs) + self.io_loop.add_future(future, self._task_callback) + success, error = future.result() + return (success, error) + + # FIXME/XXX: + # * How to call this task asynchronously ?? def _start(self, *args, **kwargs): """ Start the task by submitting it to the executor @@ -170,7 +205,7 @@ class ConsoleHandler: msg_response = json.dumps(response) self.websocket.write_message(msg_response) - def _task(self, *args, **kwargs): + def _task_test(self, *args, **kwargs): """ The task this console to manage. @@ -197,3 +232,44 @@ class ConsoleHandler: time.sleep(1) logger.info("console task: DONE!") return (True, None) + + def _task(self, *args, **kwargs): + """ + The task this console to manage. + Perform the foregrounds simulations. + + Returns + ------- + success : bool + Whether success without any errors + error : str + Detail of the error if not succeed + + NOTE + ---- + The task is synchronous and may be computationally intensive + (i.e., CPU-bound rather than IO/event-bound), therefore, + threads (or processes) are required to make it non-blocking + (i.e., asynchronous). + + Credit: https://stackoverflow.com/a/32164711/4856091 + """ + logger.info("Preparing to start foregrounds simulations ...") + logger.info("Importing modules + Numba JIT, waiting ...") + + from ..foregrounds import Foregrounds + + # FIXME: This is a hack + configs = self.websocket.configs + logger.info("Checking the configurations ...") + configs.check_all() + + fg = Foregrounds(configs) + fg.preprocess() + fg.simulate() + fg.postprocess() + + logger.info("Foregrounds simulations DONE!") + + # NOTE: Should always return a tuple of (success, error) + return (True, None) diff --git a/fg21sim/webui/static/js/console.js b/fg21sim/webui/static/js/console.js index 86ea90c..14e0ad7 100644 --- a/fg21sim/webui/static/js/console.js +++ b/fg21sim/webui/static/js/console.js @@ -144,7 +144,18 @@ var getServerTaskStatus = function (ws) { */ var startServerTask = function (ws, time) { time = typeof time !== "undefined" ? time : 5; - var msg = {type: "console", action: "start", time: time}; + var msg = {type: "console", action: "start"}; + ws.send(JSON.stringify(msg)); +}; + +/** + * Request to start the test task on the server. + * + * @param {Number} time - Time in seconds for the sleep test task on server + */ +var startServerTaskTest = function (ws, time) { + time = typeof time !== "undefined" ? time : 5; + var msg = {type: "console", action: "start_test", time: time}; ws.send(JSON.stringify(msg)); }; |