# Buggy code
def __monitor(self):
    to_monitor = self.workflow_scheduling_manager.active_workflow_schedulers
    while self.monitor_running:
        if self.invocation_grabber:
            self.invocation_grabber.grab_unhandled_items()

        monitor_step_timer = self.app.execution_timer_factory.get_timer(
            "internal.galaxy.workflows.scheduling_manager.monitor_step",
            "Workflow scheduling manager monitor step complete.",
        )
        for workflow_scheduler_id, workflow_scheduler in to_monitor.items():
            if not self.monitor_running:
                return

            self.__schedule(workflow_scheduler_id, workflow_scheduler)
        log.trace(monitor_step_timer.to_str())
        self._monitor_sleep(1)


# Traceback (psycopg2.OperationalError)
galaxy.workflow.scheduling_manager DEBUG 2021-02-21 15:59:02,329 Workflow invocation [540513] scheduled
galaxy.workflow.scheduling_manager DEBUG 2021-02-21 15:59:03,453 Attempting to schedule workflow invocation [(53535
2,)]
galaxy.workflow.run ERROR 2021-02-21 15:59:08,637 Failed to execute scheduled workflow.
Traceback (most recent call last):
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1296, in _execute_context
    result = context.get_result_proxy()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 624, in get_result_proxy
    return _result.BufferedRowResultProxy(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 775, in __init__
    self._init_metadata()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 1426, in _init_metadata
    self.__buffer_rows()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 1448, in
__buffer_rows
    self.__rowbuffer = collections.deque(self.cursor.fetchmany(size))
psycopg2.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/run.py", line 82, in __invoke
    outputs = invoker.invoke()
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/run.py", line 174, in invoke
    remaining_steps = self.progress.remaining_steps()
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/run.py", line 296, in remaining_steps
    step_states = self.workflow_invocation.step_states_by_step_id()
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/model/__init__.py", line 5379, in step_states_by_step_id
    for step_state in self.step_states:
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 287, in
__get__
    return self.impl.get(instance_state(instance), dict_)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 723, in
get
    value = self.callable_(state, passive)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/strategies.py", line 760, in
_load_for_state
    session, state, primary_key_identity, passive
  File "<string>", line 1, in <lambda>
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/strategies.py", line 902, in
_emit_lazyload
    .with_post_criteria(set_default_params)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/ext/baked.py", line 544, in all
    return list(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/ext/baked.py", line 444, in __ite
r__
    return q._execute_and_instances(context)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3560, in _exe
cute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
    return meth(self, multiparams, params)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1130, in _execute_clauseelement
    distilled_params,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1317, in _execute_context
    e, statement, parameters, cursor, context
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1511, in _handle_dbapi_exception
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1296, in _execute_context
    result = context.get_result_proxy()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 624, in get_result_proxy
    return _result.BufferedRowResultProxy(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 775, in __init__
    self._init_metadata()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 1426, in _init_metadata
    self.__buffer_rows()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/result.py", line 1448, in __buffer_rows
    self.__rowbuffer = collections.deque(self.cursor.fetchmany(size))
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

[SQL: SELECT workflow_request_step_states.id AS workflow_request_step_states_id, workflow_request_step_states.workflow_invocation_id AS workflow_request_step_states_workflow_invocation_id, workflow_request_step_states.workflow_step_id AS workflow_request_step_states_workflow_step_id, workflow_request_step_states.value AS workflow_request_step_states_value
FROM workflow_request_step_states
WHERE %(param_1)s = workflow_request_step_states.workflow_invocation_id]
[parameters: {'param_1': 535352}]
(Background on this error at: http://sqlalche.me/e/13/e3q8)
raven.base.Client DEBUG 2021-02-21 15:59:09,458 Sending message of length 8730 to https://sentry.galaxyproject.org/api/2/store/
galaxy.workflow.scheduling_manager ERROR 2021-02-21 15:59:11,246 Exception raised while attempting to schedule work
flow request.
Traceback (most recent call last):
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect
    return fn()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 364, in connect
    return _ConnectionFairy._checkout(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 500, in checkout
    rec._checkin_failed(err)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 497, in checkout
    dbapi_connection = rec.get_connection()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 638, in get_c
onnection
    self.__connect()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
    connection = pool._invoke_creator(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 493, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  the database system is in recovery mode


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/scheduling_manager.py", line 324, in __attempt_schedule
    workflow_scheduler.schedule(workflow_invocation)
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/schedulers/core.py", line 41, in schedule
    workflow_invocation=workflow_invocation,
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/run.py", line 27, in schedule
    return __invoke(trans, workflow, workflow_run_config, workflow_invocation)
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/run.py", line 103, in __invoke
    trans.sa_session.flush()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 163, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2536, in flush
    self._flush(objects)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2678, in _flush
    transaction.rollback(_capture_exception=True)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2638, in _flush
    flush_context.execute()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
    rec.execute(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
    uow,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 213, in save_obj
    ) in _organize_states_for_save(base_mapper, states, uowtransaction):
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 374, in _organize_states_for_save
    base_mapper, uowtransaction, states
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 1602, in _connections_for_states
    connection = uowtransaction.transaction.connection(base_mapper)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 314, in connection
    return self._connection_for_bind(bind, execution_options)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 433, in _connection_for_bind
    conn = bind._contextual_connect()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2302, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2340, in _wrap_pool_connect
    e, dialect, self
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1584, in _handle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect
    return fn()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 364, in connect
    return _ConnectionFairy._checkout(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 500, in checkout
    rec._checkin_failed(err)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 497, in checkout
    dbapi_connection = rec.get_connection()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 638, in get_connection
    self.__connect()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
    connection = pool._invoke_creator(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 493, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  the database system is in recovery mode

(Background on this error at: http://sqlalche.me/e/13/e3q8)
raven.base.Client DEBUG 2021-02-21 15:59:11,789 Sending message of length 8362 to https://sentry.galaxyproject.org/api/2/store/
galaxy.workflow.scheduling_manager DEBUG 2021-02-21 15:59:11,789 Attempting to schedule workflow invocation [(535353,)]
Exception in thread WorkflowRequestMonitor.monitor_thread:
Traceback (most recent call last):
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect
    return fn()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 364, in conne
ct
    return _ConnectionFairy._checkout(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 500, in checkout
    rec._checkin_failed(err)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 497, in checkout
    dbapi_connection = rec.get_connection()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 610, in get_connection
    self.__connect()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in
 __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
    connection = pool._invoke_creator(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 493, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  the database system is in recovery mode


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/cvmfs/main.galaxyproject.org/deps/_conda/envs/_galaxy_/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/cvmfs/main.galaxyproject.org/deps/_conda/envs/_galaxy_/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/scheduling_manager.py", line 297, in __monitor
    self.__schedule(workflow_scheduler_id, workflow_scheduler)
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/scheduling_manager.py", line 305, in __schedule
    self.__attempt_schedule(invocation_id, workflow_scheduler)
  File "/cvmfs/main.galaxyproject.org/galaxy/lib/galaxy/workflow/scheduling_manager.py", line 311, in __attempt_schedule
    workflow_invocation = sa_session.query(model.WorkflowInvocation).get(invocation_id)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 1018, in get
    return self._get_impl(ident, loading.load_on_pk_identity)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 1135, in _get_impl
    return db_load_fn(self, primary_key_identity)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/loading.py", line 286, in load_on_pk_identity
    return q.one()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3490, in one
    ret = self.one_or_none()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3459, in one_or_none
    ret = list(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3535, in __it
er__
    return self._execute_and_instances(context)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3557, in _execute_and_instances
    querycontext, self._connection_from_session, close_with_result=True
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3572, in _get_bind_args
    mapper=self._bind_mapper(), clause=querycontext.statement, **kw
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3550, in _connection_from_session
    conn = self.session.connection(**kw)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1141, in connection
    execution_options=execution_options,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1150, in _connection_for_bind
    conn = engine._contextual_connect(**kw)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2302, in _contextual_connect
    self._wrap_pool_connect(self.pool.connect, None),
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2340, in _wrap_pool_connect
    e, dialect, self
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1584, in _h
andle_dbapi_exception_noconnection
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2336, in _wrap_pool_connect
    return fn()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 364, in connect
    return _ConnectionFairy._checkout(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 778, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 500, in checkout
    rec._checkin_failed(err)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 497, in check
out
    dbapi_connection = rec.get_connection()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 610, in get_connection
    self.__connect()
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 661, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
    with_traceback=exc_tb,
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 656, in __connect
    connection = pool._invoke_creator(self)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
    return dialect.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 493, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/cvmfs/main.galaxyproject.org/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  the database system is in recovery mode

# Fixed code
def __monitor(self):
    to_monitor = self.workflow_scheduling_manager.active_workflow_schedulers
    while self.monitor_running:
        try:
            if self.invocation_grabber:
                self.invocation_grabber.grab_unhandled_items()

            monitor_step_timer = self.app.execution_timer_factory.get_timer(
                "internal.galaxy.workflows.scheduling_manager.monitor_step",
                "Workflow scheduling manager monitor step complete.",
            )
            for workflow_scheduler_id, workflow_scheduler in to_monitor.items():
                if not self.monitor_running:
                    return

                self.__schedule(workflow_scheduler_id, workflow_scheduler)
            log.trace(monitor_step_timer.to_str())
        except Exception:
            log.exception("An exception occured scheduling while scheduling workflows")
        self._monitor_sleep(1)