ocr_process添加try-except处理在后半部分
Showing
1 changed file
with
65 additions
and
0 deletions
| ... | @@ -910,13 +910,20 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -910,13 +910,20 @@ class Command(BaseCommand, LoggerMixin): |
| 910 | return date_res | 910 | return date_res |
| 911 | 911 | ||
| 912 | def get_validate_date(self, date_list): | 912 | def get_validate_date(self, date_list): |
| 913 | # 添加 try-except 处理 | ||
| 914 | try: | ||
| 913 | for date_str in date_list: | 915 | for date_str in date_list: |
| 914 | for format_str in consts.DATE_FORMAT: | 916 | for format_str in consts.DATE_FORMAT: |
| 915 | date_res = self.date_format(date_str, format_str) | 917 | date_res = self.date_format(date_str, format_str) |
| 916 | if isinstance(date_res, date): | 918 | if isinstance(date_res, date): |
| 917 | return date_res | 919 | return date_res |
| 920 | except Exception as e: | ||
| 921 | self.online_log.error('{0} [get_validate_date error] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 922 | return None | ||
| 918 | 923 | ||
| 919 | def merge_card(self, bs_summary): | 924 | def merge_card(self, bs_summary): |
| 925 | # 添加 try-except 处理 | ||
| 926 | try: | ||
| 920 | classify_info = {} | 927 | classify_info = {} |
| 921 | merged_bs_summary = {} | 928 | merged_bs_summary = {} |
| 922 | sorted_card = sorted(bs_summary.keys(), key=lambda x: bs_summary[x]['count'], reverse=True) | 929 | sorted_card = sorted(bs_summary.keys(), key=lambda x: bs_summary[x]['count'], reverse=True) |
| ... | @@ -948,15 +955,25 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -948,15 +955,25 @@ class Command(BaseCommand, LoggerMixin): |
| 948 | merged_bs_summary[main_card]['role'] = self.get_most(merged_bs_summary[main_card]['role']) | 955 | merged_bs_summary[main_card]['role'] = self.get_most(merged_bs_summary[main_card]['role']) |
| 949 | del bs_summary | 956 | del bs_summary |
| 950 | return merged_bs_summary, classify_info | 957 | return merged_bs_summary, classify_info |
| 958 | except Exception as e: | ||
| 959 | self.online_log.error('{0} [merge_card error] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 960 | return {}, {} | ||
| 951 | 961 | ||
| 952 | def prune_bs_summary(self, bs_summary): | 962 | def prune_bs_summary(self, bs_summary): |
| 963 | # 添加 try-except 处理 | ||
| 964 | try: | ||
| 953 | for summary in bs_summary.values(): | 965 | for summary in bs_summary.values(): |
| 954 | del summary['count'] | 966 | del summary['count'] |
| 955 | summary['classify'] = self.get_most(summary['classify']) | 967 | summary['classify'] = self.get_most(summary['classify']) |
| 956 | summary['role'] = self.get_most(summary['role']) | 968 | summary['role'] = self.get_most(summary['role']) |
| 957 | return bs_summary | 969 | return bs_summary |
| 970 | except Exception as e: | ||
| 971 | self.online_log.error('{0} [prune_bs_summary error] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 972 | return bs_summary | ||
| 958 | 973 | ||
| 959 | def rebuild_bs(self, bs_summary): | 974 | def rebuild_bs(self, bs_summary): |
| 975 | # 添加 try-except 处理 | ||
| 976 | try: | ||
| 960 | # bs_summary = { | 977 | # bs_summary = { |
| 961 | # '卡号': { | 978 | # '卡号': { |
| 962 | # 'classify': 0, | 979 | # 'classify': 0, |
| ... | @@ -989,8 +1006,13 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -989,8 +1006,13 @@ class Command(BaseCommand, LoggerMixin): |
| 989 | } | 1006 | } |
| 990 | ) | 1007 | ) |
| 991 | return res | 1008 | return res |
| 1009 | except Exception as e: | ||
| 1010 | self.online_log.error('{0} [rebuild_bs error] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 1011 | return [] | ||
| 992 | 1012 | ||
| 993 | def rebuild_contract(self, license_summary, contract_result_compare): | 1013 | def rebuild_contract(self, license_summary, contract_result_compare): |
| 1014 | # 添加 try-except 处理 | ||
| 1015 | try: | ||
| 994 | for classify, page_info_dict in contract_result_compare.items(): | 1016 | for classify, page_info_dict in contract_result_compare.items(): |
| 995 | if classify == consts.CONTRACT_CLASSIFY: | 1017 | if classify == consts.CONTRACT_CLASSIFY: |
| 996 | res = {} | 1018 | res = {} |
| ... | @@ -1090,8 +1112,12 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -1090,8 +1112,12 @@ class Command(BaseCommand, LoggerMixin): |
| 1090 | res[key] = page_info_dict.get(str(pno1), {}).get(key1) | 1112 | res[key] = page_info_dict.get(str(pno1), {}).get(key1) |
| 1091 | res.setdefault(consts.IMG_PATH_KEY, dict())[key] = page_info_dict.get(str(pno1), {}).get(consts.IMG_PATH_KEY, '') | 1113 | res.setdefault(consts.IMG_PATH_KEY, dict())[key] = page_info_dict.get(str(pno1), {}).get(consts.IMG_PATH_KEY, '') |
| 1092 | license_summary[classify] = [res] | 1114 | license_summary[classify] = [res] |
| 1115 | except Exception as e: | ||
| 1116 | self.online_log.error('{0} [rebuild_contract error] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 1093 | 1117 | ||
| 1094 | def rebuild_bs_summary(self, bs_summary, unknown_summary): | 1118 | def rebuild_bs_summary(self, bs_summary, unknown_summary): |
| 1119 | # 添加 try-except 处理 | ||
| 1120 | try: | ||
| 1095 | # bs_summary = { | 1121 | # bs_summary = { |
| 1096 | # '卡号': { | 1122 | # '卡号': { |
| 1097 | # 'count': 100, | 1123 | # 'count': 100, |
| ... | @@ -1195,8 +1221,13 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -1195,8 +1221,13 @@ class Command(BaseCommand, LoggerMixin): |
| 1195 | summary['end_date'] = self.get_validate_date(summary['end_date']) | 1221 | summary['end_date'] = self.get_validate_date(summary['end_date']) |
| 1196 | # summary['confidence'] = max(summary['confidence']) | 1222 | # summary['confidence'] = max(summary['confidence']) |
| 1197 | return merged_bs_summary | 1223 | return merged_bs_summary |
| 1224 | except Exception as e: | ||
| 1225 | self.online_log.error('{0} [rebuild_bs_summary error] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 1226 | return {} | ||
| 1198 | 1227 | ||
| 1199 | def zip_2_pdfs(self, zip_task_queue, error_list): | 1228 | def zip_2_pdfs(self, zip_task_queue, error_list): |
| 1229 | # 添加 try-except 处理 | ||
| 1230 | try: | ||
| 1200 | while len(error_list) == 0: | 1231 | while len(error_list) == 0: |
| 1201 | # 1. 从redis队列中读取任务: AFC_111_0 | 1232 | # 1. 从redis队列中读取任务: AFC_111_0 |
| 1202 | task_str = rh.dequeue_zip() | 1233 | task_str = rh.dequeue_zip() |
| ... | @@ -1324,8 +1355,14 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -1324,8 +1355,14 @@ class Command(BaseCommand, LoggerMixin): |
| 1324 | except Exception as e: | 1355 | except Exception as e: |
| 1325 | self.online_log.error('{0} [zip_2_pdfs] [process error (db save)] [task={1}] [error={2}]'.format( | 1356 | self.online_log.error('{0} [zip_2_pdfs] [process error (db save)] [task={1}] [error={2}]'.format( |
| 1326 | self.log_base, task_str, traceback.format_exc())) | 1357 | self.log_base, task_str, traceback.format_exc())) |
| 1358 | except Exception as e: | ||
| 1359 | self.online_log.error('{0} [process error (zip_2_pdfs out)] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 1360 | # 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行 | ||
| 1361 | # error_list.append(1) | ||
| 1327 | 1362 | ||
| 1328 | def pdf_2_img_2_queue(self, img_queue, todo_count_dict, lock, error_list, res_dict, finish_queue, zip_task_queue): | 1363 | def pdf_2_img_2_queue(self, img_queue, todo_count_dict, lock, error_list, res_dict, finish_queue, zip_task_queue): |
| 1364 | # 添加 try-except 处理 | ||
| 1365 | try: | ||
| 1329 | while self.switch: | 1366 | while self.switch: |
| 1330 | try: | 1367 | try: |
| 1331 | task_str = zip_task_queue.get(block=False) | 1368 | task_str = zip_task_queue.get(block=False) |
| ... | @@ -1653,8 +1690,14 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -1653,8 +1690,14 @@ class Command(BaseCommand, LoggerMixin): |
| 1653 | self.e_log_base, traceback.format_exc())) | 1690 | self.e_log_base, traceback.format_exc())) |
| 1654 | error_list.append(1) | 1691 | error_list.append(1) |
| 1655 | return | 1692 | return |
| 1693 | except Exception as e: | ||
| 1694 | self.online_log.error('{0} [process error (pdf_2_img_2_queue out)] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 1695 | # 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行 | ||
| 1696 | # error_list.append(1) | ||
| 1656 | 1697 | ||
| 1657 | def img_2_ocr_1(self, img_queue, todo_count_dict, res_dict, finish_queue, lock, url, error_list): | 1698 | def img_2_ocr_1(self, img_queue, todo_count_dict, res_dict, finish_queue, lock, url, error_list): |
| 1699 | # 添加 try-except 处理 | ||
| 1700 | try: | ||
| 1658 | while len(error_list) == 0 or not img_queue.empty(): | 1701 | while len(error_list) == 0 or not img_queue.empty(): |
| 1659 | try: | 1702 | try: |
| 1660 | channel, img_path, text_list = img_queue.get(block=False) | 1703 | channel, img_path, text_list = img_queue.get(block=False) |
| ... | @@ -1730,8 +1773,14 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -1730,8 +1773,14 @@ class Command(BaseCommand, LoggerMixin): |
| 1730 | except Exception as e: | 1773 | except Exception as e: |
| 1731 | self.online_log.error('{0} [process error (store ocr res)] [img_path={1}] [error={2}]'.format( | 1774 | self.online_log.error('{0} [process error (store ocr res)] [img_path={1}] [error={2}]'.format( |
| 1732 | self.log_base, img_path, traceback.format_exc())) | 1775 | self.log_base, img_path, traceback.format_exc())) |
| 1776 | except Exception as e: | ||
| 1777 | self.online_log.error('{0} [process error (img_2_ocr_1 out)] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 1778 | # 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行 | ||
| 1779 | # error_list.append(1 | ||
| 1733 | 1780 | ||
| 1734 | def res_2_wb(self, res_dict, img_queue, finish_queue, error_list): | 1781 | def res_2_wb(self, res_dict, img_queue, finish_queue, error_list): |
| 1782 | # 添加 try-except 处理 | ||
| 1783 | try: | ||
| 1735 | self.online_log.info('{0} [res_2_wb] [get task] [queue running] [finish_queue_size={1}]'.format(self.log_base, finish_queue.qsize())) | 1784 | self.online_log.info('{0} [res_2_wb] [get task] [queue running] [finish_queue_size={1}]'.format(self.log_base, finish_queue.qsize())) |
| 1736 | while len(error_list) == 0 or not img_queue.empty() or not finish_queue.empty(): | 1785 | while len(error_list) == 0 or not img_queue.empty() or not finish_queue.empty(): |
| 1737 | try: | 1786 | try: |
| ... | @@ -2501,8 +2550,14 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -2501,8 +2550,14 @@ class Command(BaseCommand, LoggerMixin): |
| 2501 | self.online_log.error('{0} [process error (pdf & img remove)] [task={1}] [error={2}]'.format( | 2550 | self.online_log.error('{0} [process error (pdf & img remove)] [task={1}] [error={2}]'.format( |
| 2502 | self.log_base, task_str, traceback.format_exc())) | 2551 | self.log_base, task_str, traceback.format_exc())) |
| 2503 | self.online_log.info('{0} [res_2_wb after while] [len(error_list)={1}] [img_queue={2}] [finish_queue={3}]'.format(self.log_base, len(error_list), img_queue.empty(), finish_queue.empty())) | 2552 | self.online_log.info('{0} [res_2_wb after while] [len(error_list)={1}] [img_queue={2}] [finish_queue={3}]'.format(self.log_base, len(error_list), img_queue.empty(), finish_queue.empty())) |
| 2553 | except Exception as e: | ||
| 2554 | self.online_log.error('{0} [res_2_wb failed] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 2555 | # 本应该在 error_list 中添加内容, 但是这里没有添加, 使得进程可以一直运行 | ||
| 2556 | # error_list.append(1) | ||
| 2504 | 2557 | ||
| 2505 | def handle(self, *args, **kwargs): | 2558 | def handle(self, *args, **kwargs): |
| 2559 | # 添加 try-except 处理 | ||
| 2560 | try: | ||
| 2506 | db.close_old_connections() | 2561 | db.close_old_connections() |
| 2507 | lock = Lock() | 2562 | lock = Lock() |
| 2508 | with Manager() as manager: | 2563 | with Manager() as manager: |
| ... | @@ -2537,9 +2592,13 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -2537,9 +2592,13 @@ class Command(BaseCommand, LoggerMixin): |
| 2537 | p.join() | 2592 | p.join() |
| 2538 | 2593 | ||
| 2539 | self.online_log.info('{0} [stop safely]'.format(self.log_base)) | 2594 | self.online_log.info('{0} [stop safely]'.format(self.log_base)) |
| 2595 | except Exception as e: | ||
| 2596 | self.online_log.error('{0} [stop failed] [error={1}]'.format(self.log_base, traceback.format_exc())) | ||
| 2540 | 2597 | ||
| 2541 | @transaction.atomic | 2598 | @transaction.atomic |
| 2542 | def atomicSaveDBAFC(self,result_class,doc,license_summary,ic_merge,rp_merge,task_str, financial_statement_dict, financial_explanation_dict): | 2599 | def atomicSaveDBAFC(self,result_class,doc,license_summary,ic_merge,rp_merge,task_str, financial_statement_dict, financial_explanation_dict): |
| 2600 | # 添加 try-except 处理 | ||
| 2601 | try: | ||
| 2543 | with transaction.atomic('afc'): | 2602 | with transaction.atomic('afc'): |
| 2544 | res_obj = result_class.objects.using('afc').select_for_update().filter(application_id=doc.application_id).first() | 2603 | res_obj = result_class.objects.using('afc').select_for_update().filter(application_id=doc.application_id).first() |
| 2545 | self.online_log.info('{0} [sql lock AFC application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) | 2604 | self.online_log.info('{0} [sql lock AFC application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) |
| ... | @@ -2578,9 +2637,13 @@ def atomicSaveDBAFC(self,result_class,doc,license_summary,ic_merge,rp_merge,task | ... | @@ -2578,9 +2637,13 @@ def atomicSaveDBAFC(self,result_class,doc,license_summary,ic_merge,rp_merge,task |
| 2578 | res_obj.save() | 2637 | res_obj.save() |
| 2579 | self.online_log.info('{0} [sql lock release application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) | 2638 | self.online_log.info('{0} [sql lock release application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) |
| 2580 | return res_obj | 2639 | return res_obj |
| 2640 | except Exception as e: | ||
| 2641 | self.online_log.error('{0} [process error (afc db save)] [task={1}] [error={2}]'.format(self.log_base, task_str, traceback.format_exc())) | ||
| 2581 | 2642 | ||
| 2582 | @transaction.atomic | 2643 | @transaction.atomic |
| 2583 | def atomicSaveDBHIL(self,result_class,doc,license_summary,ic_merge,rp_merge, task_str, financial_statement_dict, financial_explanation_dict): | 2644 | def atomicSaveDBHIL(self,result_class,doc,license_summary,ic_merge,rp_merge, task_str, financial_statement_dict, financial_explanation_dict): |
| 2645 | # 添加 try-except 处理 | ||
| 2646 | try: | ||
| 2584 | with transaction.atomic('default'): | 2647 | with transaction.atomic('default'): |
| 2585 | res_obj = result_class.objects.using('default').select_for_update().filter(application_id=doc.application_id).first() | 2648 | res_obj = result_class.objects.using('default').select_for_update().filter(application_id=doc.application_id).first() |
| 2586 | self.online_log.info('{0} [sql lock HIL application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) | 2649 | self.online_log.info('{0} [sql lock HIL application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) |
| ... | @@ -2619,3 +2682,5 @@ def atomicSaveDBHIL(self,result_class,doc,license_summary,ic_merge,rp_merge, tas | ... | @@ -2619,3 +2682,5 @@ def atomicSaveDBHIL(self,result_class,doc,license_summary,ic_merge,rp_merge, tas |
| 2619 | res_obj.save() | 2682 | res_obj.save() |
| 2620 | self.online_log.info('{0} [sql lock release application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) | 2683 | self.online_log.info('{0} [sql lock release application_id={1} doc_id={2}]'.format(self.log_base, doc.application_id,doc.id)) |
| 2621 | return res_obj | 2684 | return res_obj |
| 2685 | except Exception as e: | ||
| 2686 | self.online_log.error('{0} [process error (hil db save)] [task={1}] [error={2}]'.format(self.log_base, task_str, traceback.format_exc())) | ... | ... |
-
Please register or sign in to post a comment