-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.old.py
More file actions
93 lines (70 loc) · 2.83 KB
/
Copy pathmain.old.py
File metadata and controls
93 lines (70 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import os
import random
import time
from functools import wraps
from cozepy import Coze, TokenAuth, COZE_CN_BASE_URL
from dotenv import load_dotenv
from utils import save_file, generate_random_filename
# 重试装饰器
def retry_with_exponential_backoff(max_attempts=5, initial_delay=1, max_delay=180, exponential_base=2, jitter=True):
"""
带有指数退避和抖动的重试装饰器
参数:
max_attempts: 最大重试次数
initial_delay: 初始延迟时间(秒)
max_delay: 最大延迟时间(秒)
exponential_base: 指数基数
jitter: 是否添加随机抖动避免惊群效应
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
attempts = 0
while attempts < max_attempts:
try:
return func(*args, **kwargs)
except Exception as err:
attempts += 1
if attempts == max_attempts:
print(f"操作失败,已达到最大重试次数 {max_attempts},最后一次异常: {str(err)}")
raise
# 计算指数退避延迟
delay = initial_delay * (exponential_base ** (attempts - 1))
# 限制最大延迟
delay = min(delay, max_delay)
# 添加随机抖动(可选)
if jitter:
delay = delay * (0.5 + random.random()) # 在50%-150%之间随机
print(f"操作失败: {str(err)},{delay:.2f}秒后进行第{attempts + 1}次重试...")
time.sleep(delay)
return None
return wrapper
return decorator
# initialize client
load_dotenv()
coze_api_token = os.getenv("COZE_API_TOKEN")
coze_api_base = COZE_CN_BASE_URL
coze = Coze(auth=TokenAuth(coze_api_token), base_url=coze_api_base)
@retry_with_exponential_backoff(max_attempts=5, initial_delay=15)
def create_workflow_run(_workflow_id):
"""创建工作流运行,带有重试机制"""
return coze.workflows.runs.create(workflow_id=_workflow_id)
try:
# 获取工作流ID并创建运行实例,最多重试3次
workflow_id = os.getenv("WORKFLOW_ID")
if not workflow_id:
raise ValueError("环境变量中未找到WORKFLOW_ID")
ct = create_workflow_run(workflow_id)
# 打印调试URL
debug_url = ct.debug_url
print(f"工作流运行调试URL: {debug_url}")
# 处理返回结果
ct_data = json.loads(ct.data)
output = ct_data["output"]
# 保存结果到文件
file_name = generate_random_filename(extension="md")
save_file(f"content/posts/TrialRun/{file_name}", output)
print(f"成功保存结果到文件: {file_name}")
except Exception as e:
print(f"执行失败: {str(e)}")