parent
d72d3147f3
commit
12adfa50cc
@ -1,34 +0,0 @@
|
|||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ConfigEnvPrefixMap map[string]string
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
ConfigEnvPrefixMap = make(map[string]string)
|
|
||||||
fileNames := []string{
|
|
||||||
config.FileName, config.NotificationFileName, config.ShareFileName, config.WebhooksConfigFileName,
|
|
||||||
config.KafkaConfigFileName, config.RedisConfigFileName,
|
|
||||||
config.MongodbConfigFileName, config.MinioConfigFileName, config.LogConfigFileName,
|
|
||||||
config.OpenIMAPICfgFileName, config.OpenIMCronTaskCfgFileName, config.OpenIMMsgGatewayCfgFileName,
|
|
||||||
config.OpenIMMsgTransferCfgFileName, config.OpenIMPushCfgFileName, config.OpenIMRPCAuthCfgFileName,
|
|
||||||
config.OpenIMRPCConversationCfgFileName, config.OpenIMRPCFriendCfgFileName, config.OpenIMRPCGroupCfgFileName,
|
|
||||||
config.OpenIMRPCMsgCfgFileName, config.OpenIMRPCThirdCfgFileName, config.OpenIMRPCUserCfgFileName, config.DiscoveryConfigFilename,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, fileName := range fileNames {
|
|
||||||
envKey := strings.TrimSuffix(strings.TrimSuffix(fileName, ".yml"), ".yaml")
|
|
||||||
envKey = "IMENV_" + envKey
|
|
||||||
envKey = strings.ToUpper(strings.ReplaceAll(envKey, "-", "_"))
|
|
||||||
ConfigEnvPrefixMap[fileName] = envKey
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
FlagConf = "config_folder_path"
|
|
||||||
FlagTransferIndex = "index"
|
|
||||||
)
|
|
@ -0,0 +1,30 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
var EnvPrefixMap map[string]string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
EnvPrefixMap = make(map[string]string)
|
||||||
|
fileNames := []string{
|
||||||
|
FileName, NotificationFileName, ShareFileName, WebhooksConfigFileName,
|
||||||
|
KafkaConfigFileName, RedisConfigFileName,
|
||||||
|
MongodbConfigFileName, MinioConfigFileName, LogConfigFileName,
|
||||||
|
OpenIMAPICfgFileName, OpenIMCronTaskCfgFileName, OpenIMMsgGatewayCfgFileName,
|
||||||
|
OpenIMMsgTransferCfgFileName, OpenIMPushCfgFileName, OpenIMRPCAuthCfgFileName,
|
||||||
|
OpenIMRPCConversationCfgFileName, OpenIMRPCFriendCfgFileName, OpenIMRPCGroupCfgFileName,
|
||||||
|
OpenIMRPCMsgCfgFileName, OpenIMRPCThirdCfgFileName, OpenIMRPCUserCfgFileName, DiscoveryConfigFilename,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, fileName := range fileNames {
|
||||||
|
envKey := strings.TrimSuffix(strings.TrimSuffix(fileName, ".yml"), ".yaml")
|
||||||
|
envKey = "IMENV_" + envKey
|
||||||
|
envKey = strings.ToUpper(strings.ReplaceAll(envKey, "-", "_"))
|
||||||
|
EnvPrefixMap[fileName] = envKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
FlagConf = "config_folder_path"
|
||||||
|
FlagTransferIndex = "index"
|
||||||
|
)
|
@ -0,0 +1,91 @@
|
|||||||
|
package etcd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/openimsdk/tools/errs"
|
||||||
|
"github.com/openimsdk/tools/log"
|
||||||
|
"github.com/openimsdk/tools/utils/datautil"
|
||||||
|
clientv3 "go.etcd.io/etcd/client/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ConfigKeyPrefix = "/config/"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ConfigManager struct {
|
||||||
|
client *clientv3.Client
|
||||||
|
watchConfigNames []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func BuildKey(s string) string {
|
||||||
|
return ConfigKeyPrefix + s
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfigManager(client *clientv3.Client, configNames []string) *ConfigManager {
|
||||||
|
return &ConfigManager{
|
||||||
|
client: client,
|
||||||
|
watchConfigNames: datautil.Batch(func(s string) string { return BuildKey(s) }, configNames)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConfigManager) Watch(ctx context.Context) {
|
||||||
|
chans := make([]clientv3.WatchChan, 0, len(c.watchConfigNames))
|
||||||
|
for _, name := range c.watchConfigNames {
|
||||||
|
chans = append(chans, c.client.Watch(ctx, name, clientv3.WithPrefix()))
|
||||||
|
}
|
||||||
|
|
||||||
|
doWatch := func(watchChan clientv3.WatchChan) {
|
||||||
|
for watchResp := range watchChan {
|
||||||
|
if watchResp.Err() != nil {
|
||||||
|
log.ZError(ctx, "watch err", errs.Wrap(watchResp.Err()))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, event := range watchResp.Events {
|
||||||
|
if event.IsModify() {
|
||||||
|
if datautil.Contain(string(event.Kv.Key), c.watchConfigNames...) {
|
||||||
|
err := restartServer(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.ZError(ctx, "restart server err", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, ch := range chans {
|
||||||
|
go doWatch(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func restartServer(ctx context.Context) error {
|
||||||
|
exePath, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return errs.New("get executable path fail").Wrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
args := os.Args
|
||||||
|
env := os.Environ()
|
||||||
|
|
||||||
|
cmd := exec.Command(exePath, args[1:]...)
|
||||||
|
cmd.Env = env
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
|
||||||
|
if runtime.GOOS != "windows" {
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.ZInfo(ctx, "restart server")
|
||||||
|
err = cmd.Start()
|
||||||
|
if err != nil {
|
||||||
|
return errs.New("restart server fail").Wrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in new issue