Skip to content

Commit

Permalink
reduce sdk option
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyang.zhu committed Mar 13, 2024
1 parent 2888948 commit d666bca
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 48 deletions.
74 changes: 52 additions & 22 deletions sdk/gen_pluginspec_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sdk/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type ZapLogger struct {
x *zap.SugaredLogger
}

// NewZapLogger creates a new ZapLogger.
func NewZapLogger() *ZapLogger {
// newZapLogger creates a new ZapLogger.
func newZapLogger() *ZapLogger {
return &ZapLogger{
x: logbus.GetZapLogger().Sugar(),
}
Expand Down
10 changes: 4 additions & 6 deletions sdk/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ var swapperManager *hotswap.PluginManagerSwapper
func MustInit(spec *PluginSpec) {
pluginDir := initWatchDir(spec)
swapper := hotswap.NewPluginManagerSwapper(pluginDir,
hotswap.WithLogger(NewZapLogger()), // use logbus
hotswap.WithFreeDelay(spec.GetHotswapSpec().GetFreeDelay()),
hotswap.WithWhitelist(spec.GetHotswapSpec().GetWhitelist()...),
hotswap.WithExtensionNewer(spec.GetHotswapSpec().GetExtensionNewer()),
hotswap.WithReloadCallback(spec.GetHotswapSpec().GetReloadCallback()),
hotswap.WithStaticPlugins(spec.GetHotswapSpec().GetStaticPlugins()),
hotswap.WithLogger(newZapLogger()), // use logbus
hotswap.WithFreeDelay(spec.GetFreeDelay()),
hotswap.WithExtensionNewer(spec.GetExtensionNewer()),
hotswap.WithStaticPlugins(spec.GetStaticPlugins()),
)
details, err := swapper.LoadPlugins(spec.GetOnFirstLoadData())
if err != nil {
Expand Down
22 changes: 14 additions & 8 deletions sdk/options.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package sdk

import "github.com/sandwich-go/hotswap"
import (
"time"

"github.com/sandwich-go/hotswap"
)

//go:generate optionGen --v=true --xconf=false --usage_tag_name=usage
func PluginSpecOptionDeclareWithDefault() interface{} {
return map[string]interface{}{
"MountDir": "/mount/data", // annotation@MountDir(comment="磁盘挂载目录")
"HotReload": true, // annotation@HotReload(comment="允许热更新,开启watch目录")
"DirsToKeep": 10, // annotation@DirsToKeep(comment="同一service, 磁盘保留发布的目录数")
"InternalDir": "bin/plugin", // annotation@InternalDir(comment="service pod内部携带的plugin目录")
"OnFirstLoadData": interface{}(nil), // annotation@OnFirstLoadData(comment="第一次OnLoad的data参数")
"OnReloadData": interface{}(nil), // annotation@OnReloadData(comment="热更时新插件OnLoad的data参数")
"HotswapSpec": (*hotswap.Spec)(hotswap.NewSpec()), // annotation@Spec(comment="hotswap参数")
"MountDir": "/mount/data", // annotation@MountDir(comment="磁盘挂载目录")
"HotReload": true, // annotation@HotReload(comment="允许热更新,开启watch目录")
"DirsToKeep": 10, // annotation@DirsToKeep(comment="同一service, 磁盘保留发布的目录数")
"InternalDir": "bin/plugin", // annotation@InternalDir(comment="service pod内部携带的plugin目录")
"OnFirstLoadData": interface{}(nil), // annotation@OnFirstLoadData(comment="第一次OnLoad的data参数")
"OnReloadData": interface{}(nil), // annotation@OnReloadData(comment="热更时新插件OnLoad的data参数")
"FreeDelay": time.Duration(15 * time.Second), // annotation@FreeDelay(comment="the delay time of calling OnFree. The default value is 5 minutes.")
"ExtensionNewer": (func() interface{})(nil), // annotation@ExtensionNewer(comment="the function used to create a new object for PluginManager.Vault.Extension.")
"StaticPlugins": map[string]*hotswap.StaticPlugin(nil), // annotation@StaticPlugins(comment="the static plugins for static linking. 宿主程序直接编译的插件 用做debug和windows")
}
}
20 changes: 10 additions & 10 deletions sdk/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ import (
"github.com/sandwich-go/logbus"
)

const EnvCommitId = "current_revision"
const EnvServiceName = "sys_cd_service"
const EnvCdEnv = "sys_cd_env"
const EnvStage = "sys_stage"
const FlagFileName = "version.txt"
const envCommitId = "current_revision"
const envServiceName = "sys_cd_service"
const envCdEnv = "sys_cd_env"
const envStage = "sys_stage"
const flagFileName = "version.txt"

func initWatchDir(spec *PluginSpec) (loadDir string) {
if !spec.GetHotReload() {
return spec.GetInternalDir()
}
commitId := os.Getenv(EnvCommitId)
serviceName := os.Getenv(EnvServiceName)
cdEnv := os.Getenv(EnvCdEnv)
stage := os.Getenv(EnvStage)
commitId := os.Getenv(envCommitId)
serviceName := os.Getenv(envServiceName)
cdEnv := os.Getenv(envCdEnv)
stage := os.Getenv(envStage)

var watchDir = spec.GetInternalDir()
if commitId != "" { // in k8s
// /mount/data/test/gate/online/c2das4/bin/plugin
watchDir = path.Join(spec.GetMountDir(), cdEnv, serviceName, stage, commitId, spec.GetInternalDir())
}
flagFile := path.Join(watchDir, FlagFileName)
flagFile := path.Join(watchDir, flagFileName)
// 检查目录是否存在
_, err := os.Stat(watchDir)
if os.IsNotExist(err) {
Expand Down

0 comments on commit d666bca

Please sign in to comment.