2021-08-29 09:21:44 +08:00
|
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
2023-06-30 18:08:30 +08:00
|
|
|
|
import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2022-06-05 17:12:54 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/components/cache/cacheutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2021-08-29 09:21:44 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
|
"github.com/iwind/TeaGo/lists"
|
2022-06-05 17:12:54 +08:00
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
|
"github.com/iwind/TeaGo/types"
|
2021-08-29 09:21:44 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type PurgeAction struct {
|
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *PurgeAction) Init() {
|
|
|
|
|
|
this.Nav("", "setting", "purge")
|
|
|
|
|
|
this.SecondMenu("cache")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *PurgeAction) RunGet(params struct {
|
|
|
|
|
|
ServerId int64
|
|
|
|
|
|
}) {
|
|
|
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.Data["webId"] = webConfig.Id
|
|
|
|
|
|
this.Data["webConfig"] = webConfig
|
|
|
|
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *PurgeAction) RunPost(params struct {
|
|
|
|
|
|
ServerId int64
|
|
|
|
|
|
WebId int64
|
2022-06-05 17:12:54 +08:00
|
|
|
|
KeyType string
|
2021-08-29 09:21:44 +08:00
|
|
|
|
Keys string
|
|
|
|
|
|
|
|
|
|
|
|
Must *actions.Must
|
|
|
|
|
|
}) {
|
|
|
|
|
|
|
|
|
|
|
|
// 创建日志
|
2023-06-30 18:08:30 +08:00
|
|
|
|
defer this.CreateLogInfo(codes.ServerCache_LogPurgeCaches, params.ServerId)
|
2021-08-29 09:21:44 +08:00
|
|
|
|
|
|
|
|
|
|
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithId(this.AdminContext(), params.WebId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if webConfig == nil {
|
|
|
|
|
|
this.NotFound("httpWeb", params.WebId)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var cache = webConfig.Cache
|
|
|
|
|
|
if cache == nil || !cache.IsOn {
|
|
|
|
|
|
this.Fail("当前没有开启缓存")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
serverResp, err := this.RPC().ServerRPC().FindEnabledUserServerBasic(this.AdminContext(), &pb.FindEnabledUserServerBasicRequest{ServerId: params.ServerId})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var server = serverResp.Server
|
|
|
|
|
|
if server == nil || server.NodeCluster == nil {
|
|
|
|
|
|
this.NotFound("server", params.ServerId)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var clusterId = server.NodeCluster.Id
|
|
|
|
|
|
|
|
|
|
|
|
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{NodeClusterId: clusterId})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var cluster = clusterResp.NodeCluster
|
|
|
|
|
|
if cluster == nil {
|
|
|
|
|
|
this.NotFound("nodeCluster", clusterId)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
var cachePolicyId = cluster.HttpCachePolicyId
|
|
|
|
|
|
if cachePolicyId == 0 {
|
|
|
|
|
|
this.Fail("当前集群没有设置缓存策略")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{HttpCachePolicyId: cachePolicyId})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
cachePolicyJSON := cachePolicyResp.HttpCachePolicyJSON
|
|
|
|
|
|
if len(cachePolicyJSON) == 0 {
|
|
|
|
|
|
this.Fail("找不到要操作的缓存策略")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(params.Keys) == 0 {
|
|
|
|
|
|
this.Fail("请输入要删除的Key列表")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
realKeys := []string{}
|
|
|
|
|
|
for _, key := range strings.Split(params.Keys, "\n") {
|
|
|
|
|
|
key = strings.TrimSpace(key)
|
|
|
|
|
|
if len(key) == 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if lists.ContainsString(realKeys, key) {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
realKeys = append(realKeys, key)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-05 17:12:54 +08:00
|
|
|
|
// 校验Key
|
|
|
|
|
|
validateResp, err := this.RPC().HTTPCacheTaskKeyRPC().ValidateHTTPCacheTaskKeys(this.AdminContext(), &pb.ValidateHTTPCacheTaskKeysRequest{Keys: realKeys})
|
2021-08-29 09:21:44 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-05 17:12:54 +08:00
|
|
|
|
var failKeyMaps = []maps.Map{}
|
|
|
|
|
|
if len(validateResp.FailKeys) > 0 {
|
|
|
|
|
|
for _, key := range validateResp.FailKeys {
|
|
|
|
|
|
failKeyMaps = append(failKeyMaps, maps.Map{
|
|
|
|
|
|
"key": key.Key,
|
|
|
|
|
|
"reason": cacheutils.KeyFailReason(key.ReasonCode),
|
|
|
|
|
|
})
|
2021-08-29 09:21:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-06-05 17:12:54 +08:00
|
|
|
|
this.Data["failKeys"] = failKeyMaps
|
|
|
|
|
|
if len(failKeyMaps) > 0 {
|
|
|
|
|
|
this.Fail("有" + types.String(len(failKeyMaps)) + "个Key无法完成操作,请删除后重试")
|
|
|
|
|
|
}
|
2021-08-29 09:21:44 +08:00
|
|
|
|
|
2022-06-05 17:12:54 +08:00
|
|
|
|
// 提交任务
|
|
|
|
|
|
_, err = this.RPC().HTTPCacheTaskRPC().CreateHTTPCacheTask(this.AdminContext(), &pb.CreateHTTPCacheTaskRequest{
|
|
|
|
|
|
Type: "purge",
|
|
|
|
|
|
KeyType: params.KeyType,
|
|
|
|
|
|
Keys: realKeys,
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-08-29 09:21:44 +08:00
|
|
|
|
|
|
|
|
|
|
this.Success()
|
|
|
|
|
|
}
|