Files
EdgeAdmin/internal/web/actions/default/servers/components/cache/purge.go

126 lines
3.3 KiB
Go
Raw Normal View History

2020-10-02 17:22:24 +08:00
package cache
2023-06-30 18:08:30 +08:00
import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"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"
2020-10-04 16:09:50 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"net/http"
"strconv"
"strings"
2020-10-04 16:09:50 +08:00
)
2020-10-02 17:22:24 +08:00
type PurgeAction struct {
actionutils.ParentAction
}
func (this *PurgeAction) Init() {
2020-10-04 14:27:05 +08:00
this.Nav("", "", "purge")
2020-10-02 17:22:24 +08:00
}
2020-12-17 17:35:38 +08:00
func (this *PurgeAction) RunGet(params struct {
CachePolicyId int64
}) {
2020-10-04 16:09:50 +08:00
// 默认的集群ID
cookie, err := this.Request.Cookie("cache_cluster_id")
if cookie != nil && err == nil {
this.Data["clusterId"] = types.Int64(cookie.Value)
}
// 集群列表
2020-12-17 17:35:38 +08:00
clustersResp, err := this.RPC().NodeClusterRPC().FindAllEnabledNodeClustersWithHTTPCachePolicyId(this.AdminContext(), &pb.FindAllEnabledNodeClustersWithHTTPCachePolicyIdRequest{HttpCachePolicyId: params.CachePolicyId})
2020-10-04 16:09:50 +08:00
if err != nil {
this.ErrorPage(err)
return
}
clusterMaps := []maps.Map{}
2020-12-17 17:35:38 +08:00
for _, cluster := range clustersResp.NodeClusters {
2020-10-04 16:09:50 +08:00
clusterMaps = append(clusterMaps, maps.Map{
"id": cluster.Id,
"name": cluster.Name,
})
}
this.Data["clusters"] = clusterMaps
2020-10-02 17:22:24 +08:00
this.Show()
}
2020-10-04 16:09:50 +08:00
func (this *PurgeAction) RunPost(params struct {
CachePolicyId int64
ClusterId int64
KeyType string
2020-10-04 16:09:50 +08:00
Keys string
Must *actions.Must
}) {
2020-12-23 21:33:24 +08:00
// 创建日志
2023-06-30 18:08:30 +08:00
defer this.CreateLogInfo(codes.ServerCachePolicy_LogPurgeCaches, params.CachePolicyId)
2020-12-23 21:33:24 +08:00
2020-10-04 16:09:50 +08:00
// 记录clusterId
this.AddCookie(&http.Cookie{
Name: "cache_cluster_id",
Value: strconv.FormatInt(params.ClusterId, 10),
})
cachePolicyResp, err := this.RPC().HTTPCachePolicyRPC().FindEnabledHTTPCachePolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPCachePolicyConfigRequest{HttpCachePolicyId: params.CachePolicyId})
2020-10-04 16:09:50 +08:00
if err != nil {
this.ErrorPage(err)
return
}
cachePolicyJSON := cachePolicyResp.HttpCachePolicyJSON
2020-10-04 16:09:50 +08:00
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)
}
// 校验Key
validateResp, err := this.RPC().HTTPCacheTaskKeyRPC().ValidateHTTPCacheTaskKeys(this.AdminContext(), &pb.ValidateHTTPCacheTaskKeysRequest{Keys: realKeys})
2020-10-04 16:09:50 +08:00
if err != nil {
this.ErrorPage(err)
return
}
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),
})
2020-10-04 16:09:50 +08:00
}
}
this.Data["failKeys"] = failKeyMaps
if len(failKeyMaps) > 0 {
this.Fail("有" + types.String(len(failKeyMaps)) + "个Key无法完成操作请删除后重试")
}
2020-10-04 16:09:50 +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
}
2020-10-04 16:09:50 +08:00
this.Success()
}