add logger argument to ServiceManager.restart
+ replace MAIN instrument by the name of home directory
This commit is contained in:
parent
10c01a7fdd
commit
adbe97361f
47
base.py
47
base.py
@ -24,7 +24,7 @@
|
||||
import sys
|
||||
import json
|
||||
import os
|
||||
from os.path import expanduser
|
||||
from os.path import expanduser, basename
|
||||
import subprocess
|
||||
import time
|
||||
import re
|
||||
@ -135,6 +135,8 @@ class ServiceManager:
|
||||
|
||||
for ins in parser.sections():
|
||||
section = dict(parser[ins])
|
||||
if ins == 'MAIN':
|
||||
ins = basename(expanduser('~'))
|
||||
command = section.get('%s_command' % self.group)
|
||||
self.revcmd[command] = self.group
|
||||
nr = section.get(self.group)
|
||||
@ -252,16 +254,26 @@ class ServiceManager:
|
||||
env = self.env[ins]
|
||||
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:
|
||||
print('nothing to start')
|
||||
logger.info('nothing to start')
|
||||
return
|
||||
try:
|
||||
service_ports = self.get_ins_info(ins)
|
||||
except ValueError:
|
||||
raise ValueError('do not know %r' % ins)
|
||||
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:
|
||||
self.stop(ins, service)
|
||||
else:
|
||||
@ -273,12 +285,12 @@ class ServiceManager:
|
||||
to_start.append(service)
|
||||
else:
|
||||
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
|
||||
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)
|
||||
print('COMMAND', cmd)
|
||||
logger.info('COMMAND %s', cmd)
|
||||
if '%(cfg)s' in self.commands[ins] and not cfg:
|
||||
cmd = self.stopped[ins].get(service_i)
|
||||
if not cmd:
|
||||
@ -296,14 +308,14 @@ class ServiceManager:
|
||||
return
|
||||
process = subprocess.Popen(cmd.split(), env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
if not port:
|
||||
print('%s %s started' % (ins, service_i))
|
||||
logger.info('%s %s started', ins, service_i)
|
||||
continue
|
||||
|
||||
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()
|
||||
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,
|
||||
stderr=subprocess.PIPE)
|
||||
try:
|
||||
@ -311,12 +323,11 @@ class ServiceManager:
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill()
|
||||
_, erroutput = process.communicate()
|
||||
print(erroutput.decode())
|
||||
print('%s %s died' % (ins, service_i))
|
||||
logger.error('%s %s died\n%s', ins, service_i, erroutput.decode())
|
||||
break
|
||||
try:
|
||||
if print_wait and i > 4:
|
||||
print('wait for port %s' % port)
|
||||
logger.info('wait for port %s', port)
|
||||
print_wait = False
|
||||
s = socket.create_connection(('localhost', port), timeout=5)
|
||||
s.close()
|
||||
@ -324,18 +335,18 @@ class ServiceManager:
|
||||
time.sleep(0.1 * i)
|
||||
continue
|
||||
if restart:
|
||||
print('%s %s restarted' % (ins, service_i))
|
||||
logger.info('%s %s restarted', ins, service_i)
|
||||
else:
|
||||
print('%s %s started' % (ins, service_i))
|
||||
logger.info('%s %s started', ins, service_i)
|
||||
break
|
||||
else:
|
||||
print(cmd)
|
||||
print('starting %s %s failed' % (ins, service_i))
|
||||
logger.info(cmd)
|
||||
logger.error('starting %s %s failed', ins, service_i)
|
||||
finally:
|
||||
os.chdir(wd)
|
||||
|
||||
def do_restart(self, ins, service=None, cfg=None):
|
||||
self.do_start(ins, service, cfg, True)
|
||||
def do_restart(self, ins, service=None, cfg=None, logger=None):
|
||||
self.do_start(ins, service, cfg, True, logger=logger)
|
||||
|
||||
def do_run(self, ins, service=None, cfg=None):
|
||||
"""for tests: run and wait"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user