def createRetryRun(self, reductionRun, script=None, variables=None, delay=0, username=None): """ Create a run ready for re-running based on the run provided. If variables (RunVariable) are provided, copy them and associate them with the new one, otherwise use the previous run's. If a script (as a string) is supplied then use it, otherwise use the previous run's. """ from reduction_variables.utils import InstrumentVariablesUtils, VariableUtils # find the previous run version, so we don't create a duplicate last_version = -1 for run in ReductionRun.objects.filter(experiment=reductionRun.experiment, run_number=reductionRun.run_number): last_version = max(last_version, run.run_version) try: # get the script to use: script_text = script if script is not None else reductionRun.script # create the run object and save it new_job = ReductionRun( instrument=reductionRun.instrument, run_number=reductionRun.run_number, run_name="", run_version=last_version + 1, experiment=reductionRun.experiment, started_by=username, status=StatusUtils().get_queued(), script=script_text, ) new_job.save() reductionRun.retry_run = new_job reductionRun.retry_when = timezone.now().replace(microsecond=0) + datetime.timedelta( seconds=delay if delay else 0 ) reductionRun.save() # copy the previous data locations for data_location in reductionRun.data_location.all(): new_data_location = DataLocation(file_path=data_location.file_path, reduction_run=new_job) new_data_location.save() new_job.data_location.add(new_data_location) if variables is not None: # associate the variables with the new run for var in variables: var.reduction_run = new_job var.save() else: # provide variables if they aren't already InstrumentVariablesUtils().create_variables_for_run(new_job) return new_job except: new_job.delete() raise
def createRetryRun(self, reductionRun, overwrite=None, script=None, variables=None, delay=0, username=None, description=''): """ Create a run ready for re-running based on the run provided. If variables (RunVariable) are provided, copy them and associate them with the new one, otherwise use the previous run's. If a script (as a string) is supplied then use it, otherwise use the previous run's. """ from reduction_variables.utils import InstrumentVariablesUtils, VariableUtils run_last_updated = reductionRun.last_updated if username == 'super': username = 1 # find the previous run version, so we don't create a duplicate last_version = -1 for run in ReductionRun.objects.filter( experiment=reductionRun.experiment, run_number=reductionRun.run_number): last_version = max(last_version, run.run_version) try: # get the script to use: script_text = script if script is not None else reductionRun.script # create the run object and save it new_job = ReductionRun(instrument=reductionRun.instrument, run_number=reductionRun.run_number, run_name=description, run_version=last_version + 1, experiment=reductionRun.experiment, started_by=username, status=StatusUtils().get_queued(), script=script_text, overwrite=overwrite) new_job.save() reductionRun.retry_run = new_job reductionRun.retry_when = timezone.now().replace( microsecond=0) + datetime.timedelta( seconds=delay if delay else 0) reductionRun.save() ReductionRun.objects.filter(id=reductionRun.id).update( last_updated=run_last_updated) # copy the previous data locations for data_location in reductionRun.data_location.all(): new_data_location = DataLocation( file_path=data_location.file_path, reduction_run=new_job) new_data_location.save() new_job.data_location.add(new_data_location) if variables is not None: # associate the variables with the new run for var in variables: var.reduction_run = new_job var.save() else: # provide variables if they aren't already InstrumentVariablesUtils().create_variables_for_run(new_job) return new_job except Exception as e: import traceback logger.error(traceback.format_exc()) logger.error(e) new_job.delete() raise