diff --git a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py index 9363616e..2d9d0434 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py @@ -142,7 +142,7 @@ RPCConnection.register_location(config.get('SIGNER_SOCKET_PATH'), chain_spec, 's Otx.tracing = config.true('TASKS_TRACE_QUEUE_STATUS') -liveness.linux.load('cic-eth', health_modules) +liveness.linux.load(health_modules) def main(): argv = ['worker'] @@ -178,9 +178,9 @@ def main(): connect_declarator(rpc, chain_spec, trusted_addresses) connect_token_registry(rpc, chain_spec) - liveness.linux.set('cic-eth') + liveness.linux.set() current_app.worker_main(argv) - liveness.linux.reset('cic-eth') + liveness.linux.reset() @celery.signals.eventlet_pool_postshutdown.connect diff --git a/apps/cic-eth/docker/Dockerfile b/apps/cic-eth/docker/Dockerfile index 9ca4a83a..00f3903a 100644 --- a/apps/cic-eth/docker/Dockerfile +++ b/apps/cic-eth/docker/Dockerfile @@ -53,3 +53,5 @@ COPY cic-eth/crypto_dev_signer_config/ /usr/local/etc/crypto-dev-signer/ RUN git clone https://gitlab.com/grassrootseconomics/cic-contracts.git && \ mkdir -p /usr/local/share/cic/solidity && \ cp -R cic-contracts/abis /usr/local/share/cic/solidity/abi + +COPY util/liveness/health.sh /usr/local/bin/health.sh diff --git a/apps/util/liveness/health.sh b/apps/util/liveness/health.sh index 7046de42..b19763e4 100644 --- a/apps/util/liveness/health.sh +++ b/apps/util/liveness/health.sh @@ -1,33 +1,34 @@ #!/bin/bash rundir=${CIC_RUNDIR:-/run} +unit=${CIC_UNIT:-$HOSTNAME} -read p < $rundir/$CIC_UNIT/pid +read p < $rundir/$unit/pid if [ -z $p ]; then - >&2 echo unit $CIC_UNIT has no pid + >&2 echo unit $unit has no pid exit 1 fi if [ ! -d /proc/$p ]; then - >&2 echo unit $CIC_UNIT reports non-existent pid $p + >&2 echo unit $unit reports non-existent pid $p exit 1 fi ->&2 echo unit $CIC_UNIT has pid $p +>&2 echo unit $unit has pid $p -if [ ! -f $rundir/$CIC_UNIT/error ]; then - >&2 echo unit $CIC_UNIT has unspecified state +if [ ! -f $rundir/$unit/error ]; then + >&2 echo unit $unit has unspecified state exit 1 fi -read e 2> /dev/null < $rundir/$CIC_UNIT/error +read e 2> /dev/null < $rundir/$unit/error if [ -z $e ]; then - >&2 echo unit $CIC_UNIT has unspecified state + >&2 echo unit $unit has unspecified state exit 1 fi ->&2 echo unit $CIC_UNIT has error $e +>&2 echo unit $unit has error $e if [ $e -gt 0 ]; then exit 1; diff --git a/apps/util/liveness/liveness/linux.py b/apps/util/liveness/liveness/linux.py index f2143585..1ce1fb02 100644 --- a/apps/util/liveness/liveness/linux.py +++ b/apps/util/liveness/liveness/linux.py @@ -8,8 +8,18 @@ logg = logging.getLogger().getChild(__name__) pid = os.getpid() +default_namespace = os.environ.get('LIVENESS_UNIT_NAME') +if default_namespace == None: + import socket + default_namespace = socket.gethostname() + + +def load(check_strs, namespace=default_namespace, rundir='/run'): + + if namespace == None: + import socket + namespace = socket.gethostname() -def load(namespace, check_strs, rundir='/run'): logg.info('pid ' + str(pid)) checks = [] @@ -31,13 +41,13 @@ def load(namespace, check_strs, rundir='/run'): f.close() -def set(namespace, error=0, rundir='/run'): +def set(error=0, namespace=default_namespace, rundir='/run'): app_rundir = os.path.join(rundir, namespace) f = open(os.path.join(app_rundir, 'error'), 'w') f.write(str(error)) f.close() -def reset(namespace, rundir='/run'): +def reset(namespace=default_namespace, rundir='/run'): app_rundir = os.path.join(rundir, namespace) os.unlink(os.path.join(app_rundir, 'error')) diff --git a/apps/util/liveness/setup.py b/apps/util/liveness/setup.py index 684a5a55..9862eaf2 100644 --- a/apps/util/liveness/setup.py +++ b/apps/util/liveness/setup.py @@ -1,7 +1,7 @@ from setuptools import setup setup( name='liveness', - version='0.0.1a2', + version='0.0.1a4', packages=['liveness'], include_package_data=True, ) diff --git a/apps/util/liveness/tests/test_imports.py b/apps/util/liveness/tests/test_imports.py index 745123c6..1505f28f 100644 --- a/apps/util/liveness/tests/test_imports.py +++ b/apps/util/liveness/tests/test_imports.py @@ -3,6 +3,7 @@ import os import unittest import logging import tempfile +import socket # local imports import liveness.linux @@ -18,27 +19,37 @@ data_dir = os.path.join(script_dir, 'testdata') run_base_dir = os.path.join(data_dir, 'run') - class TestImports(unittest.TestCase): def setUp(self): os.makedirs(run_base_dir, exist_ok=True) self.run_dir = tempfile.mkdtemp(dir=run_base_dir) - self.unit_dir = os.path.join(self.run_dir, 'unittest') + self.unit = 'unittest' + self.unit_dir = os.path.join(self.run_dir, self.unit) self.pid_path = os.path.join(self.unit_dir, 'pid') self.error_path = os.path.join(self.unit_dir, 'error') + self.host_path = os.path.join(self.run_dir, socket.gethostname()) + def test_no_import(self): - liveness.linux.load('unittest', [], rundir=self.run_dir) + liveness.linux.load([], namespace=self.unit, rundir=self.run_dir) f = open(self.pid_path, 'r') r = f.read() f.close() self.assertEqual(str(os.getpid()), r) - + + def test_hostname(self): + liveness.linux.load([], rundir=self.run_dir) + f = open(os.path.join(self.host_path, 'pid'), 'r') + r = f.read() + f.close() + self.assertEqual(str(os.getpid()), r) + + def test_import_single_true(self): checks = ['tests.imports.import_true'] - liveness.linux.load('unittest', checks, rundir=self.run_dir) + liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir) f = open(self.pid_path, 'r') r = f.read() f.close() @@ -48,7 +59,7 @@ class TestImports(unittest.TestCase): def test_import_single_false(self): checks = ['tests.imports.import_false'] with self.assertRaises(RuntimeError): - liveness.linux.load('unittest', checks, rundir=self.run_dir) + liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir) with self.assertRaises(FileNotFoundError): os.stat(self.pid_path) @@ -56,14 +67,14 @@ class TestImports(unittest.TestCase): def test_import_false_then_true(self): checks = ['tests.imports.import_false', 'tests.imports.import_true'] with self.assertRaises(RuntimeError): - liveness.linux.load('unittest', checks, rundir=self.run_dir) + liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir) with self.assertRaises(FileNotFoundError): os.stat(self.pid_path) def test_import_multiple_true(self): checks = ['tests.imports.import_true', 'tests.imports.import_true'] - liveness.linux.load('unittest', checks, rundir=self.run_dir) + liveness.linux.load(checks, namespace=self.unit, rundir=self.run_dir) f = open(self.pid_path, 'r') r = f.read() f.close() @@ -71,24 +82,37 @@ class TestImports(unittest.TestCase): def test_set(self): - liveness.linux.load('unittest', [], rundir=self.run_dir) - liveness.linux.set('unittest', rundir=self.run_dir) + liveness.linux.load([], namespace='unittest', rundir=self.run_dir) + liveness.linux.set(namespace='unittest', rundir=self.run_dir) f = open(self.error_path, 'r') r = f.read() f.close() self.assertEqual('0', r) - liveness.linux.set('unittest', 42, rundir=self.run_dir) + liveness.linux.set(error=42, namespace='unittest', rundir=self.run_dir) f = open(self.error_path, 'r') r = f.read() f.close() self.assertEqual('42', r) - liveness.linux.reset('unittest', rundir=self.run_dir) + liveness.linux.reset(namespace='unittest', rundir=self.run_dir) with self.assertRaises(FileNotFoundError): os.stat(self.error_path) + def test_set_hostname(self): + liveness.linux.load([], rundir=self.run_dir) + liveness.linux.set(rundir=self.run_dir) + error_path = os.path.join(self.host_path, 'error') + f = open(error_path, 'r') + r = f.read() + f.close() + self.assertEqual('0', r) + + liveness.linux.reset(rundir=self.run_dir) + with self.assertRaises(FileNotFoundError): + os.stat(error_path) + if __name__ == '__main__': unittest.main()