# Buggy code
def _update_dag_next_dagruns(
    self, dag_models: Iterable[DagModel], session: Session
) -> None:
    """
    Bulk update the next_dagrun and next_dagrun_create_after for all the dags.

    We batch the select queries to get info about all the dags at once
    """
    # Check max_active_runs, to see if we are _now_ at the limit for any of
    # these dag? (we've just created a DagRun for them after all)
    active_runs_of_dags = dict(
        session.query(DagRun.dag_id, func.count("*"))
        .filter(
            DagRun.dag_id.in_([o.dag_id for o in dag_models]),
            DagRun.state == State.RUNNING,  # pylint: disable=comparison-with-callable
            DagRun.external_trigger.is_(False),
        )
        .group_by(DagRun.dag_id)
        .all()
    )

    for dag_model in dag_models:
        dag = self.dagbag.get_dag(dag_model.dag_id, session=session)
        active_runs_of_dag = active_runs_of_dags.get(dag.dag_id, 0)
        if dag.max_active_runs and active_runs_of_dag >= dag.max_active_runs:
            self.log.info(
                "DAG %s is at (or above) max_active_runs (%d of %d), not creating any more runs",
                dag.dag_id,
                active_runs_of_dag,
                dag.max_active_runs,
            )
            dag_model.next_dagrun_create_after = None
        else:
            dag_model.next_dagrun, dag_model.next_dagrun_create_after = (
                dag.next_dagrun_info(dag_model.next_dagrun)
            )


# Traceback (airflow.exceptions.SerializedDagNotFound)
[2021-01-06 10:09:38,742] {scheduler_job.py:1293} ERROR - Exception when executing SchedulerJob._run_scheduler_loop
Traceback (most recent call last):
File "/global/packages/python/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1275, in _execute
self._run_scheduler_loop()
File "/global/packages/python/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1377, in _run_scheduler_loop
num_queued_tis = self._do_scheduling(session)
File "/global/packages/python/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1474, in _do_scheduling
self._create_dag_runs(query.all(), session)
File "/global/packages/python/lib/python3.7/site-packages/airflow/jobs/scheduler_job.py", line 1557, in _create_dag_runs
dag = self.dagbag.get_dag(dag_model.dag_id, session=session)
File "/global/packages/python/lib/python3.7/site-packages/airflow/utils/session.py", line 62, in wrapper
return func(*args, **kwargs)
File "/global/packages/python/lib/python3.7/site-packages/airflow/models/dagbag.py", line 171, in get_dag
self._add_dag_from_db(dag_id=dag_id, session=session)
File "/global/packages/python/lib/python3.7/site-packages/airflow/models/dagbag.py", line 227, in _add_dag_from_db
raise SerializedDagNotFound(f"DAG '{dag_id}' not found in serialized_dag table")
airflow.exceptions.SerializedDagNotFound: DAG 'dynamic_dag_1' not found in serialized_dag table

# Fixed code
def _update_dag_next_dagruns(
    self, dag_models: Iterable[DagModel], session: Session
) -> None:
    """
    Bulk update the next_dagrun and next_dagrun_create_after for all the dags.

    We batch the select queries to get info about all the dags at once
    """
    # Check max_active_runs, to see if we are _now_ at the limit for any of
    # these dag? (we've just created a DagRun for them after all)
    active_runs_of_dags = dict(
        session.query(DagRun.dag_id, func.count("*"))
        .filter(
            DagRun.dag_id.in_([o.dag_id for o in dag_models]),
            DagRun.state == State.RUNNING,  # pylint: disable=comparison-with-callable
            DagRun.external_trigger.is_(False),
        )
        .group_by(DagRun.dag_id)
        .all()
    )

    for dag_model in dag_models:
        # Get the DAG in a try_except to not stop the Scheduler when a Serialized DAG is not found
        # This takes care of Dynamic DAGs especially
        try:
            dag = self.dagbag.get_dag(dag_model.dag_id, session=session)
        except SerializedDagNotFound:
            self.log.exception(
                "DAG '%s' not found in serialized_dag table", dag_model.dag_id
            )
            continue
        active_runs_of_dag = active_runs_of_dags.get(dag.dag_id, 0)
        if dag.max_active_runs and active_runs_of_dag >= dag.max_active_runs:
            self.log.info(
                "DAG %s is at (or above) max_active_runs (%d of %d), not creating any more runs",
                dag.dag_id,
                active_runs_of_dag,
                dag.max_active_runs,
            )
            dag_model.next_dagrun_create_after = None
        else:
            dag_model.next_dagrun, dag_model.next_dagrun_create_after = (
                dag.next_dagrun_info(dag_model.next_dagrun)
            )