Skip to content

Commit 4ce8d4c

Browse files
authored
[TWTTR] Fix for rendering code on UI (twitter-forks#34)
1 parent 974f717 commit 4ce8d4c

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

airflow/models/dag.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,24 @@ class DagModel(Base, LoggingMixin):
14751475
def __repr__(self):
14761476
return "<DAG: {self.dag_id}>".format(self=self)
14771477

1478+
def get_local_fileloc(self):
1479+
# TODO: [CX-16591] Resolve this in upstream by storing relative path in db (config driven)
1480+
try:
1481+
# Fix for DAGs that are manually triggered in the UI, as the DAG path in the DB is
1482+
# stored by the scheduler which has a different path than the webserver due to absolute
1483+
# paths in aurora including randomly generated job-specific directories. Due to this
1484+
# the path the webserver uses when it tries to trigger a DAG does not match the
1485+
# existing scheduler path and the DAG can not be found.
1486+
# Also, fix for render code on UI by changing "/code" in views.py
1487+
path_regex = "airflow_scheduler-.-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[" \
1488+
"0-9a-f]{12}/runs/.*/sandbox/airflow_home"
1489+
path_split = re.split(path_regex, self.fileloc)[1]
1490+
return os.environ.get("AIRFLOW_HOME") + path_split
1491+
except IndexError:
1492+
self.log.info("No airflow_home in path: " + self.fileloc)
1493+
1494+
return self.fileloc
1495+
14781496
@property
14791497
def timezone(self):
14801498
return settings.TIMEZONE
@@ -1505,21 +1523,7 @@ def safe_dag_id(self):
15051523
return self.dag_id.replace('.', '__dot__')
15061524

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

15241528
@provide_session
15251529
def create_dagrun(self,

airflow/www/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def code(self, session=None):
696696
dm = models.DagModel
697697
dag = session.query(dm).filter(dm.dag_id == dag_id).first()
698698
try:
699-
with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f:
699+
with wwwutils.open_maybe_zipped(dag.get_local_fileloc(), 'r') as f:
700700
code = f.read()
701701
html_code = highlight(
702702
code, lexers.PythonLexer(), HtmlFormatter(linenos=True))

airflow/www_rbac/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def code(self, session=None):
446446
dag_id = request.args.get('dag_id')
447447
dag = session.query(dm).filter(dm.dag_id == dag_id).first()
448448
try:
449-
with wwwutils.open_maybe_zipped(dag.fileloc, 'r') as f:
449+
with wwwutils.open_maybe_zipped(dag.get_local_fileloc(), 'r') as f:
450450
code = f.read()
451451
html_code = highlight(
452452
code, lexers.PythonLexer(), HtmlFormatter(linenos=True))

0 commit comments

Comments
 (0)