修改infra除repository包
This commit is contained in:
@@ -2,11 +2,14 @@ package notify
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -18,6 +21,8 @@ const (
|
||||
|
||||
// larkNotifier 实现了 Notifier 接口,用于通过飞书自建应用发送私聊消息。
|
||||
type larkNotifier struct {
|
||||
ctx context.Context
|
||||
|
||||
appID string // 应用 ID
|
||||
appSecret string // 应用密钥
|
||||
|
||||
@@ -29,8 +34,9 @@ type larkNotifier struct {
|
||||
|
||||
// NewLarkNotifier 创建一个新的 larkNotifier 实例。
|
||||
// 调用者需要注入飞书应用的 AppID 和 AppSecret。
|
||||
func NewLarkNotifier(appID, appSecret string) Notifier {
|
||||
func NewLarkNotifier(ctx context.Context, appID, appSecret string) Notifier {
|
||||
return &larkNotifier{
|
||||
ctx: ctx,
|
||||
appID: appID,
|
||||
appSecret: appSecret,
|
||||
}
|
||||
@@ -38,9 +44,10 @@ func NewLarkNotifier(appID, appSecret string) Notifier {
|
||||
|
||||
// Send 向指定用户发送一条飞书消息卡片。
|
||||
// toAddr 参数是接收者的邮箱地址。
|
||||
func (l *larkNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
func (l *larkNotifier) Send(ctx context.Context, content AlarmContent, toAddr string) error {
|
||||
notifierCtx := logs.AddFuncName(ctx, l.ctx, "Send")
|
||||
// 1. 获取有效的 tenant_access_token
|
||||
token, err := l.getAccessToken()
|
||||
token, err := l.getAccessToken(notifierCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -115,7 +122,7 @@ func (l *larkNotifier) Send(content AlarmContent, toAddr string) error {
|
||||
}
|
||||
|
||||
// getAccessToken 获取并缓存 tenant_access_token,处理了线程安全和自动刷新。
|
||||
func (l *larkNotifier) getAccessToken() (string, error) {
|
||||
func (l *larkNotifier) getAccessToken(ctx context.Context) (string, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user