Commit 105e3566 authored by Kento HASEGAWA's avatar Kento HASEGAWA

Add the stop button that will stop all working solvers (but currently does not work)

parent c2566bbc
...@@ -125,6 +125,9 @@ class Host(object): ...@@ -125,6 +125,9 @@ class Host(object):
elif cmd == 'request/status': elif cmd == 'request/status':
request_id = float(params['request_id']) request_id = float(params['request_id'])
return self.get_request_status(request_id) return self.get_request_status(request_id)
elif cmd == 'stop':
self.worker_manager.request_stop()
return {}
elif cmd == 'view/solution': elif cmd == 'view/solution':
problem_key = params['problem'] problem_key = params['problem']
solution_id = params['solution'] solution_id = params['solution']
...@@ -150,6 +153,9 @@ class WorkerManager(object): ...@@ -150,6 +153,9 @@ class WorkerManager(object):
def get_workers(self): def get_workers(self):
return self.workers return self.workers
def request_stop(self):
self.broadcast('stop', {})
def broadcast(self, cmd, params): def broadcast(self, cmd, params):
threads = [] threads = []
......
...@@ -12,7 +12,7 @@ class StoppableThread(threading.Thread): ...@@ -12,7 +12,7 @@ class StoppableThread(threading.Thread):
super(StoppableThread, self).__init__(target=target, args=args) super(StoppableThread, self).__init__(target=target, args=args)
self._status = 'running' self._status = 'running'
def stop(self): def stop(self):
if self._status == 'running': if self.is_running():
self._status = 'stopping' self._status = 'stopping'
def stopped(self): def stopped(self):
self._status = 'stopped' self._status = 'stopped'
...@@ -80,18 +80,22 @@ class Solver(object): ...@@ -80,18 +80,22 @@ class Solver(object):
else: else:
return {'status': 'busy'} return {'status': 'busy'}
def stop_solver(): def stop_solver(self):
if self.thread is not None: if self.thread is not None:
if self.thread.is_running(): if self.thread.is_running():
self.thread.stop() self.thread.stop()
self.thread.join() self.thread.join(0.1)
self.thread = None self.thread = None
return {'status': 'stopped'}
def call_api(self, method, cmd, params): def call_api(self, method, cmd, params):
if cmd == 'role': if cmd == 'role':
return {'role': self.type} return {'role': self.type}
elif cmd == 'solve': elif cmd == 'solve':
return self.start_solver(params) return self.start_solver(params)
elif cmd == 'stop':
return self.stop_solver()
else: else:
return None return None
...@@ -49,7 +49,6 @@ class StatusView { ...@@ -49,7 +49,6 @@ class StatusView {
}), }),
contentType: 'application/json' contentType: 'application/json'
}).done((d) => { }).done((d) => {
// TODO: タイムアウトになる時間まで,モーダル表示させる
var request_id = d['request_id']; var request_id = d['request_id'];
var timeout = d['timeout']; var timeout = d['timeout'];
...@@ -63,10 +62,28 @@ class StatusView { ...@@ -63,10 +62,28 @@ class StatusView {
_this.show_problem(); _this.show_problem();
}) })
_this.container.find('.stop-button').click(()=>{
_this.stop_solver();
});
_this.get_request_status(request_id, timeout); _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){ get_request_status(request_id, timeout){
var _this = this; var _this = this;
$.ajax({ $.ajax({
...@@ -81,6 +98,8 @@ class StatusView { ...@@ -81,6 +98,8 @@ class StatusView {
// console.log(status); // console.log(status);
if(status == 'done'){ if(status == 'done'){
_this.request_refresh_timer = null; _this.request_refresh_timer = null;
}else if(_this.request_refresh_timer == 'stopped'){
_this.request_refresh_timer = null;
}else{ }else{
_this.request_refresh_timer = setTimeout(_this.get_request_status(request_id, timeout), 1000); _this.request_refresh_timer = setTimeout(_this.get_request_status(request_id, timeout), 1000);
} }
......
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
処理中... 処理中...
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-danger">Stop</button> <button type="button" class="btn btn-danger stop-button">Stop</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div> </div>
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment