Skip to content

Commit

Permalink
[TWTTR] Fix for rendering code on UI (twitter-forks#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
vshshjn7 authored Mar 9, 2020
1 parent 974f717 commit 4ce8d4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
34 changes: 19 additions & 15 deletions airflow/models/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,24 @@ class DagModel(Base, LoggingMixin):
def __repr__(self):
return "<DAG: {self.dag_id}>".format(self=self)

def get_local_fileloc(self):
# TODO: [CX-16591] Resolve this in upstream by storing relative path in db (config driven)
try:
# Fix for DAGs that are manually triggered in the UI, as the DAG path in the DB is
# stored by the scheduler which has a different path than the webserver due to absolute
# paths in aurora including randomly generated job-specific directories. Due to this
# the path the webserver uses when it tries to trigger a DAG does not match the
# existing scheduler path and the DAG can not be found.
# Also, fix for render code on UI by changing "/code" in views.py
path_regex = "airflow_scheduler-.-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[" \
"0-9a-f]{12}/runs/.*/sandbox/airflow_home"
path_split = re.split(path_regex, self.fileloc)[1]
return os.environ.get("AIRFLOW_HOME") + path_split
except IndexError:
self.log.info("No airflow_home in path: " + self.fileloc)

return self.fileloc

@property
def timezone(self):
return settings.TIMEZONE
Expand Down Expand Up @@ -1505,21 +1523,7 @@ def safe_dag_id(self):
return self.dag_id.replace('.', '__dot__')

def get_dag(self):
# TODO: [CX-16591] Resolve this in upstream by storing relative path in db (config driven)
try:
# Fix for DAGs that are manually triggered in the UI, as the DAG path in the DB is
# stored by the scheduler which has a different path than the webserver due to absolute
# paths in aurora including randomly generated job-specific directories. Due to this
# the path the webserver uses when it tries to trigger a DAG does not match the
# existing scheduler path and the DAG can not be found.
path_regex = "airflow_scheduler-.-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[" \
"0-9a-f]{12}/runs/.*/sandbox/airflow_home"
path_split = re.split(path_regex, self.fileloc)[1]
self.fileloc = os.environ.get("AIRFLOW_HOME") + path_split
except IndexError:
self.log.info("No airflow_home in path: " + self.fileloc)

return DagBag(dag_folder=self.fileloc).get_dag(self.dag_id)
return DagBag(dag_folder=self.get_local_fileloc()).get_dag(self.dag_id)

@provide_session
def create_dagrun(self,
Expand Down
2 changes: 1 addition & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def code(self, session=None):
dm = models.DagModel
dag = session.query(dm).filter(dm.dag_id == dag_id).first()
try:
with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f:
with wwwutils.open_maybe_zipped(dag.get_local_fileloc(), 'r') as f:
code = f.read()
html_code = highlight(
code, lexers.PythonLexer(), HtmlFormatter(linenos=True))
Expand Down
2 changes: 1 addition & 1 deletion airflow/www_rbac/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def code(self, session=None):
dag_id = request.args.get('dag_id')
dag = session.query(dm).filter(dm.dag_id == dag_id).first()
try:
with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f:
with wwwutils.open_maybe_zipped(dag.get_local_fileloc(), 'r') as f:
code = f.read()
html_code = highlight(
code, lexers.PythonLexer(), HtmlFormatter(linenos=True))
Expand Down

0 comments on commit 4ce8d4c

Please sign in to comment.