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()