11 import arvados.commands.put as put
16 t = arvados.current_task().tmpdir
18 api = arvados.api('v1')
20 os.chdir(arvados.current_task().tmpdir)
26 if len(arvados.current_task()['parameters']) > 0:
27 p = arvados.current_task()['parameters']
29 p = arvados.current_job()['script_parameters']
34 r = os.path.basename(v)
35 os.symlink(os.path.join(os.environ['TASK_KEEPMOUNT'], v) , r)
40 return os.path.join(arvados.current_task().tmpdir, 'tmpdir')
43 return os.environ['CRUNCH_NODE_SLOTS']
45 subst.default_subs["link "] = sub_link
46 subst.default_subs["tmpdir"] = sub_tmpdir
47 subst.default_subs["node.cores"] = sub_cores
51 def machine_progress(bytes_written, bytes_expected):
52 return "run-command: {} written {} total\n".format(
53 bytes_written, -1 if (bytes_expected is None) else bytes_expected)
55 class SigHandler(object):
59 def send_signal(self, sp, signum):
60 sp.send_signal(signum)
62 print("We get signal! %s" % signum)
66 for c in p["command"]:
67 cmd.append(subst.do_substitution(p, c))
72 stdoutname = subst.do_substitution(p, p["stdout"])
73 stdoutfile = open(stdoutname, "wb")
75 print("run-command: {}{}".format(' '.join(cmd), (" > " + stdoutname) if stdoutname != None else ""))
77 sp = subprocess.Popen(cmd, shell=False, stdout=stdoutfile)
80 # forward signals to the process.
81 signal.signal(signal.SIGINT, lambda signum, frame: sig.send_signal(sp, signum))
82 signal.signal(signal.SIGTERM, lambda signum, frame: sig.send_signal(sp, signum))
83 signal.signal(signal.SIGQUIT, lambda signum, frame: sig.send_signal(sp, signum))
85 # wait for process to complete.
88 print("Sig is %s" % sig)
89 print("Sig.sig is %s" % sig.sig)
92 print("run-command: terminating on signal %s" % sig.sig)
95 print("run-command: completed with exit code %i (%s)" % (rcode, "success" if rcode == 0 else "failed"))
97 except Exception as e:
98 print("run-command: caught exception: {}".format(e))
101 # restore default signal handlers.
102 signal.signal(signal.SIGINT, signal.SIG_DFL)
103 signal.signal(signal.SIGTERM, signal.SIG_DFL)
104 signal.signal(signal.SIGQUIT, signal.SIG_DFL)
109 print("run-command: the follow output files will be saved to keep:")
111 subprocess.call(["find", ".", "-type", "f", "-printf", "run-command: %12.12s %h/%f\\n"])
113 print("run-command: start writing output to keep")
116 resume_cache = put.ResumeCache(os.path.join(arvados.current_task().tmpdir, "upload-output-checkpoint"))
117 reporter = put.progress_writer(machine_progress)
118 bytes_expected = put.expected_bytes_for(".")
121 out = put.ArvPutCollectionWriter(resume_cache, reporter, bytes_expected)
123 out.write_directory_tree(".", max_manifest_depth=0)
124 outuuid = out.finish()
125 api.job_tasks().update(uuid=arvados.current_task()['uuid'],
128 'success': (rcode == 0),
132 except KeyboardInterrupt:
133 print("run-command: terminating on signal SIGINT")
135 except Exception as e:
136 print("run-command: caught exception: {}".format(e))