服务 -- 缓存设置中增加清理和预热功能

This commit is contained in:
GoEdgeLab
2021-08-29 09:21:44 +08:00
parent 608759b927
commit ba0153fa43
11 changed files with 464 additions and 17 deletions

View File

@@ -15,6 +15,8 @@ func init() {
Prefix("/servers/server/settings/cache"). Prefix("/servers/server/settings/cache").
GetPost("", new(IndexAction)). GetPost("", new(IndexAction)).
GetPost("/createPopup", new(CreatePopupAction)). GetPost("/createPopup", new(CreatePopupAction)).
GetPost("/purge", new(PurgeAction)).
GetPost("/preheat", new(PreheatAction)).
EndAll() EndAll()
}) })
} }

View File

@@ -0,0 +1,143 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package cache
import (
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils"
"github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"strings"
)
type PreheatAction struct {
actionutils.ParentAction
}
func (this *PreheatAction) Init() {
this.Nav("", "setting", "preheat")
this.SecondMenu("cache")
}
func (this *PreheatAction) 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 *PreheatAction) RunPost(params struct {
ServerId int64
WebId int64
Keys string
Must *actions.Must
}) {
// 创建日志
defer this.CreateLog(oplogs.LevelInfo, "预热服务 %d 缓存", params.ServerId)
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)
}
// 发送命令
msg := &messageconfigs.PreheatCacheMessage{
CachePolicyJSON: cachePolicyJSON,
Keys: realKeys,
}
results, err := nodeutils.SendMessageToCluster(this.AdminContext(), clusterId, messageconfigs.MessageCodePreheatCache, msg, 300)
if err != nil {
this.ErrorPage(err)
return
}
isAllOk := true
for _, result := range results {
if !result.IsOK {
isAllOk = false
break
}
}
this.Data["isAllOk"] = isAllOk
this.Data["results"] = results
this.Success()
}

View File

@@ -0,0 +1,149 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package cache
import (
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils"
"github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"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
Type string
Keys string
Must *actions.Must
}) {
// 创建日志
defer this.CreateLog(oplogs.LevelInfo, "删除服务 %d 缓存", params.ServerId)
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)
}
// 发送命令
msg := &messageconfigs.PurgeCacheMessage{
CachePolicyJSON: cachePolicyJSON,
Keys: realKeys,
}
if params.Type == "prefix" {
msg.Type = messageconfigs.PurgeCacheMessageTypeDir
} else {
msg.Type = messageconfigs.PurgeCacheMessageTypeFile
}
results, err := nodeutils.SendMessageToCluster(this.AdminContext(), clusterId, messageconfigs.MessageCodePurgeCache, msg, 10)
if err != nil {
this.ErrorPage(err)
return
}
isAllOk := true
for _, result := range results {
if !result.IsOK {
isAllOk = false
break
}
}
this.Data["isAllOk"] = isAllOk
this.Data["results"] = results
this.Success()
}

View File

@@ -180,21 +180,16 @@ func (this *userMustAuth) modules(adminId int64) []maps.Map {
"subtitle": "服务列表", "subtitle": "服务列表",
"icon": "clone outsize", "icon": "clone outsize",
"subItems": []maps.Map{ "subItems": []maps.Map{
{
"name": "证书管理",
"url": "/servers/certs",
"code": "cert",
},
{ {
"name": "访问日志", "name": "访问日志",
"url": "/servers/logs", "url": "/servers/logs",
"code": "log", "code": "log",
}, },
{
"name": "通用设置",
"url": "/servers/components",
"code": "global",
},
{
"name": "服务分组",
"url": "/servers/components/groups",
"code": "group",
},
{ {
"name": "缓存策略", "name": "缓存策略",
"url": "/servers/components/cache", "url": "/servers/components/cache",
@@ -216,16 +211,21 @@ func (this *userMustAuth) modules(adminId int64) []maps.Map {
"url": "/servers/iplists", "url": "/servers/iplists",
"code": "iplist", "code": "iplist",
}, },
{
"name": "证书管理",
"url": "/servers/certs",
"code": "cert",
},
{ {
"name": "统计指标", "name": "统计指标",
"url": "/servers/metrics", "url": "/servers/metrics",
"code": "metric", "code": "metric",
}, },
{
"name": "服务分组",
"url": "/servers/components/groups",
"code": "group",
},
{
"name": "通用设置",
"url": "/servers/components",
"code": "global",
},
}, },
}, },
{ {

View File

@@ -8,7 +8,7 @@
<div class="ui divider"></div> <div class="ui divider"></div>
<h3>批量删除</h3> <h3>批量删除</h3>
<p class="comment">可以批量删除一组Key。</p> <p class="comment">可以在这里批量删除一组Key。</p>
<form method="post" class="ui form" data-tea-action="$" data-tea-before="before" data-tea-success="success" data-tea-fail="fail" data-tea-done="done" data-tea-timeout="300"> <form method="post" class="ui form" data-tea-action="$" data-tea-before="before" data-tea-success="success" data-tea-fail="fail" data-tea-done="done" data-tea-timeout="300">
<input type="hidden" name="cachePolicyId" :value="cachePolicyId"/> <input type="hidden" name="cachePolicyId" :value="cachePolicyId"/>
<input type="hidden" name="clusterId" :value="clusterId"/> <input type="hidden" name="clusterId" :value="clusterId"/>

View File

@@ -0,0 +1,5 @@
<first-menu>
<menu-item :href="'.?serverId=' + serverId" code="index">设置</menu-item>
<menu-item :href="'.purge?serverId=' + serverId" code="purge">清理</menu-item>
<menu-item :href="'.preheat?serverId=' + serverId" code="preheat">预热</menu-item>
</first-menu>

View File

@@ -1,8 +1,9 @@
{$layout} {$layout}
{$template "/left_menu"} {$template "/left_menu"}
<div class="right-box"> <div class="right-box">
{$template "menu"}
<form method="post" class="ui form" data-tea-action="$" data-tea-success="success"> <form method="post" class="ui form" data-tea-action="$" data-tea-success="success">
<input type="hidden" name="webId" :value="webId"/> <input type="hidden" name="webId" :value="webId"/>
<http-cache-config-box :v-cache-config="cacheConfig" :v-cache-policy="cachePolicy"></http-cache-config-box> <http-cache-config-box :v-cache-config="cacheConfig" :v-cache-policy="cachePolicy"></http-cache-config-box>

View File

@@ -0,0 +1,39 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
{$template "menu"}
<div class="margin"></div>
<div v-show="webConfig.cache == null || !webConfig.cache.isOn">
<p class="comment">没有开启缓存,不需要清理。</p>
</div>
<div v-show="webConfig.cache != null && webConfig.cache.isOn">
<p class="comment">可以在这里批量预热一组Key。</p>
<form method="post" class="ui form" data-tea-action="$" data-tea-before="before" data-tea-success="success" data-tea-fail="fail" data-tea-done="done" data-tea-timeout="3600">
<input type="hidden" name="serverId" :value="serverId"/>
<input type="hidden" name="webId" :value="webId"/>
<table class="ui table definition selectable">
<tr>
<td>Key列表</td>
<td>
<textarea name="keys" rows="10" ref="focus"></textarea>
<p class="comment">每行一个Key。</p>
</td>
</tr>
<tr>
<td class="title">操作结果</td>
<td>
<div v-if="isRequesting">数据发送中...</div>
<span class="red" v-if="!isRequesting && !isOk && message.length > 0">失败:{{message}}</span>
<div v-if="!isRequesting && isOk">
<span v-if="results.length == 0" class="red">此集群下没有任何可用的节点。</span>
<div class="ui label tiny" v-for="one in results" :class="{green:one.isOk, red:!one.isOk}" style="margin-bottom: 0.5em">{{one.nodeName}}{{one.message}}</div>
</div>
</td>
</tr>
</table>
<submit-btn v-if="!isRequesting">提交</submit-btn>
</form>
</div>
</div>

View File

@@ -0,0 +1,26 @@
Tea.context(function () {
this.isRequesting = false
this.isOk = false
this.message = ""
this.results = []
this.before = function () {
this.isRequesting = true
this.isOk = false
this.message = ""
this.results = []
}
this.success = function (resp) {
this.isOk = true
this.results = resp.data.results
}
this.fail = function (resp) {
this.message = resp.message
}
this.done = function () {
this.isRequesting = false
}
});

View File

@@ -0,0 +1,51 @@
{$layout}
{$template "/left_menu"}
<div class="right-box">
{$template "menu"}
<div class="margin"></div>
<div v-show="webConfig.cache == null || !webConfig.cache.isOn">
<p class="comment">没有开启缓存,不需要清理。</p>
</div>
<div v-show="webConfig.cache != null && webConfig.cache.isOn">
<p class="comment">可以在这里批量删除一组Key。</p>
<form method="post" class="ui form" data-tea-action="$" data-tea-before="before" data-tea-success="success" data-tea-fail="fail" data-tea-done="done" data-tea-timeout="300">
<input type="hidden" name="serverId" :value="serverId"/>
<input type="hidden" name="webId" :value="webId"/>
<table class="ui table definition selectable">
<tr>
<td class="title">操作类型</td>
<td>
<radio name="type" :v-value="'key'" v-model="type">根据Key</radio> &nbsp;
<radio name="type" :v-value="'prefix'" v-model="type">根据前缀</radio>
</td>
</tr>
<tr>
<td>
<span v-if="type == 'key'">Key列表</span>
<span v-if="type == 'prefix'">Key前缀列表</span>
</td>
<td>
<textarea name="keys" rows="10" ref="focus"></textarea>
<p class="comment" v-if="type == 'key'">每行一个Key比如是一个完整的URL<code-label>https://example.com/hello/world.html</code-label></p>
<p class="comment" v-if="type == 'prefix'">每行一个Key前缀比如是一个URL前缀<code-label>https://example.com/hello/</code-label></p>
</td>
</tr>
<tr>
<td>操作结果</td>
<td>
<div v-if="isRequesting">数据发送中...</div>
<span class="red" v-if="!isRequesting && !isOk && message.length > 0">失败:{{message}}</span>
<div v-if="!isRequesting && isOk">
<span v-if="results.length == 0" class="red">此集群下没有任何可用的节点。</span>
<div class="ui label tiny" v-for="one in results" :class="{green:one.isOk, red:!one.isOk}" style="margin-bottom: 0.5em">{{one.nodeName}}{{one.message}}</div>
</div>
</td>
</tr>
</table>
<submit-btn v-if="!isRequesting">提交</submit-btn>
</form>
</div>
</div>

View File

@@ -0,0 +1,31 @@
Tea.context(function () {
this.isRequesting = false
this.isOk = false
this.message = ""
this.results = []
this.before = function () {
this.isRequesting = true
this.isOk = false
this.message = ""
this.results = []
}
this.success = function (resp) {
this.isOk = true
this.results = resp.data.results
}
this.fail = function (resp) {
this.message = resp.message
}
this.done = function () {
this.isRequesting = false
}
/**
* 操作类型
*/
this.type = "key" // key | prefix
})