def add_job(): try: job = json.loads(request.form.get('jsonData')) except Exception as e: return json.dumps({ 'status': "Error", 'message': 'Invalid JSON: {}.'.format(e) }) jobData = jobs.add_job(job) while (jobData['status'] == 'submitted'): jobData = jobs.get_job(jobData['job_id']) time.sleep(2) jobData = json.dumps(jobs.get_job(jobData['job_id'])) jsonData = json.loads(jobData) # render a different form for a plot if (jsonData['job_type'] == 'plot'): job_link = "https://isp-proxy.tacc.utexas.edu/phart/download/" + jsonData[ 'job_id'] return render_template('plot.html', job_id=jsonData['job_id'], job_link=job_link) return render_template('formReturn.html', job_type=jsonData['job_type'], data=jobData)
def job_by_id(id): """Return a job by id.""" job_dict = jobs.get_job(redis_client, id) if job_dict: return jsonify(job_dict) else: return _make_error('job not found for job id.'), 404
def get_jobs(life, group_id): _group = get_group(life, group_id) _jobs = [] _leader = LIFE[_group['leader']] if not has_camp(group_id): _nearest_camp = camps.get_nearest_known_camp(_leader) if _leader['known_camps']: _j = jobs.create_job(_leader, 'Raid', gist='start_raid', description='Raid camp %s.' % _nearest_camp['id']) _pos = lfe.get_current_chunk(_leader)['pos'] _chunk_key = lfe.get_current_chunk_id(_leader) jobs.add_task(_j, '0', 'announce_to_group', action.make_small_script(function='announce_to_group', kwargs={'group_id': group_id, 'gist': 'announce_group_job', 'message': jobs.get_job(_j)['description'], 'job_id': _j}), player_action=action.make_small_script(function='always'), description='Gather group members.') jobs.add_task(_j, '1', 'move_to_chunk', action.make_small_script(function='travel_to_position', kwargs={'pos': _pos}), player_action=action.make_small_script(function='is_in_chunk', kwargs={'chunk_key': _chunk_key}), description='Travel to position %s, %s' % (_pos[0], _pos[1]), delete_on_finish=False) jobs.add_task(_j, '2', 'wait_for_number_of_group_members_in_chunk', action.make_small_script(function='number_of_alife_in_chunk_matching', kwargs={'amount': 2, 'chunk_key': _chunk_key, 'matching': {'group': _leader['group']}}), description='Wait until everyone arrives.') #jobs.add_task(_j, '3', 'talk', # action.make_small_script(function='travel_to_position', # kwargs={'pos': chunks.get_nearest_chunk_in_list(_leader['pos'], camps.get_camp(_nearest_camp['id'])['reference'])}), # requires=['1'], # delete_on_finish=False) _jobs.append(_j) if len(_leader['known_groups'])>1: _lowest = {'score': 0, 'group': None} for group_id in [g for g in _leader['known_groups'] if not g==_leader['group']]: _score = judgement.judge_group(_leader, group_id) if not _lowest['group'] or _score < _lowest['score']: _lowest['score'] = _score _lowest['group'] = group_id print 'RAID', _lowest else: print 'ony one' return _jobs
def get_put_delete_job(job_id): """ Request handler for the /jobs/<job_id> path. GET: returns the record for a specific job by id. Args: job_id (str): the unique identifier of the job. Returns: Job (str): job record as json if response status == 200. Error (str): json error if response status >= 500 PUT: amends the record for a specific job by id. Args: job_id (str): the unique identifier of the job. Body: job: job data in json format. Returns: Nothing: if response status >= 200 and <= 499. Error (str): json error if response status >= 500 DELETE: removes the record for a specific job by id. Args: job_id (str): the unique identifier of the job. Returns: Nothing: if response status >= 200 and <= 499. Error (str): json error if response status >= 500 """ if request.method == 'DELETE': try: return _make_response(response=jobs.delete_job(job_id)) except api_error.NotFoundError as e: return _make_error(404, e.message) except api_error.BadRequestError as e: return _make_error(400, e.message) except Exception as e: return _make_error(500, e.message) elif request.method == 'PUT': job = request.json try: return _make_response(response=jobs.update_job(job_id, job)) except api_error.BadRequestError as e: return _make_error(400, e.message) except Exception as e: return _make_error(500, e.message) else: try: return _make_response(response=jobs.get_job(job_id)) except api_error.NotFoundError as e: return _make_error(404, e.message) except api_error.BadRequestError as e: return _make_error(400, e.message) except Exception as e: return _make_error(500, e.message)
def _handle_job_id(job_id): jobs.update_status(redis_client, job_id, 'processing') job_dict = jobs.get_job(redis_client, job_id) data = _get_data(job_dict) plot = _create_plot(data, job_dict['job_type']) jobs.update_plot(redis_client, job_id, plot) jobs.update_status(redis_client, job_id, 'completed')
def get_one_job(job_id): """ Checks validity of job_id, if non-valid a 400 error will be returned. Returns job dictionary corresponding to job_id if valid. """ try: result = jobs.get_job(job_id) except: return jsonify( "Job id supplied led to no hits in our database, please try again." ), 400 return jsonify(result)
def judge_jobs(life): _tier = 0 if life["task"]: return life["job"] _new_job = None _old_job = life["job"] # life['job'] = None for job in [jobs.get_job(j) for j in life["jobs"]]: if not jobs.get_free_tasks(job["id"]): continue if not jobs.meets_job_requirements(life, job["id"]): continue if life["group"] and job["gist"] == "create_group": if not stats.wants_to_abandon_group(life, life["group"]): jobs.reject_job(job["id"], life["id"]) continue if life["group"] == jobs.get_flag(job["id"], "group"): jobs.reject_job(job["id"], life["id"]) continue if jobs.has_completed_or_rejected_job(life, job["id"]): continue if job["tier"] >= _tier: # jobs.join_job(job['id'], life['id']) _new_job = job["id"] _tier = job["tier"] if not _old_job == _new_job: if _old_job: jobs.leave_job(_old_job, life["id"]) if _new_job: jobs.join_job(_new_job, life["id"]) if life["job"]: life["task"] = jobs.get_next_task(life, life["job"]) if not life["task"]: if life["job"]: jobs.leave_job(life["job"], life["id"], reject=True) if not life["job"] == _old_job: life["completed_tasks"] = [] return life["job"]
def judge_jobs(life): _tier = 0 if life['task']: return life['job'] _new_job = None _old_job = life['job'] #life['job'] = None for job in [jobs.get_job(j) for j in life['jobs']]: if not jobs.get_free_tasks(job['id']): continue if not jobs.meets_job_requirements(life, job['id']): continue if life['group'] and job['gist'] == 'create_group': if not stats.wants_to_abandon_group(life, life['group']): jobs.reject_job(job['id'], life['id']) continue if life['group'] == jobs.get_flag(job['id'], 'group'): jobs.reject_job(job['id'], life['id']) continue if jobs.has_completed_or_rejected_job(life, job['id']): continue if job['tier'] >= _tier: #jobs.join_job(job['id'], life['id']) _new_job = job['id'] _tier = job['tier'] if not _old_job == _new_job: if _old_job: jobs.leave_job(_old_job, life['id']) if _new_job: jobs.join_job(_new_job, life['id']) if life['job']: life['task'] = jobs.get_next_task(life, life['job']) if not life['task']: if life['job']: jobs.leave_job(life['job'], life['id'], reject=True) if not life['job'] == _old_job: life['completed_tasks'] = [] return life['job']
def data_worker(job_id): """ Decorator waits for queue to have a job put in it. Once an item is put in queue the graph worker takes the job_id, gets the job_dict updates job status, executes the job, updates job's dictionary 'results' key to the results of the function, updates status again to complete and prints out "job_id + complete" to console. """ job_dict = jobs.get_job(job_id) jobs.update_job(job_dict["id"], "Processing") results = execute_job(job_dict) jobs.update_job(job_dict["id"], "Completed", results) print(job_id + " complete")
def get_job_results(job_id): """ Checks validity of job_id, if non-valid a 400 error will be returned. Returns job results corresponding to job_id if valid. """ try: results = jobs.get_job(job_id)["results"] if not results: return jsonify("Your job is not yet completed.") except: return jsonify( "Job id supplied led to no hits in our database, please try again." ), 400 return jsonify(results)
def get_tier(life): if life['job']: _job = jobs.get_job(life['job']) return _job['tier'] return TIER
def get_jobs(life, group_id): _group = get_group(life, group_id) _jobs = [] _leader = LIFE[_group['leader']] if not has_camp(group_id): _nearest_camp = camps.get_nearest_known_camp(_leader) if _leader['known_camps']: _j = jobs.create_job(_leader, 'Raid', gist='start_raid', description='Raid camp %s.' % _nearest_camp['id']) _pos = lfe.get_current_chunk(_leader)['pos'] _chunk_key = lfe.get_current_chunk_id(_leader) jobs.add_task( _j, '0', 'announce_to_group', action.make_small_script(function='announce_to_group', kwargs={ 'group_id': group_id, 'gist': 'announce_group_job', 'message': jobs.get_job(_j)['description'], 'job_id': _j }), player_action=action.make_small_script(function='always'), description='Gather group members.') jobs.add_task( _j, '1', 'move_to_chunk', action.make_small_script(function='travel_to_position', kwargs={'pos': _pos}), player_action=action.make_small_script( function='is_in_chunk', kwargs={'chunk_key': _chunk_key}), description='Travel to position %s, %s' % (_pos[0], _pos[1]), delete_on_finish=False) jobs.add_task(_j, '2', 'wait_for_number_of_group_members_in_chunk', action.make_small_script( function='number_of_alife_in_chunk_matching', kwargs={ 'amount': 2, 'chunk_key': _chunk_key, 'matching': { 'group': _leader['group'] } }), description='Wait until everyone arrives.') #jobs.add_task(_j, '3', 'talk', # action.make_small_script(function='travel_to_position', # kwargs={'pos': chunks.get_nearest_chunk_in_list(_leader['pos'], camps.get_camp(_nearest_camp['id'])['reference'])}), # requires=['1'], # delete_on_finish=False) _jobs.append(_j) if len(_leader['known_groups']) > 1: _lowest = {'score': 0, 'group': None} for group_id in [ g for g in _leader['known_groups'] if not g == _leader['group'] ]: _score = judgement.judge_group(_leader, group_id) if not _lowest['group'] or _score < _lowest['score']: _lowest['score'] = _score _lowest['group'] = group_id print 'RAID', _lowest else: print 'ony one' return _jobs