From 6632bef17dd6cc8c74a1bb25bba102d7e00df0b8 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 3 Mar 2012 13:06:43 -0500 Subject: [PATCH] async module now forks --- async_wrapper | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/async_wrapper b/async_wrapper index 2b663e9891e..f8fd72c1a33 100755 --- a/async_wrapper +++ b/async_wrapper @@ -36,9 +36,11 @@ if len(sys.argv) < 3: }) sys.exit(1) +print sys.argv + jid = sys.argv[1] -wrapped_module = sys.argv[2] -time_limit = sys.argv[3] +time_limit = sys.argv[2] +wrapped_module = sys.argv[3] args = sys.argv[4:] cmd = "%s %s" % (wrapped_module, " ".join(args)) @@ -73,11 +75,13 @@ def _run_command(wrapped_cmd, jid, log_path): except (OSError, IOError), e: result = { "failed": 1, + "cmd" : wrapped_cmd, "msg": str(e), } except: result = { "failed" : 1, + "cmd" : wrapped_cmd, "msg" : traceback.format_exc() } @@ -86,12 +90,28 @@ def _run_command(wrapped_cmd, jid, log_path): logfile.write(json.dumps(result)) logfile.close() - # TEMPORARY: - print json.dumps(result) - # TODO: daemonize this with time limits # TODO: might be nice to keep timing data, eventually... -_run_command(cmd, jid, log_path) + +pid = os.fork() +if pid == 0: + print json.dumps({ "started" : 1, "ansible_job_id" : jid }) + sys.exit(0) +else: + + # FIXME: need to implement time limits + # probably something easy like: + # + # 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) + sys.exit(0)