Commit 331216b6 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

Introducing a general killer removing pidfiles after killing everychild.

 Slapgrid now use standard pidfile.
 StopProxy Kill it.
parent c26eb8c3
...@@ -187,6 +187,39 @@ def writePid(file, pid): ...@@ -187,6 +187,39 @@ def writePid(file, pid):
"""Save process pid into a file `file`""" """Save process pid into a file `file`"""
open(file, 'w').write(str(pid)) open(file, 'w').write(str(pid))
def killRunningProcess(config, ptype):
"""Kill process and all running children process and remove pidfile"""
process_pid = os.path.join(config['run_dir'], ptype)
pid = readPid(process_pid)
if pid:
recursifKill([pid])
os.remove(process_pid)
else:
return False
def recursifKill(pids):
"""Try to kill a list of proccess by the given pid list"""
if pids == []:
return
else:
for pid in pids:
ppids = pidppid(pid)
try:
os.kill(pid, signal.SIGKILL) #kill current process
except Exception:
pass
recursifKill(ppids) #kill all children of this process
def pidppid(pid):
"""get the list of the children pids of a process `pid`"""
proc = Popen('ps -o pid,ppid ax | grep "%d"' % pid, shell=True,
stdout=subprocess.PIPE)
ppid = [x.split() for x in proc.communicate()[0].split("\n") if x]
return list(int(p) for p, pp in ppid if int(pp) == pid)
def updateInstanceParameter(config, software_type=None): def updateInstanceParameter(config, software_type=None):
""" """
Reconfigure Slapproxy to re-deploy current Software Instance with parameters. Reconfigure Slapproxy to re-deploy current Software Instance with parameters.
...@@ -220,6 +253,7 @@ def startProxy(config): ...@@ -220,6 +253,7 @@ def startProxy(config):
def stopProxy(config): def stopProxy(config):
"""Stop Slapproxy server""" """Stop Slapproxy server"""
killRunningProcess(config,'proxy.pid')
pass pass
...@@ -266,9 +300,9 @@ def runSoftwareWithLock(config): ...@@ -266,9 +300,9 @@ def runSoftwareWithLock(config):
environment = os.environ.copy() environment = os.environ.copy()
environment['MAKEFLAGS'] = '-j%r' % multiprocessing.cpu_count() environment['MAKEFLAGS'] = '-j%r' % multiprocessing.cpu_count()
slapgrid = Popen([config['slapgrid_sr'], '-vc', slapgrid = Popen([config['slapgrid_sr'], '-vc',
'--pidfile',slapgrid_pid,
config['configuration_file_path'], '--now', '--develop'], config['configuration_file_path'], '--now', '--develop'],
stdout=logfile, env=environment) stdout=logfile, env=environment)
writePid(slapgrid_pid, slapgrid.pid)
slapgrid.wait() slapgrid.wait()
#Saves the current compile software for re-use #Saves the current compile software for re-use
config_SR_folder(config) config_SR_folder(config)
...@@ -341,34 +375,6 @@ def isInstanceRunning(config): ...@@ -341,34 +375,6 @@ def isInstanceRunning(config):
running = False running = False
return running return running
def killRunningSlapgrid(config, ptype):
"""Kill slapgrid process and all running children process"""
slapgrid_pid = os.path.join(config['run_dir'], ptype)
pid = readPid(slapgrid_pid)
if pid:
recursifKill([pid])
else:
return False
def recursifKill(pids):
"""Try to kill a list of proccess by the given pid list"""
if pids == []:
return
else:
for pid in pids:
ppids = pidppid(pid)
try:
os.kill(pid, signal.SIGKILL) #kill current process
except Exception:
pass
recursifKill(ppids) #kill all children of this process
def pidppid(pid):
"""get the list of the children pids of a process `pid`"""
proc = Popen('ps -o pid,ppid ax | grep "%d"' % pid, shell=True,
stdout=subprocess.PIPE)
ppid = [x.split() for x in proc.communicate()[0].split("\n") if x]
return list(int(p) for p, pp in ppid if int(pp) == pid)
def runInstanceWithLock(config): def runInstanceWithLock(config):
""" """
...@@ -383,9 +389,9 @@ def runInstanceWithLock(config): ...@@ -383,9 +389,9 @@ def runInstanceWithLock(config):
return False return False
svcStopAll(config) #prevent lost control of process svcStopAll(config) #prevent lost control of process
slapgrid = Popen([config['slapgrid_cp'], '-vc', slapgrid = Popen([config['slapgrid_cp'], '-vc',
'--pidfile',slapgrid_pid,
config['configuration_file_path'], '--now'], config['configuration_file_path'], '--now'],
stdout=logfile) stdout=logfile)
writePid(slapgrid_pid, slapgrid.pid)
slapgrid.wait() slapgrid.wait()
return True return True
return False return False
...@@ -578,9 +584,9 @@ def configNewSR(config, projectpath): ...@@ -578,9 +584,9 @@ def configNewSR(config, projectpath):
folder = realpath(config, projectpath) folder = realpath(config, projectpath)
if folder: if folder:
if isInstanceRunning(config): if isInstanceRunning(config):
killRunningSlapgrid(config, "slapgrid-cp.pid") killRunningProcess(config, "slapgrid-cp.pid")
if isSoftwareRunning(config): if isSoftwareRunning(config):
killRunningSlapgrid(config, "slapgrid-sr.pid") killRunningProcess(config, "slapgrid-sr.pid")
stopProxy(config) stopProxy(config)
removeProxyDb(config) removeProxyDb(config)
startProxy(config) startProxy(config)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment