Skip to content

Commit 5cd2871

Browse files
authored
feat: add support for custom webhook value in env (#1341)
Introduced SWANLAB_WEBHOOK_VALUE environment variable to allow sending custom content with webhook notifications. Updated webhook logic to include this value if present and adjusted metadata typing for exp_url to allow None.
1 parent 88c6460 commit 5cd2871

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

swanlab/data/run/metadata/cooperation/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@description: 合作信息采集
66
"""
77

8-
from typing import TypedDict, Optional
8+
from typing import TypedDict, Optional, Union
99

1010
from swanlab.core_python import get_client
1111
from swanlab.env import get_mode, get_swanlog_dir
@@ -17,7 +17,7 @@ class SwanLabInfo(TypedDict):
1717
version: str
1818
mode: str
1919
swanlog_dir: str
20-
exp_url: str
20+
exp_url: Union[str, None]
2121

2222

2323
class CoopInfo(TypedDict):
@@ -41,6 +41,7 @@ def get_swanlab_info() -> SwanLabInfo:
4141
"version": get_package_version(),
4242
"mode": get_mode(),
4343
"swanlog_dir": get_swanlog_dir(),
44+
"exp_url": None,
4445
}
4546
try:
4647
client = get_client()

swanlab/data/run/webhook.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def try_send_webhook():
2121
webhook = os.getenv(SwanLabEnv.WEBHOOK.value)
2222
if not webhook:
2323
return
24-
data = get_cooperation_info(swanlab=True)
24+
value = os.getenv(SwanLabEnv.WEBHOOK_VALUE.value)
25+
data = get_cooperation_info(swanlab=True) or {}
26+
if value:
27+
data["value"] = value
2528
try:
2629
requests.post(webhook, json=data)
2730
except Exception as e: # noqa

swanlab/env.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ class SwanLabEnv(enum.Enum):
109109
"""
110110
webhook地址。swanlab初始化完毕时,如果此环境变量存在,会调用此地址,发送消息。
111111
"""
112+
WEBHOOK_VALUE = "SWANLAB_WEBHOOK_VALUE"
113+
"""
114+
webhook发送的自定义内容,以字符串读取此环境变量值后发送
115+
"""
112116
DESCRIPTION = "SWANLAB_DESCRIPTION"
113117
"""
114118
实验描述,用于为实验提供更详细的介绍或标注

0 commit comments

Comments
 (0)