mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-10-26 21:22:16 +08:00
feat: config
This commit is contained in:
parent
5cc018df7d
commit
2e4905e837
@ -21,7 +21,7 @@ prometheus:
|
|||||||
maxConcurrentWorkers: 3
|
maxConcurrentWorkers: 3
|
||||||
#Use geTui for offline push notifications, or choose fcm or jpns; corresponding configuration settings must be specified.
|
#Use geTui for offline push notifications, or choose fcm or jpns; corresponding configuration settings must be specified.
|
||||||
enable:
|
enable:
|
||||||
geTui:
|
getui:
|
||||||
pushUrl: https://restapi.getui.com/v2/$appId
|
pushUrl: https://restapi.getui.com/v2/$appId
|
||||||
masterSecret:
|
masterSecret:
|
||||||
appKey:
|
appKey:
|
||||||
|
|||||||
@ -3,6 +3,8 @@ package api
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/apistruct"
|
"github.com/openimsdk/open-im-server/v3/pkg/apistruct"
|
||||||
@ -159,12 +161,12 @@ func compareAndSave[T any](c *gin.Context, old any, req *apistruct.SetConfigReq,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConfigManager) ResetConfig(c *gin.Context) {
|
func (cm *ConfigManager) ResetConfig(c *gin.Context) {
|
||||||
go cm.restart(c)
|
go cm.resetConfig(c)
|
||||||
apiresp.GinSuccess(c, nil)
|
apiresp.GinSuccess(c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *ConfigManager) restart(c *gin.Context) {
|
func (cm *ConfigManager) resetConfig(c *gin.Context) {
|
||||||
<-etcd.CanRestart
|
txn := cm.client.Txn(c)
|
||||||
type initConf struct {
|
type initConf struct {
|
||||||
old any
|
old any
|
||||||
new any
|
new any
|
||||||
@ -220,11 +222,25 @@ func (cm *ConfigManager) restart(c *gin.Context) {
|
|||||||
log.ZError(c, "marshal config failed", err)
|
log.ZError(c, "marshal config failed", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_, err = cm.client.Put(c, etcd.BuildKey(k), string(data))
|
txn = txn.Then(clientv3.OpPut(etcd.BuildKey(k), string(data)))
|
||||||
if err != nil {
|
}
|
||||||
log.ZError(c, "save to etcd failed", err)
|
_, err := txn.Commit()
|
||||||
continue
|
if err != nil {
|
||||||
}
|
log.ZError(c, "commit etcd txn failed", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cm *ConfigManager) Restart(c *gin.Context) {
|
||||||
|
go cm.restart(c)
|
||||||
|
apiresp.GinSuccess(c, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cm *ConfigManager) restart(c *gin.Context) {
|
||||||
|
time.Sleep(time.Millisecond * 200) // wait for Restart http call return
|
||||||
|
t := time.Now().Unix()
|
||||||
|
_, err := cm.client.Put(c, etcd.BuildKey(etcd.RestartKey), strconv.Itoa(int(t)))
|
||||||
|
if err != nil {
|
||||||
|
log.ZError(c, "restart etcd put key failed", err)
|
||||||
}
|
}
|
||||||
etcd.CanRestart <- struct{}{}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -300,18 +300,22 @@ func newGinRouter(ctx context.Context, client discovery.SvcDiscoveryRegistry, cf
|
|||||||
proDiscoveryGroup.GET("/msg_transfer", pd.MessageTransfer)
|
proDiscoveryGroup.GET("/msg_transfer", pd.MessageTransfer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var etcdClient *clientv3.Client
|
||||||
|
if cfg.Discovery.Enable == config.ETCD {
|
||||||
|
etcdClient = client.(*etcd.SvcDiscoveryRegistryImpl).GetClient()
|
||||||
|
}
|
||||||
|
cm := NewConfigManager(cfg.Share.IMAdminUserID, cfg.AllConfig, etcdClient, cfg.ConfigPath, cfg.RuntimeEnv)
|
||||||
{
|
{
|
||||||
var etcdClient *clientv3.Client
|
|
||||||
if cfg.Discovery.Enable == config.ETCD {
|
|
||||||
etcdClient = client.(*etcd.SvcDiscoveryRegistryImpl).GetClient()
|
|
||||||
}
|
|
||||||
cm := NewConfigManager(cfg.Share.IMAdminUserID, cfg.AllConfig, etcdClient, cfg.ConfigPath, cfg.RuntimeEnv)
|
|
||||||
configGroup := r.Group("/config", cm.CheckAdmin)
|
configGroup := r.Group("/config", cm.CheckAdmin)
|
||||||
configGroup.POST("/get_config_list", cm.GetConfigList)
|
configGroup.POST("/get_config_list", cm.GetConfigList)
|
||||||
configGroup.POST("/get_config", cm.GetConfig)
|
configGroup.POST("/get_config", cm.GetConfig)
|
||||||
configGroup.POST("/set_config", cm.SetConfig)
|
configGroup.POST("/set_config", cm.SetConfig)
|
||||||
configGroup.POST("/reset_config", cm.ResetConfig)
|
configGroup.POST("/reset_config", cm.ResetConfig)
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
r.POST("/restart", cm.Restart)
|
||||||
|
}
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -103,6 +103,7 @@ func Start(ctx context.Context, index int, config *Config) error {
|
|||||||
config.Share.GetConfigFileName(),
|
config.Share.GetConfigFileName(),
|
||||||
config.WebhooksConfig.GetConfigFileName(),
|
config.WebhooksConfig.GetConfigFileName(),
|
||||||
config.Discovery.GetConfigFileName(),
|
config.Discovery.GetConfigFileName(),
|
||||||
|
conf.LogConfigFileName,
|
||||||
})
|
})
|
||||||
cm.Watch(ctx)
|
cm.Watch(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/openimsdk/tools/errs"
|
"github.com/openimsdk/tools/errs"
|
||||||
@ -15,18 +16,13 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
ConfigKeyPrefix = "/open-im/config/"
|
ConfigKeyPrefix = "/open-im/config/"
|
||||||
|
RestartKey = "restart"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ShutDowns []func() error
|
ShutDowns []func() error
|
||||||
CanRestart chan struct{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
CanRestart = make(chan struct{}, 1)
|
|
||||||
CanRestart <- struct{}{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func RegisterShutDown(shutDown ...func() error) {
|
func RegisterShutDown(shutDown ...func() error) {
|
||||||
ShutDowns = append(ShutDowns, shutDown...)
|
ShutDowns = append(ShutDowns, shutDown...)
|
||||||
}
|
}
|
||||||
@ -34,6 +30,7 @@ func RegisterShutDown(shutDown ...func() error) {
|
|||||||
type ConfigManager struct {
|
type ConfigManager struct {
|
||||||
client *clientv3.Client
|
client *clientv3.Client
|
||||||
watchConfigNames []string
|
watchConfigNames []string
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func BuildKey(s string) string {
|
func BuildKey(s string) string {
|
||||||
@ -43,7 +40,7 @@ func BuildKey(s string) string {
|
|||||||
func NewConfigManager(client *clientv3.Client, configNames []string) *ConfigManager {
|
func NewConfigManager(client *clientv3.Client, configNames []string) *ConfigManager {
|
||||||
return &ConfigManager{
|
return &ConfigManager{
|
||||||
client: client,
|
client: client,
|
||||||
watchConfigNames: datautil.Batch(func(s string) string { return BuildKey(s) }, configNames)}
|
watchConfigNames: datautil.Batch(func(s string) string { return BuildKey(s) }, append(configNames, RestartKey))}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigManager) Watch(ctx context.Context) {
|
func (c *ConfigManager) Watch(ctx context.Context) {
|
||||||
@ -61,12 +58,12 @@ func (c *ConfigManager) Watch(ctx context.Context) {
|
|||||||
for _, event := range watchResp.Events {
|
for _, event := range watchResp.Events {
|
||||||
if event.IsModify() {
|
if event.IsModify() {
|
||||||
if datautil.Contain(string(event.Kv.Key), c.watchConfigNames...) {
|
if datautil.Contain(string(event.Kv.Key), c.watchConfigNames...) {
|
||||||
<-CanRestart
|
c.lock.Lock()
|
||||||
err := restartServer(ctx)
|
err := restartServer(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(ctx, "restart server err", err)
|
log.ZError(ctx, "restart server err", err)
|
||||||
CanRestart <- struct{}{}
|
|
||||||
}
|
}
|
||||||
|
c.lock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -53,6 +53,7 @@ func Start[T any](ctx context.Context, discovery *conf.Discovery, prometheusConf
|
|||||||
rpcFn func(ctx context.Context, config T, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error,
|
rpcFn func(ctx context.Context, config T, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error,
|
||||||
options ...grpc.ServerOption) error {
|
options ...grpc.ServerOption) error {
|
||||||
|
|
||||||
|
watchConfigNames = append(watchConfigNames, conf.LogConfigFileName)
|
||||||
var (
|
var (
|
||||||
rpcTcpAddr string
|
rpcTcpAddr string
|
||||||
netDone = make(chan struct{}, 2)
|
netDone = make(chan struct{}, 2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user