Async module, mostly operational, daemonizing/watch code may have bugs

This commit is contained in:
Michael DeHaan 2012-03-03 13:19:33 -05:00
parent 718e2930b2
commit 8e07d83ad1

View file

@ -28,6 +28,8 @@ import subprocess
import sys import sys
import datetime import datetime
import traceback import traceback
import signal
import time
if len(sys.argv) < 3: if len(sys.argv) < 3:
print json.dumps({ print json.dumps({
@ -66,7 +68,6 @@ def _run_command(wrapped_cmd, jid, log_path):
try: try:
cmd = shlex.split(wrapped_cmd) cmd = shlex.split(wrapped_cmd)
subprocess.call("/usr/bin/logger %s" % wrapped_cmd, shell=True)
script = subprocess.Popen(cmd, shell=False, script = subprocess.Popen(cmd, shell=False,
stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = script.communicate() out, err = script.communicate()
@ -90,28 +91,36 @@ def _run_command(wrapped_cmd, jid, log_path):
logfile.write(json.dumps(result)) logfile.write(json.dumps(result))
logfile.close() logfile.close()
# TODO: daemonize this with time limits # immediately exit this process, leaving an orphaned process
# TODO: might be nice to keep timing data, eventually... # running which immediately forks a supervisory timing process
pid = os.fork() pid = os.fork()
if pid == 0: if pid == 0:
print "RETURNING SUCCESS IN UNO"
print json.dumps({ "started" : 1, "ansible_job_id" : jid }) print json.dumps({ "started" : 1, "ansible_job_id" : jid })
sys.exit(0) sys.exit(0)
else: else:
print "DAEMONIZED DOS"
# FIXME: need to implement time limits sub_pid = os.fork()
# probably something easy like: if sub_pid == 0:
# print "RUNNING IN KID A"
# sub_pid = os.fork()
# if sub_pid == 0:
# run command
# else
# check status
# sleep 1 second
# kill if greater than timelimit
_run_command(cmd, jid, log_path) _run_command(cmd, jid, log_path)
sys.exit(0) sys.exit(0)
else:
print "WATCHING IN KID B"
remaining = int(time_limit)
if os.path.exists("/proc/%s" % sub_pid):
print "STILL RUNNING"
time.sleep(1)
remaining = remaining - 1
else:
print "DONE IN KID B"
sys.exit(0)
if remaining == 0:
print "SLAYING IN KID B"
os.kill(sub_pid, signals.SIGKILL)
sys.exit(1)
sys.exit(0)