Skip to content

Commit 1c2d417

Browse files
committed
Fixes #348
1 parent 952c22f commit 1c2d417

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

elastichq/api/socket.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,8 @@ def do_msg(message):
9999
@socketio.on('disconnect', namespace='/ws')
100100
def disconnect():
101101
LOG.debug('Client disconnected')
102-
# TODO
102+
103+
# client disconnected
104+
sid = request.sid
105+
taskPool.diconnect_client(sid)
106+

elastichq/common/TaskPool.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ def init_app(self, socketio):
1010
return self
1111

1212
def add(self, task):
13-
self.tasks.append(task)
13+
# sanity check for unique tasks:
14+
if self.get_task_by_room_name(task.room_name) is None:
15+
self.tasks.append(task)
1416

1517
def get_task_by_room_name(self, room_name):
18+
"""
19+
20+
:param room_name: unique ID of tasks
21+
:return:
22+
"""
1623
for task in self.tasks:
1724
if task.room_name == room_name:
1825
return task
26+
return None
1927

2028
def get_tasks_by_cluster_name(self, cluster_name):
2129
for task in self.tasks:
@@ -37,5 +45,16 @@ def create_task(self, task, sid):
3745
task.add_session(sid)
3846
self.socketio.start_background_task(target=task.run)
3947
self.add(task)
40-
else:
48+
else: # task already exists
4149
task.add_session(sid)
50+
51+
def diconnect_client(self, sid):
52+
for task in self.tasks:
53+
for session in task.sessions:
54+
if session == sid:
55+
task.remove_session(sid)
56+
if len(task.sessions) == 0:
57+
task.stop()
58+
self.remove(task.room_name)
59+
break
60+

elastichq/config/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class TestSettings(BaseSettings):
5656
# static
5757
HQ_SITE_URL = 'http://elastichq.org'
5858
HQ_GH_URL = 'https://github.com/ElasticHQ/elasticsearch-HQ'
59-
API_VERSION = 'v3.0'
59+
API_VERSION = 'v3.1.0'
6060
ES_V2_HOST = '127.0.0.1'
6161
ES_V2_PORT = '9200'
6262
ES_V5_HOST = '127.0.0.1'
@@ -116,7 +116,7 @@ class ProdSettings(BaseSettings):
116116
# static
117117
HQ_SITE_URL = 'http://elastichq.org'
118118
HQ_GH_URL = 'https://github.com/ElasticHQ/elasticsearch-HQ'
119-
API_VERSION = '3.0.3'
119+
API_VERSION = '3.1.0'
120120

121121

122122
SCHEDULER_EXECUTORS = {

elastichq/model/Task.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

1313
class Task:
1414
"""
15-
15+
Websocket background thread that broadcasts to a room.
1616
"""
1717

18+
# room_name == Task Name. Unique identifier
1819
room_name = None
1920
cluster_name = None
2021
metric = None
@@ -30,6 +31,11 @@ def __init__(self, room_name, cluster_name, metric):
3031
self.cluster_name = cluster_name
3132
self.metric = metric
3233

34+
def remove_session(self, session_id):
35+
self.sessions.remove(session_id)
36+
LOG.debug("Removing client: " + session_id)
37+
38+
3339
def add_session(self, session_id):
3440
self.sessions.append(session_id)
3541

@@ -46,6 +52,12 @@ def run(self):
4652

4753
# https://stackoverflow.com/questions/44371041/python-socketio-and-flask-how-to-stop-a-loop-in-a-background-thread
4854
while self.switch:
55+
56+
# automatically stop this task when the room is empty
57+
if len(self.sessions) == 0:
58+
self.stop()
59+
# TODO: remove task from pool
60+
4961
eventlet.sleep(5)
5062

5163
LOG.debug('-----------------------------------------')
@@ -75,10 +87,11 @@ def run(self):
7587
"docs_deleted": jmespath.search("indices.docs.deleted", node_dict),
7688
"store_size": jmespath.search("indices.store.size_in_bytes", node_dict),
7789
"cpu_percent": jmespath.search("process.cpu.percent", node_dict),
78-
"field_data_cache_in_bytes": jmespath.search("indices.fielddata.memory_size_in_bytes", node_dict),
79-
"fs_used_in_bytes" : total_in_bytes - available_in_bytes,
80-
"fs_free_in_bytes" : available_in_bytes,
81-
"index_total" : jmespath.search("indices.indexing.index_total", node_dict)
90+
"field_data_cache_in_bytes": jmespath.search("indices.fielddata.memory_size_in_bytes",
91+
node_dict),
92+
"fs_used_in_bytes": total_in_bytes - available_in_bytes,
93+
"fs_free_in_bytes": available_in_bytes,
94+
"index_total": jmespath.search("indices.indexing.index_total", node_dict)
8295
}
8396
nodes.append(node)
8497
LOG.debug("Broadcast to room: " + self.room_name)
@@ -88,3 +101,4 @@ def run(self):
88101

89102
def stop(self):
90103
self.switch = False
104+
LOG.debug("Task Stopped: " + self.room_name)

ui/src/components/cluster-nodes-line-graph/cluster-nodes-line-graph.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class clusterNodesLineGraphController {
1111
this.$window = $window;
1212
this.widthFrom = angular.element($element[0].children[0])[0];
1313

14-
this.h = 240;
14+
this.h = 220;
1515
this.svgContainer = d3.select(angular.element($element[0].querySelector('.chart'))[0])
1616
.append('svg').attr('height', this.h);
1717

0 commit comments

Comments
 (0)