This commit is contained in:
2022-01-20 14:19:20 +00:00
parent 82664c1d9a
commit 52199cbbd4

View File

@@ -8,25 +8,29 @@ log = logging.getLogger(__name__)
def __is_process(cmd:str, arr): def __is_process(cmd:str, arr):
item = " ".join(arr) item = " ".join(arr)
log.info("[onlyone][startprocess] -> " + item + " ends with " + cmd) #log.info("[onlyone][startprocess] -> " + item + " ends with " + cmd)
if(item.endswith(cmd)): if(item.endswith(cmd)):
return True return True
log.info("[onlyone][startprocess] -> " + item + " ends with " + cmd.replace("\"", "")) #log.info("[onlyone][startprocess] -> " + item + " ends with " + cmd.replace("\"", ""))
if(item.endswith(cmd.replace("\"", ""))): if(item.endswith(cmd.replace("\"", ""))):
return True return True
log.info("[onlyone][startprocess] -> " + item + " eq " + cmd) #log.info("[onlyone][startprocess] -> " + item + " eq " + cmd)
if(item == cmd): if(item == cmd):
return True return True
log.info("[onlyone][startprocess] -> " + item + " eq " + cmd.replace("\"", "")) #log.info("[onlyone][startprocess] -> " + item + " eq " + cmd.replace("\"", ""))
if(item == cmd.replace("\"", "")): if(item == cmd.replace("\"", "")):
return True return True
log.info("compare finished not equal") #log.info("compare finished not equal")
return False return False
def isrunning(cmd:str): def isrunning(cmd:str):
for proc in psutil.process_iter(): return __is_process(cmd, psutil.process_iter())
def __isrunning(cmd:str, processes):
for proc in processes:
if __is_process(cmd,proc.cmdline()): if __is_process(cmd,proc.cmdline()):
return True return True
return False return False
@@ -46,8 +50,12 @@ def __killprocesses(killcmds:List, sysprocesses: List):
return False return False
for proc in sysprocesses: for proc in sysprocesses:
if any( __is_process(x, proc.cmdline()) for x in killcmds): if any( __is_process(x, proc.cmdline()) for x in killcmds):
proc.kill() try:
log.info("[onlyone][__killprocesses] -> processed killed : " + proc.name()) log.info("[onlyone][__killprocesses] -> processed killed : " + proc.name())
except:
log.info("[onlyone][__killprocesses] -> error killing process")
return False return False
@@ -55,26 +63,28 @@ def killprocesses(killcmds:List):
return __killprocesses(killcmds, psutil.process_iter()) return __killprocesses(killcmds, psutil.process_iter())
def __forceoneprocessinstance(cmd:str, processes:List): # def __forceoneprocessinstance(cmd:str, processes:List):
found = False # found = False
for proc in processes: # for proc in processes:
if __is_process(cmd, proc.cmdline()): # if __is_process(cmd, proc.cmdline()):
if found: # if found:
proc.kill() # proc.kill()
log.info("[onlyone][__forceoneprocessinstance] -> processed killed : " + proc.name + "(" + proc.id + ")") # log.info("[onlyone][__forceoneprocessinstance] -> processed killed : " + proc.name + "(" + proc.id + ")")
else : # else :
found = True # found = True
if not found: # if not found:
startprocess(cmd) # startprocess(cmd)
def forceoneprocessinstance(cmd:str): # def forceoneprocessinstance(cmd:str):
return __forceoneprocessinstance(cmd, psutil.process_iter()) # return __forceoneprocessinstance(cmd, psutil.process_iter())
def onlyone(cmd:str, killcmds:list): def onlyone(cmd:str, killcmds:list):
log.info("[onlyone][onlyone] -> invoked start " + cmd + ", kill " + str(killcmds)) log.info("[onlyone][onlyone] -> invoked start " + cmd + ", kill " + str(killcmds))
processes = psutil.process_iter() processes = psutil.process_iter()
__killprocesses(killcmds, processes) __killprocesses(killcmds, processes)
__forceoneprocessinstance(cmd, processes) if (not(__isrunning(cmd, processes))):
startprocess(cmd)
#__forceoneprocessinstance(cmd, processes)
class Manager: class Manager:
def __init__(self): def __init__(self):