Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions airflow/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,10 @@ def _process_dags(self, dagbag, dags, tis_out):
"""
for dag in dags:
dag = dagbag.get_dag(dag.dag_id)
if dag.reached_max_runs:
self.logger.info("Not processing DAG {} since its max runs has been reached"
.format(dag.dag_id))
continue
if dag.is_paused:
self.logger.info("Not processing DAG {} since it's paused"
.format(dag.dag_id))
Expand Down
9 changes: 9 additions & 0 deletions airflow/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2879,6 +2879,15 @@ def subdags(self):
l += task.subdag.subdags
return l

@property
def reached_max_runs(self):
active_runs = DagRun.find(
dag_id=self.dag_id,
state=State.RUNNING,
external_trigger=False
)
return len(active_runs) >= self.max_active_runs

def resolve_template_files(self):
for t in self.tasks:
t.resolve_template_files()
Expand Down
18 changes: 14 additions & 4 deletions tests/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import datetime
import logging
import os
import time
import unittest

from airflow import AirflowException, settings
Expand Down Expand Up @@ -609,14 +610,23 @@ def test_scheduler_verify_max_active_runs(self):
session.commit()
session.close()

scheduler = SchedulerJob()
dag.clear()
scheduler = SchedulerJob(dag.dag_id,
run_duration=1)

dr = scheduler.create_dag_run(dag)
self.assertIsNotNone(dr)

dr = scheduler.create_dag_run(dag)
self.assertIsNone(dr)
dr2 = scheduler.create_dag_run(dag)
self.assertIsNone(dr2)

dag.clear()

dag.max_active_runs = 0
scheduler.run()

session = settings.Session()
self.assertEqual(
len(session.query(TI).filter(TI.dag_id == dag.dag_id).all()), 0)

def test_scheduler_fail_dagrun_timeout(self):
"""
Expand Down