fix:exam 修改考试的证书
This commit is contained in:
parent
aa37c99ac4
commit
03b273d6cd
|
@ -688,8 +688,11 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
|
|||
courese_ids = [i.id for i in course]
|
||||
current_date = now_data.strftime('%Y-%m-%d')
|
||||
cer_number = now_data.strftime('%Y%m%d')
|
||||
# redis 连接并获取锁
|
||||
cache.set("certificate", str(uuid.uuid4()), timeout=60)
|
||||
# 获取锁
|
||||
iden = acquire_lock('certificate')
|
||||
if iden is None:
|
||||
raise ParseError("系统忙,请稍后再试。", status=503)
|
||||
try:
|
||||
# 查询证明编号创建时间为最后一个
|
||||
cer = Certificate.objects.latest('证书编号')
|
||||
if cer:
|
||||
|
@ -711,10 +714,12 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
|
|||
serializer = CertificateSerializer(data=data_dict)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
val = cache.get("certificate")
|
||||
if val: # 如果存在,则删除
|
||||
# 释放锁
|
||||
cache.delete("certificate")
|
||||
except Exception as e:
|
||||
import traceback
|
||||
raise ParseError(traceback.print_exc())
|
||||
finally:
|
||||
release_lock('certificate', iden)
|
||||
|
||||
er.took = (now - er.create_time).total_seconds()
|
||||
er.end_time = now
|
||||
er.belong_dept=request.user.dept
|
||||
|
@ -723,31 +728,14 @@ class ExamRecordViewSet(ListModelMixin, DestroyModelMixin, RetrieveModelMixin, G
|
|||
return Response(ExamRecordListSerializer(instance=er).data)
|
||||
|
||||
|
||||
def acquire_lock(redis_conn, lock_name, ttl):
|
||||
def acquire_lock(lock_name, timeout=60):
|
||||
identifier = str(uuid.uuid4())
|
||||
lock = redis_conn.set(lock_name, identifier, nx=True, ex=ttl)
|
||||
lock = cache.add(lock_name, identifier, timeout=timeout, nx=True)
|
||||
return identifier if lock else None
|
||||
|
||||
|
||||
import redis
|
||||
def release_lock(redis_conn, lock_name, identifier):
|
||||
with redis_conn.pipeline() as pipe:
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
pipe.watch(lock_name)
|
||||
# 使用 multi/exec 模式来确保原子性
|
||||
if pipe.get(lock_name) == identifier:
|
||||
pipe.multi()
|
||||
pipe.delete(lock_name)
|
||||
pipe.execute()
|
||||
def release_lock(lock_name, identifier):
|
||||
if cache.get(lock_name) == identifier:
|
||||
cache.delete(lock_name)
|
||||
return True
|
||||
# 如果锁已经被修改,取消监视
|
||||
pipe.unwatch()
|
||||
return False
|
||||
except redis.exceptions.WatchError:
|
||||
# 如果在监视期间锁被修改,重试
|
||||
continue
|
||||
except redis.exceptions.RedisError as e:
|
||||
print(f"Error releasing lock: {e}")
|
||||
return False
|
||||
else:
|
||||
raise ParseError('Lock identifier does not match')
|
|
@ -1,5 +1,5 @@
|
|||
from .settings import *
|
||||
DEBUG = True
|
||||
DEBUG = False
|
||||
DATABASES = {
|
||||
# 'default': {
|
||||
# 'ENGINE': 'django.db.backends.postgresql',
|
||||
|
|
Loading…
Reference in New Issue