add logger argument to ServiceManager.restart

+ replace MAIN instrument by the name of home directory
This commit is contained in:
zolliker 2021-03-09 18:03:13 +01:00
parent 10c01a7fdd
commit adbe97361f

47
base.py
View File

@ -24,7 +24,7 @@
import sys import sys
import json import json
import os import os
from os.path import expanduser from os.path import expanduser, basename
import subprocess import subprocess
import time import time
import re import re
@ -135,6 +135,8 @@ class ServiceManager:
for ins in parser.sections(): for ins in parser.sections():
section = dict(parser[ins]) section = dict(parser[ins])
if ins == 'MAIN':
ins = basename(expanduser('~'))
command = section.get('%s_command' % self.group) command = section.get('%s_command' % self.group)
self.revcmd[command] = self.group self.revcmd[command] = self.group
nr = section.get(self.group) nr = section.get(self.group)
@ -252,16 +254,26 @@ class ServiceManager:
env = self.env[ins] env = self.env[ins]
return env.get('%s_ROOT' % gr, ''), env return env.get('%s_ROOT' % gr, ''), env
def do_start(self, ins, service=None, cfg='', restart=False, wait=False): def do_start(self, ins, service=None, cfg='', restart=False, wait=False, logger=None):
if logger is None:
class logger:
@staticmethod
def info(fmt, *args):
print(fmt % args)
@staticmethod
def error(fmt, *args):
print(('ERROR: ' + fmt) % args)
if ins is None: if ins is None:
print('nothing to start') logger.info('nothing to start')
return return
try: try:
service_ports = self.get_ins_info(ins) service_ports = self.get_ins_info(ins)
except ValueError: except ValueError:
raise ValueError('do not know %r' % ins) raise ValueError('do not know %r' % ins)
services = list(service_ports) if service is None else [service] services = list(service_ports) if service is None else [service]
print('start', services, service_ports) # logger.info('start %r %r', services, service_ports)
if restart: if restart:
self.stop(ins, service) self.stop(ins, service)
else: else:
@ -273,12 +285,12 @@ class ServiceManager:
to_start.append(service) to_start.append(service)
else: else:
count = '' if n == 1 else ' %sx' % n count = '' if n == 1 else ' %sx' % n
print('%s %s is already running%s' % (ins, service, count)) logger.info('%s %s is already running%s', ins, service, count)
services = to_start services = to_start
for service_i in services: for service_i in services:
port = service_ports[service_i] 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)
print('COMMAND', cmd) logger.info('COMMAND %s', cmd)
if '%(cfg)s' in self.commands[ins] and not cfg: if '%(cfg)s' in self.commands[ins] and not cfg:
cmd = self.stopped[ins].get(service_i) cmd = self.stopped[ins].get(service_i)
if not cmd: if not cmd:
@ -296,14 +308,14 @@ class ServiceManager:
return return
process = subprocess.Popen(cmd.split(), env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) process = subprocess.Popen(cmd.split(), env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if not port: if not port:
print('%s %s started' % (ins, service_i)) logger.info('%s %s started', ins, service_i)
continue continue
print_wait = True print_wait = True
for i in range(10): # total 10 * 9 / 2 = 4.5 sec for i in range(10): # total 10 * 9 / 2 * 0.1 = 4.5 sec
returnvalue = process.poll() returnvalue = process.poll()
if returnvalue is not None: if returnvalue is not None:
print('started process finished with %r' % returnvalue) logger.info('started process failed, try to find out why')
process = subprocess.Popen(cmd.split(), env=env, stdout=subprocess.DEVNULL, process = subprocess.Popen(cmd.split(), env=env, stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
try: try:
@ -311,12 +323,11 @@ class ServiceManager:
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
process.kill() process.kill()
_, erroutput = process.communicate() _, erroutput = process.communicate()
print(erroutput.decode()) logger.error('%s %s died\n%s', ins, service_i, erroutput.decode())
print('%s %s died' % (ins, service_i))
break break
try: try:
if print_wait and i > 4: if print_wait and i > 4:
print('wait for port %s' % port) logger.info('wait for port %s', port)
print_wait = False print_wait = False
s = socket.create_connection(('localhost', port), timeout=5) s = socket.create_connection(('localhost', port), timeout=5)
s.close() s.close()
@ -324,18 +335,18 @@ class ServiceManager:
time.sleep(0.1 * i) time.sleep(0.1 * i)
continue continue
if restart: if restart:
print('%s %s restarted' % (ins, service_i)) logger.info('%s %s restarted', ins, service_i)
else: else:
print('%s %s started' % (ins, service_i)) logger.info('%s %s started', ins, service_i)
break break
else: else:
print(cmd) logger.info(cmd)
print('starting %s %s failed' % (ins, service_i)) logger.error('starting %s %s failed', ins, service_i)
finally: finally:
os.chdir(wd) os.chdir(wd)
def do_restart(self, ins, service=None, cfg=None): def do_restart(self, ins, service=None, cfg=None, logger=None):
self.do_start(ins, service, cfg, True) self.do_start(ins, service, cfg, True, logger=logger)
def do_run(self, ins, service=None, cfg=None): def do_run(self, ins, service=None, cfg=None):
"""for tests: run and wait""" """for tests: run and wait"""