Skip to content

Commit

Permalink
Emit project related tags from within project decorator (#695)
Browse files Browse the repository at this point in the history
* Emit project related tags from within project decorator

* update test
  • Loading branch information
savingoyal committed Sep 10, 2021
1 parent bd3705a commit 2f15f43
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 22 deletions.
24 changes: 11 additions & 13 deletions metaflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,19 +870,6 @@ def start(ctx,
if e.TYPE == environment][0](ctx.obj.flow)
ctx.obj.environment.validate_environment(echo)

ctx.obj.datastore = DATASTORES[datastore]
ctx.obj.datastore_root = datastore_root

# It is important to initialize flow decorators early as some of the
# things they provide may be used by some of the objects initialize after.
decorators._init_flow_decorators(ctx.obj.flow,
ctx.obj.graph,
ctx.obj.environment,
ctx.obj.datastore,
ctx.obj.logger,
echo,
deco_options)

ctx.obj.monitor = Monitor(monitor, ctx.obj.environment, ctx.obj.flow.name)
ctx.obj.monitor.start()

Expand All @@ -898,6 +885,17 @@ def start(ctx,
ctx.obj.datastore.get_datastore_root_from_config(ctx.obj.echo)
ctx.obj.datastore_root = ctx.obj.datastore.datastore_root = datastore_root

# It is important to initialize flow decorators early as some of the
# things they provide may be used by some of the objects initialize after.
decorators._init_flow_decorators(ctx.obj.flow,
ctx.obj.graph,
ctx.obj.environment,
ctx.obj.datastore,
ctx.obj.metadata,
ctx.obj.logger,
echo,
deco_options)

if decospecs:
decorators._attach_decorators(ctx.obj.flow, decospecs)

Expand Down
22 changes: 19 additions & 3 deletions metaflow/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ def __init__(self, *args, **kwargs):
self._flow_decorators.append(self)
super(FlowDecorator, self).__init__(*args, **kwargs)

def flow_init(self, flow, graph, environment, datastore, logger, echo, options):
def flow_init(self,
flow,
graph,
environment,
datastore,
metadata,
logger,
echo,
options):
"""
Called when all decorators have been created for this flow.
"""
Expand Down Expand Up @@ -431,10 +439,18 @@ def _attach_decorators_to_step(step, decospecs):
deco = decos[deconame]._parse_decorator_spec(decospec)
step.decorators.append(deco)

def _init_flow_decorators(flow, graph, environment, datastore, logger, echo, deco_options):
def _init_flow_decorators(flow,
graph,
environment,
datastore,
metadata,
logger,
echo,
deco_options):
for deco in flow._flow_decorators.values():
opts = {option: deco_options[option] for option in deco.options}
deco.flow_init(flow, graph, environment, datastore, logger, echo, opts)
deco.flow_init(flow, graph, environment,
datastore, metadata, logger, echo, opts)


def _init_step_decorators(flow, graph, environment, datastore, logger):
Expand Down
3 changes: 0 additions & 3 deletions metaflow/metadata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,6 @@ def _tags(self):
tags.append('metaflow_r_version:' + env['metaflow_r_version'])
if 'r_version_code' in env:
tags.append('r_version:' + env['r_version_code'])
if 'project_name' in current:
tags.append('project:' + current.project_name)
tags.append('project_branch:' + current.branch_name)
return tags

def _register_code_package_metadata(self, run_id, step_name, task_id):
Expand Down
10 changes: 9 additions & 1 deletion metaflow/plugins/aws/step_functions/schedule_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ class ScheduleDecorator(FlowDecorator):
'daily': True,
'hourly': False}

def flow_init(self, flow, graph, environment, datastore, logger, echo, options):
def flow_init(self,
flow,
graph,
environment,
datastore,
metadata,
logger,
echo,
options):
# Currently supports quartz cron expressions in UTC as defined in
# https://docs.aws.amazon.com/eventbridge/latest/userguide/scheduled-events.html#cron-expressions
if self.attributes['cron']:
Expand Down
10 changes: 9 additions & 1 deletion metaflow/plugins/conda/conda_flow_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ class MyFlow(FlowSpec):
'python': None,
'disabled': None}

def flow_init(self, flow, graph, environment, datastore, logger, echo, options):
def flow_init(self,
flow,
graph,
environment,
datastore,
metadata,
logger,
echo,
options):
if environment.TYPE != 'conda':
raise InvalidEnvironmentException('The *@conda* decorator requires '
'--environment=conda')
4 changes: 4 additions & 0 deletions metaflow/plugins/project_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def flow_init(self,
graph,
environment,
datastore,
metadata,
logger,
echo,
options):
Expand All @@ -54,6 +55,9 @@ def flow_init(self,
'is_user_branch': is_user_branch,
'is_production': options['production'],
'project_flow_name': project_flow_name})
metadata.add_sticky_tags(sys_tags=[
'project:%s' % project_name,
'project_branch:%s' % branch_name])

def get_top_level_options(self):
return list(self._option_values.items())
Expand Down
2 changes: 1 addition & 1 deletion test/core/metaflow_extensions/plugins/flow_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class FlowDecoratorWithOptions(FlowDecorator):
)
}

def flow_init(self, flow, graph, environment, flow_datastore, logger, echo, options):
def flow_init(self, flow, graph, environment, flow_datastore, metadata, logger, echo, options):
current._update_env({'foobar_value': options['foobar']})

0 comments on commit 2f15f43

Please sign in to comment.