Commit 3fa8ee35 authored by Pavel Vainerman's avatar Pavel Vainerman

added option '--run-after' ('-a') for run programs after main process terminated

parent 874ead4e
Pipeline #1258 failed with stage
......@@ -18,6 +18,7 @@ Help
-d | --run-from-dir dir - run programs from directory
-f | --run-from-file file - run programs from file
-r | --run '[params]prog args..' - run programs from command line"
-a | --run-after 'prog args..' - run programs after main process terminated"
-p | --monitor-pid pid - pid of main process (for monitoring)
-v | --verbose - Print info messages
-V | --version - Version info
......@@ -90,3 +91,10 @@ Example 4
---------
prunner -p PID -d ./child.d -f runlist.txt -r '[restart=2] prog1 arg1 arg2'
Т.е. можно указывать и каталог и файл и -r одновременно
Example 5
---------
prunner -p PID -d ./child.d --run-after 'prog_after_term arg1 arg2'
В этом примере запустяться программы из каталога *child.d*, а после того
как процесс PID завершится, запустится программа *'prog_after_term'*
Параметр *--run-after* или *-a* можно указывать многократно.
......@@ -188,15 +188,22 @@ def do_check_alive(childs):
term_check_alive.wait(check_alive_period)
def do_monitoring(main_pid, run_list, not_monit):
def do_run_after(run_list):
for p in run_list:
p.run()
def do_monitoring(main_pid, run_list, not_monit, run_after_list):
"""
Running processes and monitoring
:param main_pid: pid for monitoring
:param run_list: list of process for run
:param not_monit: True - disable monitoring (only run process)
:param run_after: True - run programs after main process terminated
:return: None
"""
proc_list = list()
interrupted = False
for p in run_list:
if p.run():
proc_list.append(p)
......@@ -204,13 +211,18 @@ def do_monitoring(main_pid, run_list, not_monit):
terminate_all_process(proc_list)
exit(1)
check_alive_thread = threading.Thread(target=do_check_alive, args=(proc_list,))
check_alive_thread = None
if len(proc_list) > 0:
check_alive_thread = threading.Thread(target=do_check_alive, args=(proc_list,))
try:
if not_monit:
return
term_check_alive.clear()
check_alive_thread.start()
if check_alive_thread:
check_alive_thread.start()
monit_process = psutil.Process(main_pid)
while not term_check_alive.is_set():
try:
......@@ -220,14 +232,19 @@ def do_monitoring(main_pid, run_list, not_monit):
pass
except (KeyboardInterrupt, SystemExit):
pass
interrupted = True
finally:
term_check_alive.set()
if check_alive_thread.is_alive():
if check_alive_thread and check_alive_thread.is_alive():
check_alive_thread.join()
terminate_all_process(proc_list)
# run programs after teminated
if not interrupted and len(run_after_list) > 0:
log_info("run programms after terminated main process...")
do_run_after(run_after_list)
def terminate_all_process(proc_list):
"""
......@@ -357,10 +374,11 @@ def read_from_commandline(param):
def usage():
print "[-p|--monitor-pid] pid - pid of main process (for monitoring)"
print "[-d|--run-from-dir] dir - run programs from directory"
print "[-f|--run-from-file] file - run programs from file"
print "[-r|--run] '[params]prog args..' - run programs from command line"
print "[-p|--monitor-pid] pid - pid of main process (for monitoring)"
print "[-a|--run-after] 'prog args..' - run programs after main process terminated"
print "[-v|--verbose] - Print info messages"
print "[-V|--version] - Version info"
print "[-c|--check-period] sec - period for check processes. Default: %s" % check_alive_period
......@@ -405,9 +423,10 @@ if __name__ == "__main__":
run_list = run_list + read_from_dir(from_dir)
run_list = run_list + read_from_commandline(['-r', '--run'])
run_after_list = read_from_commandline(['-a', '--run-after'])
if len(run_list) == 0:
if len(run_list) == 0 and len(run_after_list) == 0:
log_error("not found run list. Use -h for help")
exit(1)
do_monitoring(main_pid, run_list, not_monit=not_monit)
do_monitoring(main_pid=main_pid, run_list=run_list, not_monit=not_monit, run_after_list=run_after_list)
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