68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/channels/dingtalk"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/coverage"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/faqtable"
|
|
)
|
|
|
|
type supportDispatcherRunner interface {
|
|
RunOnce(context.Context) (coverage.DispatcherStats, error)
|
|
}
|
|
|
|
func buildSupportDispatcher(repo coverage.NotificationRepository, clientID, clientSecret, openConversationID string, recordLinks *faqtable.RecordLinkBuilder) (*coverage.Dispatcher, error) {
|
|
openConversationID = strings.TrimSpace(openConversationID)
|
|
if openConversationID == "" {
|
|
return nil, nil
|
|
}
|
|
if repo == nil {
|
|
return nil, errors.New("support notification repository is required")
|
|
}
|
|
if recordLinks == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
sender, err := dingtalk.NewSupportGroupSender(clientID, clientSecret, openConversationID, nil, dingtalk.WithFAQRecordLinkBuilder(recordLinks))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return coverage.NewDispatcher(repo, sender)
|
|
}
|
|
|
|
func runSupportDispatcher(ctx context.Context, dispatcher supportDispatcherRunner, interval time.Duration) {
|
|
if dispatcher == nil || interval <= 0 {
|
|
return
|
|
}
|
|
|
|
runOnce := func() {
|
|
runCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
stats, err := dispatcher.RunOnce(runCtx)
|
|
cancel()
|
|
if err != nil {
|
|
log.Printf("support notification dispatch failed: %v", err)
|
|
return
|
|
}
|
|
if stats.Claimed > 0 {
|
|
log.Printf("support notification dispatch: claimed=%d notified=%d failed=%d", stats.Claimed, stats.Notified, stats.Failed)
|
|
}
|
|
}
|
|
|
|
runOnce()
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
runOnce()
|
|
}
|
|
}
|
|
}
|