def run(batch, runs, cwd=None): """Submit a job array for the given runs """ txt_output = text_file_directory(batch) scripts = script_file_directory(batch) if not os.path.exists(txt_output): os.mkdir(txt_output) os.chmod(txt_output, 0777) if not os.path.exists(scripts): os.mkdir(scripts) os.chmod(scripts, 0755) with bpcursor() as cursor: bats = [] batch_array = BPBatchArray.create(cursor, batch) for idx, run in enumerate(runs): task_id = idx + TASK_ID_START bats.append( BPBatchArrayTask.create(cursor, batch_array, run, task_id)) script = "#!/bin/sh\n" # # A work-around if HOME has been defined differently on the host # script += """ if [ ! -z "$SGE_O_HOME" ]; then export HOME="$SGE_O_HOME" echo "Set home to $HOME" fi """ # # Source cpenv.sh # script += ". %s\n" % os.path.join(PREFIX, "bin", "cpenv.sh") if JS_USE_REST: # # This is a REST PUT to JobStatus.py to create the job record # data = "{" + (",".join([ '"%s":"%s"' % (k, v) for k, v in ((K_ACTION, A_UPDATE), (K_STATUS, JS_RUNNING)) ])) data += ',"%s":\'$JOB_ID\'' % JOB_ID data += ',"%s":%d' % (BATCH_ARRAY_ID, batch_array.batch_array_id) data += ',"%s":\'$SGE_TASK_ID\'' % TASK_ID data += ',"%s":"\'$HOSTNAME\'"}' % (K_HOST_NAME) script += """BATCHPROFILER_COMMAND=`curl -s """ script += """-H "Content-type: application/json" -X PUT """ script += """--data '%s' %s/JobStatus.py`\n""" % ( data, BATCHPROFILER_DEFAULTS[URL]) else: script += """ if [ -e $HOME/.batchprofiler.sh ]; then . $HOME/.batchprofiler.sh fi """ script += "BATCHPROFILER_COMMAND=" script += "`PYTHONPATH=%s:$PYTHONPATH " % os.path.dirname(__file__) script += "python -c 'from JobStatus import update_status;" script += "print update_status(%d, '$JOB_ID', '$SGE_TASK_ID', " % \ batch_array.batch_array_id script += "\"%s\", \"'$HOSTNAME'\")'`\n" % JS_RUNNING # # CD to the CellProfiler root directory # if cwd is None: cwd = batch.cpcluster script += 'cd %s\n' % cwd # # set +e allows the command to error-out without ending this script. # This lets us capture the error status. # script += "set +e\n" # # Run CellProfiler # script += "echo 'Running '$BATCHPROFILER_COMMAND' on '$HOSTNAME\n" script += "echo $BATCHPROFILER_COMMAND| bash\n" # # Figure out the status from the error code # script += "if [ $? == 0 ]; then\n" script += "JOB_STATUS=%s\n" % JS_DONE script += "else\n JOB_STATUS=%s\n " % JS_ERROR script += "fi\n" # # Go back to erroring-out # script += "set -e\n" if JS_USE_REST: # # Set the status based on the result from CellProfiler # Use CURL again # script += """curl -s -H "Content-type: application/json" -X PUT """ script += """--data '{"action":"update","%s":'$JOB_ID',""" % JOB_ID script += """"%s":%d,""" % (BATCH_ARRAY_ID, batch_array.batch_array_id) script += """"%s":'$SGE_TASK_ID',""" % TASK_ID script += """"%s":"'$JOB_STATUS'"}' """ % K_STATUS script += '%s/JobStatus.py\n' % BATCHPROFILER_DEFAULTS[URL] else: script += "PYTHONPATH=%s:$PYTHONPATH " % os.path.dirname(__file__) script += "python -c 'from JobStatus import update_status;" script += "print update_status(%d, '$JOB_ID', '$SGE_TASK_ID', " % \ batch_array.batch_array_id script += "\"'$JOB_STATUS'\", \"'$HOSTNAME'\")'\n" script_filename = batch_array_script_file_path(batch_array) with open(script_filename, "w") as fd: fd.write(script) os.chmod( script_filename, stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) priority = min(0, max(-1023, int(batch.priority * 1023 / 100 - 1023))) try: memory_limit = int(batch.memory_limit) / 1000 except: memory_limit = 2 job_id = bputilities.run_on_tgt_os( script=script, group_name=batch.project, job_name=batch_array.get_job_name(), queue_name=batch.queue, priority=priority, memory=memory_limit, cwd=cwd, output=batch_array_text_file_path(batch_array), err_output=batch_array_err_file_path(batch_array), task_range=slice(TASK_ID_START, TASK_ID_START + len(bats))) tasks = [] with bpcursor() as cursor: job = BPJob.create(cursor, batch_array, job_id) for bat in bats: task = BPJobTask.create(cursor, job, bat) tasks.append(task) BPJobTaskStatus.create(cursor, task, JS_SUBMITTED) return tasks