Skip to content

Commit 4cde762

Browse files
committed
Fixes #111 - stupid error in the signals management... some refinements
1 parent 45e9745 commit 4cde762

File tree

5 files changed

+33
-63
lines changed

5 files changed

+33
-63
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
tags: true
5454
user: alignak
5555
password:
56-
secure: "RpL6CArq0yC734lzOh3ERq5p67274RayoiqLvEb5r5Sas+Zv0lcrEHNLT8v1fwIs0aJBZEE8NnXyTwD3bK03MGoRYGb00viVV/8x3ASYhvZ6MW41LcnLIDJRL2ZqhCU9LWcgkbGDxYoiuObedMvtwtj/iADjkQ9T7R/X9waWCLgfxOZtXDE3xnEBYXrntvGPYd/KQkrQQ1pg5SC74sdIsnHcsN5j7Dt/HlSNDv0CZaZ8zptg7dIlbn472+2TTesiyIFcEoFviGw6UmElkUTwkxyH0ix8mX1unibwaROHM/YJoz06GmAgPzjXmdAFvccWqCtp8uYMVitJSt8/JUIhsXntJM2yIhzCE/SRT7HIU0Z7IiQwjaY6bhGha8o0DmSIYdc+3cARtna5/alGGv5xQxF2TTZGEZJeOKsQsXfh9G8SpLjdl+2eznMgAO5G0SOxBs57ovSMrFmhpA3qjz8kVLgroNvEOE9gd2+9NjXm5p7ioxlXpixwWhSEkMDLP8jH7RzvnFMw+iAjeYJMYpjGAyzViDE09tsgwWlsu3aw69LBRDVS9/3b8Cb0FG7/uxffTtYc79KLI2XPt2eQRi1SihnR8mpXHqE4A5fAVn6sUmsyUMFriJipsUwaAEBNgXioGR99iDgkoEHaxFSl5cW0R/JOCoKr1Q/Bs3NjH3WBcTw="
56+
secure: "a5MNlvUNtFuQzaQc0ygYi5J0xZnEyUcwdcTAlZ17BUk1vXicb5kxBS/Yt7GXHYFxq9pqxEy0NDctTf8o0VumZVZpRfFoghzcOpO2lkSc5L1LCaEOl/v1o2kb31aT/kRuCWbFKHBXj7pDj10UyjMILcR6GdV2UVawShlSi92qo/WAKDiTaJR2wMI7zDViX6B5D3jUsPVP9TQDkzqnO5ELTbOn3eMbj7CvXjpzUxN9QxBWofzw8kLJLubo/9z0XpW83T9cYaU0rYHsBtqYnD82ZP0/nb/fFp2+TT7cv+U849lb6XXq40bWbNqz27M1fZNJEqDBUB8aOALChTceNC1kvjzyqOH+w2URa0VIpSqru9uxeU7FanNhPZ/KwJH2xN0L9r1L5SCvbTPbOp8XYaYc9DsfNVO5NNliWx0Vc2y3J8HbN9f4hgGH2PcpZQevb9IDL2cASM5tCuHHqnQw4FD2r8l9paaGt6qLbmj45SS+7XCeVrGAN5VGq52bJLHkVEsYm1mDjb4+gCNlg8GcSx4ZvaTXoGHeLQJ00WQtzbk71xqdjctuZk8oHj2/R/rrlg/ZrdKV9kf7PmpdEFJGUR8eJYQVODoBymhiubCHs2P39LFyh/e6AZa2qrMhPfj7IJCTccC6W82Ldta3lYWItd7eduNbr8ZmqvWW/8gdTVaIAdY="
5757
- stage: Debian packaging
5858
python: 2.7
5959
install:

alignak_module_ws/ws.py

+4-55
Original file line numberDiff line numberDiff line change
@@ -1800,59 +1800,6 @@ def do_loop_turn(self):
18001800
logger.info("In loop")
18011801
time.sleep(1)
18021802

1803-
def manage_signal2(self, sig, frame): # pylint: disable=unused-argument
1804-
"""Generic function to handle signals
1805-
1806-
Only called when the module process received SIGINT or SIGKILL.
1807-
1808-
Set interrupted attribute to True, self.process to None and returns
1809-
1810-
:param sig: signal sent
1811-
:type sig:
1812-
:param frame: frame before catching signal
1813-
:type frame:
1814-
:return: None
1815-
"""
1816-
logger.warning("received a signal: %s", SIGNALS_TO_NAMES_DICT[sig])
1817-
1818-
if sig == signal.SIGHUP:
1819-
# if SIGHUP, reload configuration in arbiter
1820-
logger.info("Modules are not able to reload their configuration. "
1821-
"Stopping the module...")
1822-
1823-
logger.info("Request to stop the module")
1824-
self.interrupted = True
1825-
# self.process = None
1826-
1827-
def set_signal_handler2(self, sigs=None):
1828-
"""Set the signal handler to manage_signal (defined in this class)
1829-
1830-
Only set handlers for:
1831-
- signal.SIGTERM, signal.SIGINT
1832-
- signal.SIGUSR1, signal.SIGUSR2
1833-
- signal.SIGHUP
1834-
1835-
:return: None
1836-
"""
1837-
exit(12)
1838-
logger.warning("set signal handler 2")
1839-
if sigs is None:
1840-
sigs = (signal.SIGTERM, signal.SIGINT, signal.SIGUSR1, signal.SIGUSR2, signal.SIGHUP)
1841-
1842-
func = self.manage_signal2
1843-
if os.name == "nt": # pragma: no cover, no Windows implementation currently
1844-
try:
1845-
import win32api
1846-
win32api.SetConsoleCtrlHandler(func, True)
1847-
except ImportError:
1848-
version = ".".join([str(i) for i in os.sys.version_info[:2]])
1849-
raise Exception("pywin32 not installed for Python " + version)
1850-
else:
1851-
for sig in sigs:
1852-
signal.signal(sig, func)
1853-
1854-
set_exit_handler = set_signal_handler2
1855-
18561803
def main(self):
18571804
# pylint: disable=too-many-nested-blocks
18581805
"""Main loop of the process
@@ -1862,7 +1809,7 @@ def main(self):
18621809
"""
18631810
# Set the OS process title
18641811
self.set_proctitle(self.alias)
1865-
self.set_signal_handler2()
1812+
self.set_signal_handler()
18661813

18671814
logger.info("starting...")
18681815

@@ -1902,6 +1849,7 @@ def main(self):
19021849

19031850
if not self.alignak_host:
19041851
# Do not check Alignak daemons...
1852+
time.sleep(0.1)
19051853
continue
19061854

19071855
if ping_alignak_next_time < start:
@@ -1923,7 +1871,8 @@ def main(self):
19231871
get_daemons_next_time = start + self.alignak_daemons_polling_period
19241872

19251873
# Get Arbiter all states
1926-
response = requests.get("http://%s:%s/get_all_states" %
1874+
# todo: refactor this and use /system endpoint ?
1875+
response = requests.get("http://%s:%s/satellites_configuration" %
19271876
(self.alignak_host, self.alignak_port))
19281877
if response.status_code != 200:
19291878
continue

test/alignak_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ def setup_with_file(self, configuration_file=None, env_file=None,
826826
with requests_mock.mock() as mr:
827827
for link in self._arbiter.dispatcher.all_daemons_links:
828828
# mr.get('http://%s:%s/ping' % (link.address, link.port), json='pong')
829+
mr.get('http://%s:%s/' % (link.address, link.port),
830+
json={"running_id": 123456.123456})
829831
mr.get('http://%s:%s/identity' % (link.address, link.port),
830832
json={"running_id": 123456.123456})
831833
mr.get('http://%s:%s/_wait_new_conf' % (link.address, link.port), json=True)

test/test_module.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def test_module_loading(self):
184184
"""
185185
self.setup_with_file('./cfg/cfg_default.cfg')
186186
self.assertTrue(self.conf_is_correct)
187+
print("-----\n\n")
187188
self.show_configuration_logs()
188189

189190
# No arbiter modules created
@@ -271,6 +272,8 @@ def test_module_manager(self):
271272

272273
# Start external modules
273274
self.modulemanager.start_external_instances()
275+
time.sleep(1.0)
276+
print("My module PID: %s" % my_module.process.pid)
274277

275278
# Starting external module logs
276279
self.assert_log_match("Trying to initialize module: web-services", 0)
@@ -292,14 +295,13 @@ def test_module_manager(self):
292295
index = 0
293296
self.assert_log_match("Killing external module", index)
294297
index = index +1
295-
# self.assert_log_match("web-services is still living 10 seconds after a normal kill, I help it to die", 1)
296298
self.assert_log_match("External module killed", index)
297299
index = index +1
298300

299301
# Should be dead (not normally stopped...) but we still know a process for this module!
300302
self.assertIsNotNone(my_module.process)
301303

302-
# Nothing special ...
304+
# The module is dead but the modules manager do not know yet!
303305
self.modulemanager.check_alive_instances()
304306
self.assert_log_match("The external module web-services died unexpectedly!", index)
305307
index = index +1
@@ -341,17 +343,25 @@ def test_module_manager(self):
341343
# Clear logs
342344
self.clear_logs()
343345

344-
# Now we look for time restart so we kill it again
346+
# Let the module start and then kill it again
347+
time.sleep(3.0)
345348
my_module.kill()
349+
# time.sleep(5.0)
350+
self.show_logs()
351+
print("My module PID 2: %s" % my_module.process.pid)
346352
time.sleep(0.2)
347353
self.assertFalse(my_module.process.is_alive())
348354
index = 0
349355
self.assert_log_match("Killing external module", index)
350356
index = index +1
357+
# # todo: This log is not expected! But it is probably because of the py.test ...
358+
# # Indeed the receiver daemon that the module is attached to is receiving a SIGTERM !!!
359+
# self.assert_log_match(re.escape("'web-services' is still living 10 seconds after a normal kill, I help it to die"), index)
360+
# index = index +1
351361
self.assert_log_match("External module killed", index)
352362
index = index +1
353363

354-
# Should be too early
364+
# The module is dead but the modules manager do not know yet!
355365
self.modulemanager.check_alive_instances()
356366
self.assert_log_match("The external module web-services died unexpectedly!", index)
357367
index = index +1
@@ -385,6 +395,8 @@ def test_module_manager(self):
385395
index = index +1
386396
self.assert_log_match("web-services is now started", index)
387397
index = index +1
398+
time.sleep(1.0)
399+
print("My module PID: %s" % my_module.process.pid)
388400

389401
# Clear logs
390402
self.clear_logs()

test/test_module_host.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,14 @@ def test_module_zzz_host(self):
637637
self.assertEqual(my_module.received_commands, 6)
638638

639639
# Logout
640+
print("Logout...")
640641
response = session.get(self.ws_endpoint + '/logout')
641642
self.assertEqual(response.status_code, 200)
642643
result = response.json()
643644
self.assertEqual(result['_status'], 'OK')
644645
self.assertEqual(result['_result'], 'Logged out')
645646

647+
time.sleep(1.0)
646648
self.modulemanager.stop_all()
647649

648650
def test_module_zzz_simulate_host(self):
@@ -906,10 +908,12 @@ def test_module_zzz_host_timestamp(self):
906908
'alignak_host': '',
907909
'alignak_port': 7770,
908910
})
911+
print("Module...")
909912

910913
# Create a receiver daemon
911914
args = {'env_file': '', 'daemon_name': 'receiver-master'}
912915
self._receiver_daemon = Receiver(**args)
916+
print("Daemon...")
913917

914918
# Create the modules manager for the daemon
915919
self.modulemanager = ModulesManager(self._receiver_daemon)
@@ -936,8 +940,8 @@ def test_module_zzz_host_timestamp(self):
936940
# Check alive
937941
self.assertIsNotNone(my_module.process)
938942
self.assertTrue(my_module.process.is_alive())
939-
940-
time.sleep(1)
943+
time.sleep(1.0)
944+
print("My module PID: %s" % my_module.process.pid)
941945

942946
# Do not allow GET request on /host - not authorized
943947
response = requests.get(self.ws_endpoint + '/host')
@@ -951,6 +955,7 @@ def test_module_zzz_host_timestamp(self):
951955
response = session.post(self.ws_endpoint + '/login', json=params, headers=headers)
952956
assert response.status_code == 200
953957
resp = response.json()
958+
print("Login...")
954959

955960
# You must have parameters when POSTing on /host
956961
headers = {'Content-Type': 'application/json'}
@@ -1426,13 +1431,15 @@ def test_module_zzz_host_timestamp(self):
14261431
})
14271432

14281433
# Logout
1434+
print("Logout...")
14291435
response = session.get(self.ws_endpoint + '/logout')
14301436
self.assertEqual(response.status_code, 200)
14311437
result = response.json()
14321438
self.assertEqual(result['_status'], 'OK')
14331439
self.assertEqual(result['_result'], 'Logged out')
14341440

1435-
self.modulemanager.stop_all()
1441+
time.sleep(1.0)
1442+
# self.modulemanager.stop_all()
14361443

14371444
def test_module_zzz_host_variables(self):
14381445
"""Test the module /host API * create/update custom variables

0 commit comments

Comments
 (0)