include changes from tmp branch

This commit is contained in:
l_samenv
2024-06-10 10:19:22 +02:00
parent 3d8f220182
commit e31d0248d7
4 changed files with 224 additions and 169 deletions

41
base.py
View File

@ -146,7 +146,7 @@ class ServiceManager:
nr = section.get(self.group)
if nr is not None:
nr = '%02d' % int(nr)
self.commands[ins] = command
self.commands[ins] = command.replace('~', expanduser('~'))
services = self.get_services(section)
env = {k: get_subs(section, k, ins, nr) for k in section if k.isupper()}
result[ins] = services
@ -156,8 +156,12 @@ class ServiceManager:
#def get_cmdpats(self, groups):
# return self.cmdpats
def get_ins_info(self, ins):
def get_ins_info(self, ins=None):
self.get_info()
if ins is None:
ins = os.environ.get('Instrument')
if ins is None:
return {}
return self.info[ins]
def get_cfg(self, ins, service):
@ -175,7 +179,7 @@ class ServiceManager:
def get_procs(self, groups=None, cfginfo=None):
"""return processes
:param groups: group to look for or None for all groups
:param groups: group to look for or None for own groups
:param cfginfo: cfginfo dict to be populated
:result: a dict[ins] of dict[service] of list of tuples (process, cfg)
"""
@ -211,6 +215,10 @@ class ServiceManager:
return result
def wildcard(self, ins):
"""return a list of matching instruments
or None, when no wildcard character in ins
"""
if ins is None or ins == 'all':
return list(self.info)
pat = re.sub(r'(\.|\*)', '.*', ins)
@ -248,13 +256,16 @@ class ServiceManager:
continue # already killed
for i in range(10): # total 0.1 * 10 * 9 / 2 = 4.5 sec
try:
p.wait(0.1 * i)
except psutil.TimeoutExpired:
if p.status() != psutil.STATUS_ZOMBIE:
if print_wait and i > 4:
print('wait for %s %s' % (ins, service))
print_wait = False
continue
try:
p.wait(0.1 * i)
except psutil.TimeoutExpired:
if p.status() != psutil.STATUS_ZOMBIE:
if print_wait and i > 4:
print('wait for %s %s' % (ins, service))
print_wait = False
continue
except psutil.NoSuchProcess:
pass # process stopped in the meantime
self.stopped[ins][service] = ' '.join(p.info['cmdline'])
break
else:
@ -325,9 +336,10 @@ class ServiceManager:
services = to_start
for service_i in services:
port = service_ports[service_i]
cmd = self.commands[ins] % dict(ins=ins, serv=service_i, port=port, cfg=cfg, pkg=self.pkg)
cmd = self.commands[ins] % dict(ins=ins, serv=service_i, port=port, cfg=cfg, pkg=self.pkg or ins)
if opts:
cmd = f'{cmd} {opts}'
print(cmd)
if '%(cfg)s' in self.commands[ins] and not cfg:
cmd = self.stopped[ins].get(service_i)
if not cmd:
@ -337,7 +349,7 @@ class ServiceManager:
wd = os.getcwd()
try:
start_dir, env = self.prepare_start(ins, service_i, cfg)
env = dict(os.environ, **env)
env = dict(os.environ, **env, Instrument=ins)
os.chdir(start_dir)
if start_dir not in sys.path:
sys.path.insert(0, start_dir)
@ -414,8 +426,7 @@ class ServiceManager:
ins = None
instances = self.wildcard(ins)
if instances is None:
# ins_set = {ins}
ins_set = set(self.info)
ins_set = {ins}
else:
ins_set = set(instances)
cfginfo = {}
@ -441,7 +452,7 @@ class ServiceManager:
plist = procs_dict.get(serv)
if plist:
cfg = cfginfo.get((ins_i, serv), '') or sm.get_cfg(ins_i, serv)
if sm == self:
if sm == self or instances is None:
show_ins = True
gs = '%s %s' % (group, serv)
port = str(port or '')