Skip to content

Commit

Permalink
Merge pull request #62 from galaxyproject/additional_context_params
Browse files Browse the repository at this point in the history
Additional context params
  • Loading branch information
nuwang authored Dec 8, 2022
2 parents 66aeda8 + 873d02d commit a1abac5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
6 changes: 5 additions & 1 deletion tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ def test_all_entities_refer_to_same_loader(self):
# get the original loader
original_loader = gateway.ACTIVE_DESTINATION_MAPPER.loader

context = {
'app': app,
'job': job
}
# make sure we are still referring to the same loader after evaluation
_, evaluated_entity = gateway.ACTIVE_DESTINATION_MAPPER.match_combine_evaluate_entities(app, tool, user, job)
evaluated_entity = gateway.ACTIVE_DESTINATION_MAPPER.match_combine_evaluate_entities(context, tool, user)
assert evaluated_entity.loader == original_loader
for rule in evaluated_entity.rules:
assert rule.loader == original_loader
Expand Down
50 changes: 28 additions & 22 deletions tpv/core/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def combine_entities(self, entities):
def rank(self, entity, destinations, context):
return entity.rank_destinations(destinations, context)

def find_matching_destinations(self, entity, destinations, context):
def match_and_rank_destinations(self, entity, destinations, context):
matches = [dest for dest in destinations.values() if dest.matches(entity, context)]
return self.rank(entity, matches, context)

Expand Down Expand Up @@ -93,29 +93,18 @@ def _find_matching_entities(self, tool, user):

return entity_list

def match_combine_evaluate_entities(self, app, tool, user, job):
def match_combine_evaluate_entities(self, context, tool, user):
# 1. Find the entities relevant to this job
entity_list = self._find_matching_entities(tool, user)

# 2. Create evaluation context - these are the common variables available within any code block
context = {}
context.update(self.global_context or {})
context.update({
'app': app,
'tool': tool,
'user': user,
'job': job,
'mapper': self
})

# 3. Combine entity requirements
# 2. Combine entity requirements
combined_entity = self.combine_entities(entity_list)
context.update({
'entity': combined_entity,
'self': combined_entity
})

# 4. Evaluate expressions
# 3. Evaluate expressions
evaluated_entity = combined_entity.evaluate(context)
context.update({
'entity': evaluated_entity,
Expand All @@ -126,16 +115,32 @@ def match_combine_evaluate_entities(self, app, tool, user, job):
# with destinations
evaluated_entity.rules = {}

return context, evaluated_entity
return evaluated_entity

def map_to_destination(self, app, tool, user, job, job_wrapper=None, resource_params=None,
workflow_invocation_uuid=None):

# 1. Create evaluation context - these are the common variables available within any code block
context = {}
context.update(self.global_context or {})
context.update({
'app': app,
'tool': tool,
'user': user,
'job': job,
'job_wrapper': job_wrapper,
'resource_params': resource_params,
'workflow_invocation_uuid': workflow_invocation_uuid,
'mapper': self
})

def map_to_destination(self, app, tool, user, job):
# 1. Find, combine and evaluate entities that match this tool and user
context, evaluated_entity = self.match_combine_evaluate_entities(app, tool, user, job)
# 2. Find, combine and evaluate entities that match this tool and user
evaluated_entity = self.match_combine_evaluate_entities(context, tool, user)

# 2. Shortlist destinations with tags that match the combined entity
ranked_dest_entities = self.find_matching_destinations(evaluated_entity, self.destinations, context)
# 3. Match and rank destinations that best match the combined entity
ranked_dest_entities = self.match_and_rank_destinations(evaluated_entity, self.destinations, context)

# 3. Fully combine entity with matching destinations
# 4. Fully combine entity with matching destinations
if ranked_dest_entities:
wait_exception_raised = False
for d in ranked_dest_entities:
Expand All @@ -145,6 +150,7 @@ def map_to_destination(self, app, tool, user, job):
gxy_destination = app.job_config.get_destination(d.id)
if evaluated_destination.params.get('destination_name_override'):
gxy_destination.id = evaluated_destination.params.get('destination_name_override')
# 5. Return the top-ranked destination that evaluates successfully
return self.configure_gxy_destination(gxy_destination, evaluated_destination)
except TryNextDestinationOrFail as ef:
log.exception(f"Destination entity: {d} matched but could not fulfill requirements due to: {ef}."
Expand Down
6 changes: 4 additions & 2 deletions tpv/rules/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ def reload_destination_mapper(path=None):
return mapper


def map_tool_to_destination(app, job, tool, user, tpv_config_files):
def map_tool_to_destination(app, job, tool, user, tpv_config_files, job_wrapper=None, resource_params=None,
workflow_invocation_uuid=None):
global ACTIVE_DESTINATION_MAPPER
if not ACTIVE_DESTINATION_MAPPER:
ACTIVE_DESTINATION_MAPPER = setup_destination_mapper(app, tpv_config_files)
return ACTIVE_DESTINATION_MAPPER.map_to_destination(app, tool, user, job)
return ACTIVE_DESTINATION_MAPPER.map_to_destination(app, tool, user, job, job_wrapper, resource_params,
workflow_invocation_uuid)

0 comments on commit a1abac5

Please sign in to comment.