fixing a race condition in celery task wrapper. could randomly blow up any task. (#2321)

This commit is contained in:
rkuo-danswer authored and GitHub committed 2024-09-04 04:17:29 +00:00
1 parent c358c91e4c
commit 630e2248bd
3 files changed
+11 -5

No files matched your search

+9 -2
View File
@@ -93,9 +93,16 @@ def build_apply_async_wrapper(build_name_fn: Callable[..., str]) -> Callable[[AA
kwargs_for_build_name = kwargs or {}
task_name = build_name_fn(*args_for_build_name, **kwargs_for_build_name)
with Session(get_sqlalchemy_engine()) as db_session:
# mark the task as started
# register_task must come before fn = apply_async or else the task
# might run mark_task_start (and crash) before the task row exists
db_task = register_task(task_name, db_session)
task = fn(args, kwargs, *other_args, **other_kwargs)
register_task(task.id, task_name, db_session)
# we update the celery task id for diagnostic purposes
# but it isn't currently used by any code
db_task.task_id = task.id
db_session.commit()
return task
+1 -1
View File
@@ -1409,7 +1409,7 @@ class TaskQueueState(Base):
__tablename__ = "task_queue_jobs"
id: Mapped[int] = mapped_column(primary_key=True)
# Celery task id
# Celery task id. currently only for readability/diagnostics
task_id: Mapped[str] = mapped_column(String)
# For any job type, this would be the same
task_name: Mapped[str] = mapped_column(String)
+1 -2
View File
@@ -44,12 +44,11 @@ def get_latest_task_by_type(
def register_task(
task_id: str,
task_name: str,
db_session: Session,
) -> TaskQueueState:
new_task = TaskQueueState(
task_id=task_id, task_name=task_name, status=TaskStatus.PENDING
task_id="", task_name=task_name, status=TaskStatus.PENDING
)
db_session.add(new_task)