fix file remove
Showing
1 changed file
with
26 additions
and
6 deletions
| 1 | import os | 1 | import os |
| 2 | import time | 2 | import time |
| 3 | import json | 3 | import json |
| 4 | import shutil | ||
| 4 | import base64 | 5 | import base64 |
| 5 | import signal | 6 | import signal |
| 6 | import asyncio | 7 | import asyncio |
| ... | @@ -591,21 +592,40 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -591,21 +592,40 @@ class Command(BaseCommand, LoggerMixin): |
| 591 | doc, business_type = self.get_doc_object(task_str) | 592 | doc, business_type = self.get_doc_object(task_str) |
| 592 | doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, str(doc.id)) | 593 | doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, str(doc.id)) |
| 593 | excel_path = os.path.join(doc_data_path, '{0}.xlsx'.format(doc.id)) | 594 | excel_path = os.path.join(doc_data_path, '{0}.xlsx'.format(doc.id)) |
| 594 | img_save_path = os.path.join(doc_data_path, 'img') | 595 | src_excel_path = os.path.join(doc_data_path, 'src.xlsx') |
| 595 | # wb.save(src_excel_path) | 596 | wb.save(src_excel_path) |
| 596 | count_list = wb.rebuild(merged_bs_summary, license_summary, res_list, doc.document_scheme) | 597 | count_list = wb.rebuild(merged_bs_summary, license_summary, res_list, doc.document_scheme) |
| 597 | wb.save(excel_path) | 598 | wb.save(excel_path) |
| 598 | except Exception as e: | 599 | except Exception as e: |
| 599 | with lock: | 600 | with lock: |
| 600 | if task_str in res_dict: | 601 | if task_str in res_dict: |
| 601 | del res_dict[task_str] | 602 | del res_dict[task_str] |
| 602 | doc, _ = self.get_doc_object(task_str) | 603 | doc, business_type = self.get_doc_object(task_str) |
| 603 | doc.status = DocStatus.PROCESS_FAILED.value | 604 | doc.status = DocStatus.PROCESS_FAILED.value |
| 604 | doc.save() | 605 | doc.save() |
| 605 | self.cronjob_log.error('{0} [process failed (res to wb)] [task={1}] [error={2}]'.format( | 606 | self.cronjob_log.error('{0} [process failed (res to wb)] [task={1}] [error={2}]'.format( |
| 606 | self.log_base, task_str, traceback.format_exc())) | 607 | self.log_base, task_str, traceback.format_exc())) |
| 608 | try: | ||
| 609 | doc_data_path = os.path.join(self.data_dir, business_type, consts.TMP_DIR_NAME, str(doc.id)) | ||
| 610 | img_save_path = os.path.join(doc_data_path, 'img') | ||
| 611 | shutil.rmtree(img_save_path, ignore_errors=True) | ||
| 612 | pdf_path = os.path.join(doc_data_path, '{0}.pdf'.format(doc.id)) | ||
| 613 | os.remove(pdf_path) | ||
| 614 | except Exception as e: | ||
| 615 | self.cronjob_log.error('{0} [file remove failed] [task={1}] [error={2}]'.format( | ||
| 616 | self.log_base, task_str, traceback.format_exc())) | ||
| 607 | else: | 617 | else: |
| 608 | try: | 618 | try: |
| 619 | img_save_path = os.path.join(doc_data_path, 'img') | ||
| 620 | write_zip_file(img_save_path, os.path.join(doc_data_path, '{0}_img.zip'.format(doc.id))) | ||
| 621 | shutil.rmtree(img_save_path, ignore_errors=True) | ||
| 622 | # pdf_path = os.path.join(doc_data_path, '{0}.pdf'.format(doc.id)) | ||
| 623 | # os.remove(pdf_path) | ||
| 624 | # os.remove(src_excel_path) | ||
| 625 | except Exception as e: | ||
| 626 | self.cronjob_log.error('{0} [file remove failed] [task={1}] [error={2}]'.format( | ||
| 627 | self.log_base, task_str, traceback.format_exc())) | ||
| 628 | try: | ||
| 609 | # 5.上传至EDMS | 629 | # 5.上传至EDMS |
| 610 | for times in range(consts.RETRY_TIMES): | 630 | for times in range(consts.RETRY_TIMES): |
| 611 | try: | 631 | try: |
| ... | @@ -629,8 +649,6 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -629,8 +649,6 @@ class Command(BaseCommand, LoggerMixin): |
| 629 | doc.save() | 649 | doc.save() |
| 630 | self.cronjob_log.error('{0} [process failed (edms upload)] [task={1}] [error={2}]'.format( | 650 | self.cronjob_log.error('{0} [process failed (edms upload)] [task={1}] [error={2}]'.format( |
| 631 | self.log_base, task_str, traceback.format_exc())) | 651 | self.log_base, task_str, traceback.format_exc())) |
| 632 | write_zip_file(img_save_path, os.path.join(doc_data_path, '{0}_img.zip'.format(doc.id))) | ||
| 633 | |||
| 634 | else: | 652 | else: |
| 635 | doc.status = DocStatus.COMPLETE.value | 653 | doc.status = DocStatus.COMPLETE.value |
| 636 | doc.end_time = timezone.now() | 654 | doc.end_time = timezone.now() |
| ... | @@ -640,7 +658,9 @@ class Command(BaseCommand, LoggerMixin): | ... | @@ -640,7 +658,9 @@ class Command(BaseCommand, LoggerMixin): |
| 640 | setattr(doc, field, count) | 658 | setattr(doc, field, count) |
| 641 | doc.save() | 659 | doc.save() |
| 642 | self.cronjob_log.info('{0} [process complete] [task={1}]'.format(self.log_base, task_str)) | 660 | self.cronjob_log.info('{0} [process complete] [task={1}]'.format(self.log_base, task_str)) |
| 643 | write_zip_file(img_save_path, os.path.join(doc_data_path, '{0}_img.zip'.format(doc.id))) | 661 | # os.remove(excel_path) |
| 662 | |||
| 663 | |||
| 644 | 664 | ||
| 645 | # TODO 细化文件状态,不同异常状态,归还队列,重试时采取不同的处理 | 665 | # TODO 细化文件状态,不同异常状态,归还队列,重试时采取不同的处理 |
| 646 | # TODO 异常邮件通知 | 666 | # TODO 异常邮件通知 | ... | ... |
-
Please register or sign in to post a comment