Skip to content

Commit

Permalink
Renaming high priority threads
Browse files Browse the repository at this point in the history
Ref #303
  • Loading branch information
algorys committed Apr 12, 2018
1 parent f586005 commit 2ad3bea
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions alignak_app/qobjects/host/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def update_host(self, host_item=None):
# Update host services
self.refresh_counter += 1
if self.refresh_counter > 10:
thread_manager.add_priority_thread('service', self.host_item.item_id)
thread_manager.add_high_priority_thread('service', self.host_item.item_id)
self.refresh_counter = 0

# Update host
Expand Down Expand Up @@ -518,7 +518,7 @@ def update_host(self, host_item=None):
self.history_btn.setEnabled(False)

if app_backend.connected:
thread_manager.add_priority_thread(
thread_manager.add_high_priority_thread(
'history',
{'hostname': self.host_item.name, 'host_id': self.host_item.item_id}
)
Expand Down
34 changes: 19 additions & 15 deletions alignak_app/qobjects/threads/threadmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
"""
Thread Manager
++++++++++++++
Thread Manager manage BackendQThreads creations and their priority
Thread Manager manage BackendQThreads creations and their priority:
* low : threads that group the queries about the elements that change little
* normal: threads that group the queries on items that change often
* high: threads that group the queries made on the fly
"""

from logging import getLogger
Expand All @@ -48,7 +52,7 @@ def __init__(self, parent=None):
super(ThreadManager, self).__init__(parent)
self.threads_to_launch = {'low': [], 'normal': []}
self.launched_threads = {'low': [], 'normal': []}
self.priority_threads = []
self.high_threads = []

def fill_threads(self, thread_type):
"""
Expand Down Expand Up @@ -112,7 +116,7 @@ def launch_threads(self, thread_type=None): # pragma: no cover
thread
)

def add_priority_thread(self, thread_name, data):
def add_high_priority_thread(self, thread_name, data):
"""
Launch a thread with higher priority (doesn't wait launch_threads() function)
Expand All @@ -122,11 +126,11 @@ def add_priority_thread(self, thread_name, data):
:type data: dict
"""

if len(self.priority_threads) < 3:
if len(self.high_threads) < 3:
backend_thread = BackendQThread(thread_name, data)
backend_thread.start()

self.priority_threads.append(backend_thread)
self.high_threads.append(backend_thread)
else:
logger.debug('Too many priority threads for the moment...')

Expand All @@ -143,11 +147,11 @@ def clean_threads(self): # pragma: no cover
thread.quit()
self.launched_threads[t_thread].remove(thread)

if self.priority_threads:
for thread in self.priority_threads:
if self.high_threads:
for thread in self.high_threads:
if thread.isFinished():
thread.quit()
self.priority_threads.remove(thread)
self.high_threads.remove(thread)
logger.debug('Remove finished thread: %s', thread.name)

def stop_threads(self):
Expand All @@ -156,7 +160,7 @@ def stop_threads(self):
"""

if not self.priority_threads and not self.launched_threads['low'] and \
if not self.high_threads and not self.launched_threads['low'] and \
not self.launched_threads['normal']:
logger.debug('No thread to stop.')

Expand All @@ -166,20 +170,20 @@ def stop_threads(self):
thread.quit()
self.launched_threads[t_thread].remove(thread)

if self.priority_threads:
self.stop_priority_threads()
if self.high_threads:
self.stop_high_priority_threads()

def stop_priority_threads(self):
def stop_high_priority_threads(self):
"""
Stop priority threads
Stop threads with high priority
"""

for thread in self.priority_threads:
for thread in self.high_threads:
logger.debug('Quit priority thread: %s', thread.name)
thread.quit()

self.priority_threads.remove(thread)
self.high_threads.remove(thread)


thread_manager = ThreadManager()
28 changes: 14 additions & 14 deletions test/test_thread_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_initialize_thread_manager(self):

for t_type in under_test.threads_to_launch:
self.assertFalse(under_test.threads_to_launch[t_type])
self.assertFalse(under_test.priority_threads)
self.assertFalse(under_test.high_threads)

def test_fill_threads(self):
"""Add Low / Normal Threads"""
Expand Down Expand Up @@ -81,42 +81,42 @@ def test_priority_threads(self):
"""Add / Remove Priority Threads"""

under_test = ThreadManager()
under_test.priority_threads.append(BackendQThread('user'))
under_test.high_threads.append(BackendQThread('user'))

self.assertTrue(under_test.priority_threads)
self.assertTrue(under_test.high_threads)

under_test.stop_priority_threads()
under_test.stop_high_priority_threads()

# Priority thread is removed
self.assertFalse(under_test.priority_threads)
self.assertFalse(under_test.high_threads)

# Add 3 priority threads
under_test.priority_threads.append(BackendQThread('user'))
under_test.priority_threads.append(BackendQThread('host'))
under_test.priority_threads.append(BackendQThread('history'))
under_test.high_threads.append(BackendQThread('user'))
under_test.high_threads.append(BackendQThread('host'))
under_test.high_threads.append(BackendQThread('history'))

self.assertTrue(len(under_test.priority_threads) == 3)
self.assertTrue(len(under_test.high_threads) == 3)

under_test.add_priority_thread('user', {})
under_test.add_high_priority_thread('user', {})

# When already 3 priority threads, next priority htread is not add
self.assertTrue(len(under_test.priority_threads) == 3)
self.assertTrue(len(under_test.high_threads) == 3)

def test_stop_threads(self):
"""Stop All Threads"""

under_test = ThreadManager()

under_test.priority_threads.append(BackendQThread(''))
under_test.high_threads.append(BackendQThread(''))
under_test.launched_threads['low'].append(BackendQThread(''))
under_test.launched_threads['normal'].append(BackendQThread(''))

self.assertTrue(under_test.priority_threads)
self.assertTrue(under_test.high_threads)
self.assertTrue(under_test.launched_threads['low'])
self.assertTrue(under_test.launched_threads['normal'])

under_test.stop_threads()

self.assertFalse(under_test.priority_threads)
self.assertFalse(under_test.high_threads)
self.assertFalse(under_test.launched_threads['low'])
self.assertFalse(under_test.launched_threads['normal'])

0 comments on commit 2ad3bea

Please sign in to comment.