def set_job_state(jid, state): # TODO # TODO Later: notify_user # TODO Later: update_current_scheduler_priority result = db.query(Job).filter(Job.id == jid)\ .filter(Job.state != 'Error')\ .filter(Job.state != 'Terminated')\ .filter(Job.state != state)\ .update({Job.state: state}) db.commit() if result == 1: # OK for sqlite logger.debug( "Job state updated, job_id: " + str(jid) + ", wanted state: " + state) date = tools.get_date() # TODO: optimize job log db.query(JobStateLog).filter(JobStateLog.date_stop == 0)\ .filter(JobStateLog.job_id == jid)\ .update({JobStateLog.date_stop: date}) db.commit() req = db.insert(JobStateLog).values( {'job_id': jid, 'job_state': state, 'date_start': date}) db.session.execute(req) if state == "Terminated" or state == "Error" or state == "toLaunch" or \ state == "Running" or state == "Suspended" or state == "Resuming": job = db.query(Job).filter(Job.id == jid).one() if state == "Suspend": tools.notify_user(job, "SUSPENDED", "Job is suspended.") elif state == "Resuming": tools.notify_user(job, "RESUMING", "Job is resuming.") elif state == "Running": tools.notify_user(job, "RUNNING", "Job is running.") elif state == "toLaunch": update_current_scheduler_priority(job, "+2", "START") else: # job is "Terminated" or ($state eq "Error") if job.stop_time < job.start_time: db.query(Job).filter(Job.id == jid)\ .update({Job.stop_time: job.start_time}) db.commit() if job.assigned_moldable_job != "0": # Update last_job_date field for resources used update_scheduler_last_job_date( date, int(job.assigned_moldable_job)) if state == "Terminated": tools.notify_user(job, "END", "Job stopped normally.") else: # Verify if the job was suspended and if the resource # property suspended is updated if job.suspended == "YES": r = get_current_resources_with_suspended_job() if r != (): db.query(Resource).filter(~Resource.id.in_(r))\ .update({Resource.suspended_jobs: 'NO'}) else: db.query(Resource).update( {Resource.suspended_jobs: 'NO'}) db.commit() tools.notify_user( job, "ERROR", "Job stopped abnormally or an OAR error occured.") update_current_scheduler_priority(job, "-2", "STOP") # Here we must not be asynchronously with the scheduler log_job(job) # $dbh is valid so these 2 variables must be defined nb_sent = tools.notify_almighty("ChState") if nb_sent == 0: logger.warning("Not able to notify almighty to launch the job " + str(job.id) + " (socket error)") else: logger.warning("Job is already termindated or in error or wanted state, job_id: " + str(jid) + ", wanted state: " + state)
def add_micheline_subjob(job_vars, ssh_private_key, ssh_public_key, array_id, array_index, array_commands, properties_applied_after_validation): # Estimate_job_nb_resources and incidentally test if properties and resources request are coherent # against avalaible resources # pdb.set_trace() date = get_date() properties = job_vars['properties'] resource_request = job_vars['resource_request'] resource_available, estimated_nb_resources = estimate_job_nb_resources(resource_request, properties) # Add admin properties to the job if properties_applied_after_validation: if properties: properties = '(' + properties + ') AND ' + properties_applied_after_validation else: properties = properties_applied_after_validation job_vars['properties'] = properties # TODO Verify the content of the ssh keys # TODO format job message # message = '' # my $job_message = format_job_message_text($job_name,$estimated_nb_resources, $estimated_walltime, # $jobType, $reservationField, $queue_name, $project, $type_list, ''); # TODO job_group # name = job_vars['name'] stdout = job_vars['stdout'] if not stdout: stdout = 'OAR' if name: stdout += '.' + name stdout += ".%jobid%.stdout" else: stdout = re.sub(r'%jobname%', name, stdout) job_vars['stdout'] = stdout stderr = job_vars['stderr'] if not stderr: stderr = 'OAR' if name: stderr += '.' + name stderr += '.%jobid%.stderr' else: stderr = re.sub(r'%jobname%', name, stderr) stderr = job_vars['stderr'] # Insert job kwargs = job_kwargs(job_vars, array_commands[0], date) kwargs['message'] = '' # TODO message kwargs['array_index'] = array_index if array_id > 0: kwargs['array_id'] = array_id ins = Job.__table__.insert().values(**kwargs) result = db.session.execute(ins) job_id = result.inserted_primary_key[0] if array_id <= 0: db.query(Job).filter(Job.id == job_id).update({Job.array_id: job_id}) db.commit() random_number = random.randint(1, 1000000000000) ins = Challenge.__table__.insert().values( {'job_id': job_id, 'challenge': random_number, 'ssh_private_key': ssh_private_key, 'ssh_public_key': ssh_public_key}) db.session.execute(ins) # print(resource_request) # Insert resources request in DB mld_jid_walltimes = [] resource_desc_lst = [] for moldable_instance in resource_request: resource_desc, walltime = moldable_instance if not walltime: # TODO add nullable=True in [email protected] ? walltime = 0 mld_jid_walltimes.append( {'moldable_job_id': job_id, 'moldable_walltime': walltime}) resource_desc_lst.append(resource_desc) # Insert MoldableJobDescription job_id and walltime # print('mld_jid_walltimes) result = db.session.execute(MoldableJobDescription.__table__.insert(), mld_jid_walltimes) # Retrieve MoldableJobDescription.ids if len(mld_jid_walltimes) == 1: mld_ids = [result.inserted_primary_key[0]] else: r = db.query(MoldableJobDescription.id)\ .filter(MoldableJobDescription.job_id == job_id).all() mld_ids = [e[0] for e in r] # # print(mld_ids, resource_desc_lst) for mld_idx, resource_desc in enumerate(resource_desc_lst): # job_resource_groups mld_id_property = [] res_lst = [] moldable_id = mld_ids[mld_idx] for prop_res in resource_desc: prop = prop_res['property'] res = prop_res['resources'] mld_id_property.append({'res_group_moldable_id': moldable_id, 'res_group_property': prop}) res_lst.append(res) # print(mld_id_property) # Insert property for moldable db.session.execute(JobResourceGroup.__table__.insert(), mld_id_property) if len(mld_id_property) == 1: grp_ids = [result.inserted_primary_key[0]] else: r = db.query(JobResourceGroup.id)\ .filter(JobResourceGroup.moldable_id == moldable_id).all() grp_ids = [e[0] for e in r] # print('grp_ids, res_lst) # Insert job_resource_descriptions for grp_idx, res in enumerate(res_lst): res_description = [] for idx, res_value in enumerate(res): res_description.append({'res_job_group_id': grp_ids[grp_idx], 'res_job_resource_type': res_value['resource'], 'res_job_value': res_value['value'], 'res_job_order': idx}) # print(res_description) db.session.execute(JobResourceDescription.__table__.insert(), res_description) # types of job types = job_vars['types'] if types: ins = [{'job_id': job_id, 'type': typ} for typ in types] db.session.execute(JobType.__table__.insert(), ins) # TODO dependencies with min_start_shift and max_start_shift dependencies = job_vars['dependencies'] if dependencies: ins = [{'job_id': job_id, 'job_id_required': dep} for dep in dependencies] db.session.execute(JobDependencie.__table__.insert(), ins) # foreach my $a (@{$anterior_ref}){ # if (my ($j,$min,$max) = $a =~ /^(\d+)(?:,([\[\]][-+]?\d+)?(?:,([\[\]][-+]?\d+)?)?)?$/) { # $dbh->do(" INSERT INTO job_dependencies (job_id,job_id_required,min_start_shift,max_start_shift) # VALUES ($job_id,$j,'".(defined($min)?$min:"")."','".(defined($max)?$max:"")."') if not job_vars['hold']: req = db.insert(JobStateLog).values( {'job_id': job_id, 'job_state': 'Waiting', 'date_start': date}) db.session.execute(req) db.commit() db.query(Job).filter(Job.id == job_id).update({Job.state: 'Waiting'}) db.commit() else: req = db.insert(JobStateLog).values( {'job_id': job_id, 'job_state': 'Hold', 'date_start': date}) db.session.execute(req) db.commit() return(0, job_id)