feat: 保存失败的 Ticket ID 到 CSV,支持重试
This commit is contained in:
@@ -24,9 +24,12 @@ WORKERS = 5
|
||||
|
||||
SCRIPT = os.path.join(os.path.dirname(__file__), "sap-c4c-AttachmentFolder.py")
|
||||
ERROR_LOG = os.path.join(os.path.dirname(__file__), "error_log.txt")
|
||||
FAILED_CSV = os.path.join(os.path.dirname(__file__), "failed_tickets.csv")
|
||||
DATASOURCE = os.path.join(os.path.dirname(__file__), "datasource")
|
||||
|
||||
print_lock = threading.Lock()
|
||||
failed_lock = threading.Lock()
|
||||
failed_ids = set()
|
||||
# ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -67,6 +70,11 @@ def log_error(ticket_id, message):
|
||||
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
with open(ERROR_LOG, "a", encoding="utf-8") as f:
|
||||
f.write(f"[{ts}] Ticket {ticket_id}: {message}\n")
|
||||
with failed_lock:
|
||||
if ticket_id not in failed_ids:
|
||||
failed_ids.add(ticket_id)
|
||||
with open(FAILED_CSV, "a", encoding="utf-8", newline="") as f:
|
||||
csv.writer(f).writerow([ticket_id])
|
||||
|
||||
|
||||
def run_ticket(ticket_id, index, total):
|
||||
@@ -130,16 +138,19 @@ def run_ticket(ticket_id, index, total):
|
||||
|
||||
|
||||
def main():
|
||||
global failed_ids
|
||||
print("读取 Ticket ID ...")
|
||||
ids = get_ticket_ids(10)
|
||||
ids = get_ticket_ids()
|
||||
if not ids:
|
||||
print("未找到任何 Ticket ID,请检查 datasource 目录")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"共 {len(ids)} 个 Ticket: {', '.join(ids)}")
|
||||
|
||||
# 清空/创建 error_log
|
||||
# 清空/创建 error_log 和 failed_tickets.csv
|
||||
open(ERROR_LOG, "w").close()
|
||||
open(FAILED_CSV, "w", encoding="utf-8", newline="").close()
|
||||
failed_ids.clear()
|
||||
|
||||
with ThreadPoolExecutor(max_workers=WORKERS) as executor:
|
||||
futures = {executor.submit(run_ticket, tid, i, len(ids)): tid
|
||||
@@ -148,10 +159,12 @@ def main():
|
||||
future.result() # 触发异常传播(已在 run_ticket 内处理)
|
||||
|
||||
print("\n全部完成。")
|
||||
if os.path.getsize(ERROR_LOG) > 0:
|
||||
print(f"有错误,详见 {ERROR_LOG}")
|
||||
if failed_ids:
|
||||
print(f"失败 {len(failed_ids)} 个 Ticket,已保存到 {FAILED_CSV}")
|
||||
if os.path.getsize(ERROR_LOG) > 0:
|
||||
print(f"错误详情见 {ERROR_LOG}")
|
||||
else:
|
||||
print("无错误。")
|
||||
print("全部成功,无失败。")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user