Fix: Issue where cron would end up in nested transaction blocks and unable to escape.

This commit is contained in:
Ben Rog-Wilhelm 2023-05-01 00:00:05 -05:00
parent 94309ed257
commit 46a392ba31

View file

@ -87,8 +87,9 @@ def _run_tasks(db_session_factory: scoped_session):
running task does not lock the entire table for its entire run, which would
for example, prevent any statistics about status from being gathered.
'''
db: Session = db_session_factory()
db: Session
with db_session_factory() as db:
with _acquire_lock_exclusive(db, RepeatableTask.__tablename__):
now: datetime = datetime.now(tz=timezone.utc)
@ -116,10 +117,11 @@ def _run_tasks(db_session_factory: scoped_session):
task.run_time_last = now
task.run_state_enum = ScheduledTaskState.RUNNING
# This *must* happen before we start doing db queries, including sqlalchemy db queries
db.begin()
task_debug_identifier = f"(ID {task.id}:{task.label})"
logging.info(f"Running task {task_debug_identifier}")
db.begin()
run: RepeatableTaskRun = task.run(db, task.run_time_last_or_created_utc)
if run.exception: