Skip to content

Commit

Permalink
ok. passes tests now for #793
Browse files Browse the repository at this point in the history
  • Loading branch information
petersilva committed Nov 2, 2023
1 parent 69ad96e commit 0f7437c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 11 deletions.
40 changes: 34 additions & 6 deletions sarracenia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,21 @@ def get_log_filename(hostdir, component, configuration, no):

return logdir + os.sep + component + configuration + '_%02d' % no + '.log'

def get_metrics_filename(hostdir, component, configuration, no):
"""
return the name of a single logfile for a single instance.
"""
metricsdir = get_user_cache_dir(hostdir) + os.sep + 'metrics'

if configuration is None:
configuration = ''
else:
configuration = '_' + configuration

if configuration[-5:] == '.conf':
configuration = configuration[:-5]

return metricsdir + os.sep + component + configuration + '_%02d' % no + '.json'

def wget_config(urlstr, path, remote_config_url=False):
logger.debug("wget_config %s %s" % (urlstr, path))
Expand Down Expand Up @@ -1829,10 +1844,9 @@ def finalize(self, component=None, config=None):
hostdir = self.hostdir
else:
hostdir = None
self.pid_filename = get_pid_filename(hostdir, component, cfg,
self.no)
self.metricsFilename = get_metrics_filename(hostdir, component, cfg, self.no)
self.pid_filename = get_pid_filename(hostdir, component, cfg, self.no)
self.retry_path = self.pid_filename.replace('.pid', '.retry')
self.metricsFilename = self.pid_filename.replace('.pid', '.metrics')
self.novipFilename = self.pid_filename.replace('.pid', '.noVip')


Expand Down Expand Up @@ -2581,10 +2595,24 @@ def cfglogs(cfg_preparse, component, config, logLevel, child_inst):
else:
hostdir = None

logfilename = get_log_filename(
hostdir, component, config, child_inst)
metricsfilename = get_metrics_filename( hostdir, component, config, child_inst)

dir_not_there = not os.path.exists(os.path.dirname(metricsfilename))
while dir_not_there:
try:
os.makedirs(os.path.dirname(metricsfilename), exist_ok=True)
dir_not_there = False
except FileExistsError:
dir_not_there = False
except Exception as ex:
logging.error( "makedirs {} failed err={}".format(os.path.dirname(metricsfilename),ex))
logging.debug("Exception details:", exc_info=True)
os.sleep(1)

cfg_preparse.metricsFilename = metricsfilename

logfilename = get_log_filename( hostdir, component, config, child_inst)

#print('logfilename= %s' % logfilename )
dir_not_there = not os.path.exists(os.path.dirname(logfilename))
while dir_not_there:
try:
Expand Down
21 changes: 17 additions & 4 deletions sarracenia/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,24 @@ def start(self):
else:
hostdir = None

logfilename = sarracenia.config.get_log_filename(
hostdir, component, config, cfg_preparse.no)
metricsfilename = sarracenia.config.get_metrics_filename( hostdir, component, config, cfg_preparse.no)

dir_not_there = not os.path.exists(os.path.dirname(metricsfilename))
while dir_not_there:
try:
os.makedirs(os.path.dirname(metricsfilename), exist_ok=True)
dir_not_there = False
except FileExistsError:
dir_not_there = False
except Exception as ex:
logging.error( "makedirs {} failed err={}".format(os.path.dirname(metricsfilename),ex))
logging.debug("Exception details:", exc_info=True)

cfg_preparse.metricsFilename = metricsfilename


logfilename = sarracenia.config.get_log_filename( hostdir, component, config, cfg_preparse.no)

#print('logfilename= %s' % logfilename )
dir_not_there = not os.path.exists(os.path.dirname(logfilename))
while dir_not_there:
try:
Expand Down Expand Up @@ -209,7 +223,6 @@ def start(self):

cfg = sarracenia.config.one_config(component, config)

cfg.metricsFilename = pidfilename.replace(".pid", ".metrics")
cfg.novipFilename = pidfilename.replace(".pid", ".noVip")

if not hasattr(cfg, 'env_declared'):
Expand Down
43 changes: 42 additions & 1 deletion sarracenia/sr.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def _read_state_dir(self, dir1):

for pathname in os.listdir():
p = pathlib.Path(pathname)
if p.suffix in ['.pid', '.qname', '.state', '.metrics', '.noVip']:
if p.suffix in ['.pid', '.qname', '.state', '.noVip']:
if sys.version_info[0] > 3 or sys.version_info[
1] > 4:
t = p.read_text().strip()
Expand Down Expand Up @@ -501,13 +501,54 @@ def _read_state_dir(self, dir1):
os.chdir('..')
os.chdir('..')

def _read_metrics_dir(self,metrics_parent_dir):
# read in metrics files

dir1 = metrics_parent_dir + os.sep + 'metrics'

if not os.path.isdir(dir1):
return
os.chdir(dir1)

for l in os.listdir(dir1):

if not l.endswith('.json'):
continue

ll = os.path.basename(l.replace('.json','')).split('_')
if len(ll) < 3:
continue

c= ll[0]
cfg = '_'.join(ll[1:-1])
i = int(ll[-1].replace('i',''))

if (not c in self.components) or (not cfg in self.states[c]):
continue

if not 'instance_metrics' in self.states[c][cfg]:
self.states[c][cfg]['instance_metrics'] = {}

try:
p = pathlib.Path(l)
with p.open() as f:
t = f.read().strip()

self.states[c][cfg]['instance_metrics'][i] = json.loads(t)
self.states[c][cfg]['instance_metrics'][i]['status'] = { 'mtime':os.stat(p).st_mtime }
except:
logger.error( f"corrupt metrics file {pathname}: {t}" )


def _read_states(self):
self.states = {}
for c in self.components:
self.states[c] = {}

self._read_state_dir(self.user_cache_dir)
self._read_state_dir(self.user_cache_dir + os.sep + self.hostdir)
self._read_metrics_dir(self.user_cache_dir)
self._read_metrics_dir(self.user_cache_dir + os.sep + self.hostdir)

def _find_missing_instances_dir(self, dir):
""" find processes which are no longer running, based on pidfiles in state, and procs.
Expand Down

0 comments on commit 0f7437c

Please sign in to comment.