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):
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 = []
......
......@@ -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
......@@ -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);
}
......
......@@ -55,7 +55,7 @@
処理中...
</div>
<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>
</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