From 4d11516869c9717cd8b6f6dd276eedc66052f7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=89=8D=E6=98=8E?= <909355014@qq.com> Date: Mon, 26 Sep 2022 17:46:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=A1=E7=89=87=E4=B8=8D=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=81=9A=E9=A2=9D=E5=A4=96=E5=A4=84=E7=90=86/=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=B8=85=E9=99=A4=E6=97=A5=E5=BF=97=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/hrm/services.py | 13 +++++++++++-- apps/monitor/tasks.py | 14 ++++++++++++++ apps/third/dahua.py | 2 +- apps/third/speaker.py | 2 +- apps/third/tasks.py | 18 ++++++++++++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 apps/third/tasks.py diff --git a/apps/hrm/services.py b/apps/hrm/services.py index b3d41d08..817522e4 100755 --- a/apps/hrm/services.py +++ b/apps/hrm/services.py @@ -1,6 +1,8 @@ +from rest_framework.exceptions import ParseError, APIException import logging import time from apps.hrm.models import ClockRecord, Employee +from apps.third.errors import DH_REQUEST_ERROR from apps.third.models import TDevice from apps.third.tapis import dhapis from apps.third.clients import dhClient @@ -149,7 +151,7 @@ class HrmService: "endDate": endDate } _, res = dhClient.request(**dhapis['card_add'], json=json_data) - time.sleep(10) # 等待确保生成卡片 + time.sleep(8) # 等待确保生成卡片 cls.save(ep, data={'dh_face_card': cardNumber}) return cardNumber @@ -177,7 +179,14 @@ class HrmService: "timeQuantumId": 1, "cardPrivilegeDetails": details } - dhClient.request(**dhapis['card_door_authority'], json=json_data) + is_ok, res = dhClient.request(**dhapis['card_door_authority'], json=json_data, raise_exception=False) + if is_ok == 'fail' and '44999999' in res['code']: + time.sleep(6) + dhClient.request(**dhapis['card_door_authority'], json=json_data) + elif is_ok == 'fail': + raise ParseError(**res) + elif is_ok == 'error': + raise APIException(**DH_REQUEST_ERROR) cls.save(ep, data={'dh_dchannels': dh_dchannels}) return dh_dchannels diff --git a/apps/monitor/tasks.py b/apps/monitor/tasks.py index 77a41992..7e05f21b 100644 --- a/apps/monitor/tasks.py +++ b/apps/monitor/tasks.py @@ -1,4 +1,18 @@ # Create your tasks here from __future__ import absolute_import, unicode_literals +from datetime import timedelta +from apps.monitor.models import DrfRequestLog from apps.utils.tasks import CustomTask from celery import shared_task +from django.utils import timezone + + +@shared_task(base=CustomTask) +def clear_drf_log(): + """清除7天前的日志记录 + + 清除7天前的日志记录 + """ + now = timezone.now() + days7_ago = now - timedelta(days=7) + DrfRequestLog.objects.filter(create_time__lte=days7_ago).delete() diff --git a/apps/third/dahua.py b/apps/third/dahua.py index 7133d1f1..2a0ad635 100644 --- a/apps/third/dahua.py +++ b/apps/third/dahua.py @@ -92,7 +92,7 @@ class DhClient: self.handle_log(result='fail', response=ret) if raise_exception: raise ParseError(**err_detail) - return 'fail', dict(detail=detail, code='dh_'+str(ret['code'])) + return 'fail', err_detail # self.handle_log(result='success', response=ret) # 成功的日志就不记录了 return 'success', ret['data'] if 'data' in ret else None diff --git a/apps/third/speaker.py b/apps/third/speaker.py index a29563a5..793d1ec6 100644 --- a/apps/third/speaker.py +++ b/apps/third/speaker.py @@ -86,7 +86,7 @@ class SpClient: self.handle_log(result='fail', response=ret) if raise_exception: raise ParseError(**err_detail) - return 'fail', dict(detail=detail, code='sp_'+str(ret['code'])) + return 'fail', err_detail # self.handle_log(result='success', response=ret) return 'success', ret self.handle_log(result='error', errors=traceback.format_exc()) diff --git a/apps/third/tasks.py b/apps/third/tasks.py new file mode 100644 index 00000000..7595c237 --- /dev/null +++ b/apps/third/tasks.py @@ -0,0 +1,18 @@ +# Create your tasks here +from __future__ import absolute_import, unicode_literals +from datetime import timedelta +from apps.utils.tasks import CustomTask +from apps.third.models import Tlog +from celery import shared_task +from django.utils import timezone + + +@shared_task(base=CustomTask) +def clear_tlog(): + """清除7天前的日志记录 + + 清除7天前的日志记录 + """ + now = timezone.now() + days7_ago = now - timedelta(days=7) + Tlog.objects.filter(create_time__lte=days7_ago).delete()