aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSatoshi Fujii <fujii-satoshi@jp.fujitsu.com>2021-10-18 06:23:34 +0000
committerSatoshi Fujii <fujii-satoshi@jp.fujitsu.com>2021-10-21 15:10:50 +0000
commitc48b9767a6bcad91f89fc3fded0b6024e89525c0 (patch)
tree5f06c4c6637941902822374d551911d7c6dc0748
parenta8d624c0502c241feadcfba71ba1442b22d9908b (diff)
Fix no such file or directory error
fetch_json_file() function is called from two processes, misshtbtd and cbs_polling. When they call the func at the same time, both procs try to remove download.json, so `No such file or directory` error occurs on one of them and the process stop working by the error. This change adds an exception handling to ignore the error. Issue-ID: DCAEGEN2-2872 Signed-off-by: Satoshi Fujii <fujii-satoshi@jp.fujitsu.com> Change-Id: I4f9f0a4522badcdef51392f4bbccfd6aa42fd9d0
-rw-r--r--Changelog.md1
-rw-r--r--miss_htbt_service/misshtbtd.py49
2 files changed, 24 insertions, 26 deletions
diff --git a/Changelog.md b/Changelog.md
index faa6057..db72ea9 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [DCAEGEN2-2939] Removed unused code (config\_notif.py)
### Fixed
- [DCAEGEN2-2832] Pod become unready state
+- [DCAEGEN2-2872] No such file or directory error and stop working
- [DCAEGEN2-2940] Microsec timestamp not properly handled
- [DCAEGEN2-2944] cbs polling process startup failure
diff --git a/miss_htbt_service/misshtbtd.py b/miss_htbt_service/misshtbtd.py
index aeb5694..61da9d9 100644
--- a/miss_htbt_service/misshtbtd.py
+++ b/miss_htbt_service/misshtbtd.py
@@ -27,7 +27,7 @@
# - Download the CBS configuration and populate the DB
#
# Author Prakash Hosangady(ph553f@att.com)
-
+import shutil
import traceback
import os
import sys
@@ -38,6 +38,7 @@ import subprocess
import yaml
import socket
import os.path as path
+import tempfile
from pathlib import Path
import check_health
@@ -51,6 +52,7 @@ hb_properties_file = path.abspath(path.join(__file__, "../config/hbproperties.ya
ABSOLUTE_PATH1 = path.abspath(path.join(__file__, "../htbtworker.py"))
ABSOLUTE_PATH2 = path.abspath(path.join(__file__, "../db_monitoring.py"))
+CONFIG_PATH = "../etc/config.json"
def create_database(update_db, jsfile, ip_address, port_num, user_name, password, db_name):
@@ -259,33 +261,28 @@ def read_hb_properties(jsfile):
return ip_address, port_num, user_name, password, db_name, cbs_polling_required, cbs_polling_interval
-def fetch_json_file():
+def fetch_json_file() -> str:
+ """Get configuration from CBS and save it to json file.
+
+ :return: path to saved json file
+ """
+ # note: this func is called from multiple subprocesses. need to be thread-safe.
+ jsfile = CONFIG_PATH
+ # Try to get config from CBS. If succeeded, config json is stored to tds.c_config .
if get_cbs_config():
- envPytest = os.getenv('pytest', "")
- if envPytest == 'test':
- current_runtime_config_file_name = "/tmp/opt/app/miss_htbt_service/etc/config.json"
- else:
- current_runtime_config_file_name = "../etc/download.json"
- msg = "MSHBD:current config logged to : %s" % current_runtime_config_file_name
- _logger.info(msg)
- with open(current_runtime_config_file_name, 'w') as outfile:
- json.dump(tds.c_config, outfile)
- if os.getenv('pytest', "") == 'test':
- jsfile = current_runtime_config_file_name
- else:
- jsfile = "../etc/config.json"
- os.system('cp ../etc/download.json ../etc/config.json')
- os.remove("../etc/download.json")
+ # Save config to temporary file
+ with tempfile.NamedTemporaryFile('w', delete=False) as temp:
+ _logger.info("MSHBD: New config saved to temp file %s", temp.name)
+ json.dump(tds.c_config, temp)
+ # Swap current config with downloaded config
+ os.makedirs(Path(jsfile).parent, exist_ok=True)
+ shutil.move(temp.name, jsfile)
else:
- msg = "MSHBD:CBS Config not available, using local config"
- _logger.warning(msg)
- my_file = Path("./etc/config.json")
- if my_file.is_file():
- jsfile = "./etc/config.json"
- else:
- jsfile = "../etc/config.json"
- msg = "MSHBT: The json file is - ", jsfile
- _logger.info(msg)
+ _logger.warning("MSHBD: CBS Config not available, using local config")
+ local_config = "./etc/config.json"
+ if Path(local_config).is_file():
+ jsfile = local_config
+ _logger.info("MSHBD: The json file is %s", jsfile)
return jsfile