feat: support automatic channel testing & balance updates (close #11, close #59)

This commit is contained in:
JustSong
2023-06-22 22:01:03 +08:00
parent ad1049b0cf
commit 4463224f04
5 changed files with 57 additions and 15 deletions

View File

@@ -257,6 +257,7 @@ func updateAllChannelsBalance() error {
disableChannel(channel.Id, channel.Name, "余额不足")
}
}
time.Sleep(common.RequestInterval)
}
return nil
}
@@ -277,3 +278,12 @@ func UpdateAllChannelsBalance(c *gin.Context) {
})
return
}
func AutomaticallyUpdateChannels(frequency int) {
for {
time.Sleep(time.Duration(frequency) * time.Minute)
common.SysLog("updating all channels")
_ = updateAllChannelsBalance()
common.SysLog("channels update done")
}
}

View File

@@ -62,10 +62,9 @@ func testChannel(channel *model.Channel, request ChatRequest) error {
return nil
}
func buildTestRequest(c *gin.Context) *ChatRequest {
model_ := c.Query("model")
func buildTestRequest() *ChatRequest {
testRequest := &ChatRequest{
Model: model_,
Model: "", // this will be set later
MaxTokens: 1,
}
testMessage := Message{
@@ -93,7 +92,7 @@ func TestChannel(c *gin.Context) {
})
return
}
testRequest := buildTestRequest(c)
testRequest := buildTestRequest()
tik := time.Now()
err = testChannel(channel, *testRequest)
tok := time.Now()
@@ -133,7 +132,7 @@ func disableChannel(channelId int, channelName string, reason string) {
}
}
func testAllChannels(c *gin.Context) error {
func testAllChannels(notify bool) error {
if common.RootUserEmail == "" {
common.RootUserEmail = model.GetRootUserEmail()
}
@@ -146,13 +145,9 @@ func testAllChannels(c *gin.Context) error {
testAllChannelsLock.Unlock()
channels, err := model.GetAllChannels(0, 0, true)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return err
}
testRequest := buildTestRequest(c)
testRequest := buildTestRequest()
var disableThreshold = int64(common.ChannelDisableThreshold * 1000)
if disableThreshold == 0 {
disableThreshold = 10000000 // a impossible value
@@ -173,20 +168,23 @@ func testAllChannels(c *gin.Context) error {
disableChannel(channel.Id, channel.Name, err.Error())
}
channel.UpdateResponseTime(milliseconds)
}
err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常")
if err != nil {
common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
time.Sleep(common.RequestInterval)
}
testAllChannelsLock.Lock()
testAllChannelsRunning = false
testAllChannelsLock.Unlock()
if notify {
err := common.SendEmail("通道测试完成", common.RootUserEmail, "通道测试完成,如果没有收到禁用通知,说明所有通道都正常")
if err != nil {
common.SysError(fmt.Sprintf("failed to send email: %s", err.Error()))
}
}
}()
return nil
}
func TestAllChannels(c *gin.Context) {
err := testAllChannels(c)
err := testAllChannels(true)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
@@ -200,3 +198,12 @@ func TestAllChannels(c *gin.Context) {
})
return
}
func AutomaticallyTestChannels(frequency int) {
for {
time.Sleep(time.Duration(frequency) * time.Minute)
common.SysLog("testing all channels")
_ = testAllChannels(false)
common.SysLog("channel test finished")
}
}