feat: Enhance channel status update with success tracking and dynamic notification #812
This commit is contained in:
@@ -290,35 +290,42 @@ func (channel *Channel) Delete() error {
|
|||||||
|
|
||||||
var channelStatusLock sync.Mutex
|
var channelStatusLock sync.Mutex
|
||||||
|
|
||||||
func UpdateChannelStatusById(id int, status int, reason string) {
|
func UpdateChannelStatusById(id int, status int, reason string) bool {
|
||||||
if common.MemoryCacheEnabled {
|
if common.MemoryCacheEnabled {
|
||||||
channelStatusLock.Lock()
|
channelStatusLock.Lock()
|
||||||
|
defer channelStatusLock.Unlock()
|
||||||
|
|
||||||
channelCache, _ := CacheGetChannel(id)
|
channelCache, _ := CacheGetChannel(id)
|
||||||
// 如果缓存渠道存在,且状态已是目标状态,直接返回
|
// 如果缓存渠道存在,且状态已是目标状态,直接返回
|
||||||
if channelCache != nil && channelCache.Status == status {
|
if channelCache != nil && channelCache.Status == status {
|
||||||
channelStatusLock.Unlock()
|
return false
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// 如果缓存渠道不存在(说明已经被禁用),且要设置的状态不为启用,直接返回
|
// 如果缓存渠道不存在(说明已经被禁用),且要设置的状态不为启用,直接返回
|
||||||
if channelCache == nil && status != common.ChannelStatusEnabled {
|
if channelCache == nil && status != common.ChannelStatusEnabled {
|
||||||
channelStatusLock.Unlock()
|
return false
|
||||||
return
|
|
||||||
}
|
}
|
||||||
CacheUpdateChannelStatus(id, status)
|
CacheUpdateChannelStatus(id, status)
|
||||||
channelStatusLock.Unlock()
|
|
||||||
}
|
}
|
||||||
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("failed to update ability status: " + err.Error())
|
common.SysError("failed to update ability status: " + err.Error())
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
channel, err := GetChannelById(id, true)
|
channel, err := GetChannelById(id, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// find channel by id error, directly update status
|
// find channel by id error, directly update status
|
||||||
err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
result := DB.Model(&Channel{}).Where("id = ?", id).Update("status", status)
|
||||||
if err != nil {
|
if result.Error != nil {
|
||||||
common.SysError("failed to update channel status: " + err.Error())
|
common.SysError("failed to update channel status: " + result.Error.Error())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if result.RowsAffected == 0 {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if channel.Status == status {
|
||||||
|
return false
|
||||||
|
}
|
||||||
// find channel by id success, update status and other info
|
// find channel by id success, update status and other info
|
||||||
info := channel.GetOtherInfo()
|
info := channel.GetOtherInfo()
|
||||||
info["status_reason"] = reason
|
info["status_reason"] = reason
|
||||||
@@ -328,9 +335,10 @@ func UpdateChannelStatusById(id int, status int, reason string) {
|
|||||||
err = channel.Save()
|
err = channel.Save()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("failed to update channel status: " + err.Error())
|
common.SysError("failed to update channel status: " + err.Error())
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableChannelByTag(tag string) error {
|
func EnableChannelByTag(tag string) error {
|
||||||
|
|||||||
@@ -10,19 +10,27 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func formatNotifyType(channelId int, status int) string {
|
||||||
|
return fmt.Sprintf("%s_%d_%d", dto.NotifyTypeChannelUpdate, channelId, status)
|
||||||
|
}
|
||||||
|
|
||||||
// disable & notify
|
// disable & notify
|
||||||
func DisableChannel(channelId int, channelName string, reason string) {
|
func DisableChannel(channelId int, channelName string, reason string) {
|
||||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
|
success := model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled, reason)
|
||||||
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
if success {
|
||||||
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
||||||
NotifyRootUser(dto.NotifyTypeChannelUpdate, subject, content)
|
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
||||||
|
NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusAutoDisabled), subject, content)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableChannel(channelId int, channelName string) {
|
func EnableChannel(channelId int, channelName string) {
|
||||||
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
|
success := model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled, "")
|
||||||
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
if success {
|
||||||
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||||||
NotifyRootUser(dto.NotifyTypeChannelUpdate, subject, content)
|
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||||||
|
NotifyRootUser(formatNotifyType(channelId, common.ChannelStatusEnabled), subject, content)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShouldDisableChannel(channelType int, err *dto.OpenAIErrorWithStatusCode) bool {
|
func ShouldDisableChannel(channelType int, err *dto.OpenAIErrorWithStatusCode) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user