|
|
@ -128,52 +128,68 @@ def _run_command(wrapped_cmd, jid, log_path):
|
|
|
|
# immediately exit this process, leaving an orphaned process
|
|
|
|
# immediately exit this process, leaving an orphaned process
|
|
|
|
# running which immediately forks a supervisory timing process
|
|
|
|
# running which immediately forks a supervisory timing process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#import logging
|
|
|
|
|
|
|
|
#import logging.handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#logger = logging.getLogger("ansible_async")
|
|
|
|
|
|
|
|
#logger.setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
#logger.addHandler( logging.handlers.SysLogHandler("/dev/log") )
|
|
|
|
|
|
|
|
def debug(msg):
|
|
|
|
|
|
|
|
#logger.warning(msg)
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
pid = os.fork()
|
|
|
|
pid = os.fork()
|
|
|
|
if pid != 0:
|
|
|
|
if pid:
|
|
|
|
# the parent indicates the job has started
|
|
|
|
# Notify the overlord that the async process started
|
|
|
|
# print "RETURNING SUCCESS IN PARENT"
|
|
|
|
|
|
|
|
print json.dumps({ "started" : 1, "ansible_job_id" : jid, "results_file" : log_path })
|
|
|
|
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
# we need to not return immmediately such that the launched command has an attempt
|
|
|
|
# we need to not return immmediately such that the launched command has an attempt
|
|
|
|
# to initialize PRIOR to ansible trying to clean up the launch directory (and argsfile)
|
|
|
|
# to initialize PRIOR to ansible trying to clean up the launch directory (and argsfile)
|
|
|
|
# this probably could be done with some IPC later. Modules should always read
|
|
|
|
# this probably could be done with some IPC later. Modules should always read
|
|
|
|
# the argsfile at the very first start of their execution anyway
|
|
|
|
# the argsfile at the very first start of their execution anyway
|
|
|
|
time.sleep(1)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
debug("Return async_wrapper task started.")
|
|
|
|
|
|
|
|
print json.dumps({ "started" : 1, "ansible_job_id" : jid, "results_file" : log_path })
|
|
|
|
|
|
|
|
sys.stdout.flush()
|
|
|
|
sys.exit(0)
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# the kid manages the job
|
|
|
|
# The actual wrapper process
|
|
|
|
# WARNING: the following call may be total overkill
|
|
|
|
|
|
|
|
|
|
|
|
# Daemonize, so we keep on running
|
|
|
|
daemonize_self()
|
|
|
|
daemonize_self()
|
|
|
|
|
|
|
|
|
|
|
|
# we are now daemonized in this other fork but still
|
|
|
|
# we are now daemonized, create a supervisory process
|
|
|
|
# want to create a supervisory process
|
|
|
|
debug("Starting module and watcher")
|
|
|
|
|
|
|
|
|
|
|
|
#print "DAEMONIZED KID MAKING MORE KIDS"
|
|
|
|
|
|
|
|
sub_pid = os.fork()
|
|
|
|
sub_pid = os.fork()
|
|
|
|
if sub_pid == 0:
|
|
|
|
if sub_pid:
|
|
|
|
#print "RUNNING IN KID A"
|
|
|
|
# the parent stops the process after the time limit
|
|
|
|
_run_command(cmd, jid, log_path)
|
|
|
|
|
|
|
|
#print "KID A COMPLETE"
|
|
|
|
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
#print "WATCHING IN KID B"
|
|
|
|
|
|
|
|
remaining = int(time_limit)
|
|
|
|
remaining = int(time_limit)
|
|
|
|
if os.path.exists("/proc/%s" % sub_pid):
|
|
|
|
|
|
|
|
#print "STILL RUNNING"
|
|
|
|
# set the child process group id to kill all children
|
|
|
|
|
|
|
|
os.setpgid(sub_pid, sub_pid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
debug("Start watching %s (%s)"%(sub_pid, remaining))
|
|
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
while os.waitpid(sub_pid, os.WNOHANG) == (0, 0):
|
|
|
|
|
|
|
|
debug("%s still running (%s)"%(sub_pid, remaining))
|
|
|
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
remaining = remaining - 5
|
|
|
|
|
|
|
|
if remaining == 0:
|
|
|
|
|
|
|
|
debug("Now killing %s"%(sub_pid))
|
|
|
|
|
|
|
|
os.killpg(sub_pid, signal.SIGKILL)
|
|
|
|
|
|
|
|
debug("Sent kill to group %s"%sub_pid)
|
|
|
|
time.sleep(1)
|
|
|
|
time.sleep(1)
|
|
|
|
remaining = remaining - 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
#print "DONE IN KID B"
|
|
|
|
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
sys.exit(0)
|
|
|
|
if remaining == 0:
|
|
|
|
debug("Done in kid B.")
|
|
|
|
#print "SLAYING IN KID B"
|
|
|
|
os._exit(0)
|
|
|
|
os.kill(sub_pid, signals.SIGKILL)
|
|
|
|
else:
|
|
|
|
sys.stdout.flush()
|
|
|
|
# the child process runs the actual module
|
|
|
|
sys.exit(1)
|
|
|
|
debug("Start module (%s)"%os.getpid())
|
|
|
|
|
|
|
|
_run_command(cmd, jid, log_path)
|
|
|
|
sys.stdout.flush()
|
|
|
|
debug("Module complete (%s)"%os.getpid())
|
|
|
|
sys.exit(0)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception, err:
|
|
|
|
|
|
|
|
debug("error: %s"%(err))
|
|
|
|
|
|
|
|
raise err
|
|
|
|