diff --git a/roles/host.py b/roles/host.py index 400fce4d85ebfa1cc23b314d97efcf3c3a498bdb..73a9d98cdd0ddae5aa1e12e423481db3461010af 100644 --- a/roles/host.py +++ b/roles/host.py @@ -125,6 +125,9 @@ class Host(object): elif cmd == 'request/status': request_id = float(params['request_id']) return self.get_request_status(request_id) + elif cmd == 'stop': + self.worker_manager.request_stop() + return {} elif cmd == 'view/solution': problem_key = params['problem'] solution_id = params['solution'] @@ -150,6 +153,9 @@ class WorkerManager(object): def get_workers(self): return self.workers + def request_stop(self): + self.broadcast('stop', {}) + def broadcast(self, cmd, params): threads = [] diff --git a/roles/solver.py b/roles/solver.py index f79096b5fda83f8f053b5a2e522f8abdfd437d3d..8668abda64390c02f3792a42b94c7eeeae090d91 100644 --- a/roles/solver.py +++ b/roles/solver.py @@ -12,7 +12,7 @@ class StoppableThread(threading.Thread): super(StoppableThread, self).__init__(target=target, args=args) self._status = 'running' def stop(self): - if self._status == 'running': + if self.is_running(): self._status = 'stopping' def stopped(self): self._status = 'stopped' @@ -80,18 +80,22 @@ class Solver(object): else: return {'status': 'busy'} - def stop_solver(): + def stop_solver(self): if self.thread is not None: if self.thread.is_running(): self.thread.stop() - self.thread.join() + self.thread.join(0.1) self.thread = None + + return {'status': 'stopped'} def call_api(self, method, cmd, params): if cmd == 'role': return {'role': self.type} elif cmd == 'solve': return self.start_solver(params) + elif cmd == 'stop': + return self.stop_solver() else: return None diff --git a/static/js/adc2019.js b/static/js/adc2019.js index 171cbb231e7ec9a92b7fa835b4b3e57f2376b567..72d16c66c5d354da644ca149c48c847f5708e4f6 100644 --- a/static/js/adc2019.js +++ b/static/js/adc2019.js @@ -49,7 +49,6 @@ class StatusView { }), contentType: 'application/json' }).done((d) => { - // TODO: タイムアウトになる時間まで,モーダル表示させる var request_id = d['request_id']; var timeout = d['timeout']; @@ -62,10 +61,28 @@ class StatusView { // console.log(e); _this.show_problem(); }) + + _this.container.find('.stop-button').click(()=>{ + _this.stop_solver(); + }); _this.get_request_status(request_id, timeout); }); } + + // 強制停止 + stop_solver(){ + var _this = this; + + $.ajax({ + type: "GET", + dataType: "json", + url: "/api/stop", + }).done((d) => { + _this.request_refresh_timer = 'stopped'; + alert(d); + }); + } get_request_status(request_id, timeout){ var _this = this; @@ -81,6 +98,8 @@ class StatusView { // console.log(status); if(status == 'done'){ _this.request_refresh_timer = null; + }else if(_this.request_refresh_timer == 'stopped'){ + _this.request_refresh_timer = null; }else{ _this.request_refresh_timer = setTimeout(_this.get_request_status(request_id, timeout), 1000); } diff --git a/templates/part_problem_status.html b/templates/part_problem_status.html index 088a64e77df5c8fa891f67476fe7d56de9daf24d..e3565ac799b766854ccc5ea5b8522969d6b5e6fc 100644 --- a/templates/part_problem_status.html +++ b/templates/part_problem_status.html @@ -55,7 +55,7 @@ 処理中...