From a06e6858ea149b759d9702ee6a27109c0eb3c09e Mon Sep 17 00:00:00 2001 From: InitialMoon Date: Tue, 27 Aug 2024 18:29:30 +0800 Subject: [PATCH 1/2] fix celery bug --- contest/contest/tasks.py | 72 +++++++++++++++++++--------------------- justfile | 7 ++++ 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/contest/contest/tasks.py b/contest/contest/tasks.py index 4865824..38035ed 100644 --- a/contest/contest/tasks.py +++ b/contest/contest/tasks.py @@ -1,5 +1,3 @@ -"""定时清理""" - import logging import redis @@ -24,57 +22,57 @@ @shared_task def auto_save_redis_to_database() -> None: - """同步 Redis 缓存到数据库""" # 获取 Redis 连接 r = redis.Redis(host="127.0.0.1", port=6379, db=1) # 使用 scan_iter 获取所有键 keys = r.scan_iter("*_ddl") + if keys is None: + return for key in keys: ddl_key = key.decode("utf-8")[3:] ddl = cache.get(ddl_key) now = timezone.now() if ddl is not None: if ddl < now: - draft_response = DraftResponse.objects.get(pk=ddl_key[:-4]) + try: + draft_response = DraftResponse.objects.get(id=int(ddl_key[:-4])) + cache_key = f"{ddl_key[:-4]}_json" + # # 从 Redis 获取现有的答案缓存 + cached_answers = cache.get(cache_key, {}) - cache_key = f"{ddl_key[:-4]}_json" - # # 从 Redis 获取现有的答案缓存 - cached_answers = cache.get(cache_key, {}) + if cached_answers is not None: # 防止未提交的是白卷 + for question_id, choice_id in cached_answers.items(): + # Filter out tokens + if not question_id.startswith("question-"): + continue - for question_id, choice_id in cached_answers.items(): - # Filter out tokens - if not question_id.startswith("question-"): - continue + if not isinstance(choice_id, str) or not choice_id.startswith("choice-"): + return - if not isinstance(choice_id, str) or not choice_id.startswith("choice-"): - return + answer: DraftAnswer = get_object_or_404( + draft_response.answer_set, + question_id=int(question_id.removeprefix("question-")), + ) - answer: DraftAnswer = get_object_or_404( - draft_response.answer_set, - question_id=int(question_id.removeprefix("question-")), - ) + answer.choice = get_object_or_404( + Choice.objects, + pk=int(choice_id.removeprefix("choice-")), + question=answer.question, + ) - answer.choice = get_object_or_404( - Choice.objects, - pk=int(choice_id.removeprefix("choice-")), - question=answer.question, - ) + answer.save() - answer.save() + # 1. Convert from draft + response, answers = draft_response.finalize(submit_at=timezone.now()) - # 1. Convert from draft - response, answers = draft_response.finalize(submit_at=timezone.now()) + # 2. Save + response.save() + response.answer_set.bulk_create(answers) + draft_response.delete() - # 2. Save - response.save() - response.answer_set.bulk_create(answers) - draft_response.delete() + except DraftResponse.DoesNotExist as e: + print("here is tasks.py 74 line") + print(e) - cache.delete(f"{draft_response.id}_json") - cache.delete(f"{draft_response.id}_ddl") - cache.delete(f"{draft_response.id}_sequence") - else: - pass - # 关闭 Redis 连接 - del keys - r.close() + r.delete(key) + r.delete(':1:' + ddl_key[:-4] + '_json') diff --git a/justfile b/justfile index a4024b4..36bc1ed 100644 --- a/justfile +++ b/justfile @@ -73,3 +73,10 @@ watch-js: # 构建 js build-js: pnpm --dir {{ src_dir }}/js/static_src/ run build + +# celery +task-beat: + cd {{ src_dir }} ; celery -A contest beat -l info + +task-worker: + cd {{ src_dir }} ; celery -A contest worker -P eventlet -l info From 6db0b81d20433e2a553dcc78ebe21ce8fa316213 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 27 Aug 2024 10:33:36 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- contest/contest/tasks.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/contest/contest/tasks.py b/contest/contest/tasks.py index 38035ed..c753582 100644 --- a/contest/contest/tasks.py +++ b/contest/contest/tasks.py @@ -40,13 +40,15 @@ def auto_save_redis_to_database() -> None: # # 从 Redis 获取现有的答案缓存 cached_answers = cache.get(cache_key, {}) - if cached_answers is not None: # 防止未提交的是白卷 + if cached_answers is not None: # 防止未提交的是白卷 for question_id, choice_id in cached_answers.items(): # Filter out tokens if not question_id.startswith("question-"): continue - if not isinstance(choice_id, str) or not choice_id.startswith("choice-"): + if not isinstance(choice_id, str) or not choice_id.startswith( + "choice-" + ): return answer: DraftAnswer = get_object_or_404( @@ -75,4 +77,4 @@ def auto_save_redis_to_database() -> None: print(e) r.delete(key) - r.delete(':1:' + ddl_key[:-4] + '_json') + r.delete(":1:" + ddl_key[:-4] + "_json")