fix: test case issue

This commit is contained in:
Marvin Zhang
2024-11-19 15:53:40 +08:00
parent d436087404
commit 47cf368f26
16 changed files with 102 additions and 238 deletions

View File

@@ -1,68 +1,15 @@
package config
import (
"github.com/crawlab-team/crawlab/core/constants"
"github.com/crawlab-team/crawlab/core/entity"
"github.com/crawlab-team/crawlab/core/utils"
"github.com/spf13/viper"
)
type Config entity.NodeInfo
type Options struct {
Key string
Name string
IsMaster bool
AuthKey string
MaxRunners int
}
var DefaultMaxRunner = 8
var DefaultConfigOptions = &Options{
Key: utils.NewUUIDString(),
IsMaster: utils.IsMaster(),
AuthKey: constants.DefaultGrpcAuthKey,
MaxRunners: 0,
}
func NewConfig(opts *Options) (cfg *Config) {
if opts == nil {
opts = DefaultConfigOptions
}
if opts.Key == "" {
if viper.GetString("node.key") != "" {
opts.Key = viper.GetString("node.key")
} else {
opts.Key = utils.NewUUIDString()
}
}
if opts.Name == "" {
if viper.GetString("node.name") != "" {
opts.Name = viper.GetString("node.name")
} else {
opts.Name = opts.Key
}
}
if opts.AuthKey == "" {
if viper.GetString("grpc.authKey") != "" {
opts.AuthKey = viper.GetString("grpc.authKey")
} else {
opts.AuthKey = constants.DefaultGrpcAuthKey
}
}
if opts.MaxRunners == 0 {
if viper.GetInt("task.handler.maxRunners") != 0 {
opts.MaxRunners = viper.GetInt("task.handler.maxRunners")
} else {
opts.MaxRunners = DefaultMaxRunner
}
}
return &Config{
Key: opts.Key,
Name: opts.Name,
IsMaster: opts.IsMaster,
AuthKey: opts.AuthKey,
MaxRunners: opts.MaxRunners,
func newConfig() (cfg *entity.NodeInfo) {
return &entity.NodeInfo{
Key: utils.GetNodeKey(),
Name: utils.GetNodeName(),
IsMaster: utils.IsMaster(),
MaxRunners: utils.GetNodeMaxRunners(),
}
}

View File

@@ -2,7 +2,7 @@ package config
import (
"encoding/json"
"github.com/crawlab-team/crawlab/core/config"
"github.com/apex/log"
"github.com/crawlab-team/crawlab/core/entity"
"github.com/crawlab-team/crawlab/core/interfaces"
"github.com/crawlab-team/crawlab/core/utils"
@@ -13,38 +13,42 @@ import (
)
type Service struct {
cfg *Config
path string
cfg *entity.NodeInfo
}
func (svc *Service) Init() (err error) {
metadataConfigPath := utils.GetMetadataConfigPath()
// check config directory path
configDirPath := filepath.Dir(svc.path)
configDirPath := filepath.Dir(metadataConfigPath)
if !utils.Exists(configDirPath) {
if err := os.MkdirAll(configDirPath, os.FileMode(0766)); err != nil {
return trace.TraceError(err)
}
}
if !utils.Exists(svc.path) {
// not exists, set to default config
// and create a config file for persistence
svc.cfg = NewConfig(nil)
if !utils.Exists(metadataConfigPath) {
// not exists, set to default config, and create a config file for persistence
svc.cfg = newConfig()
data, err := json.Marshal(svc.cfg)
if err != nil {
return trace.TraceError(err)
log.Errorf("marshal config error: %v", err)
return err
}
if err := os.WriteFile(svc.path, data, os.FileMode(0766)); err != nil {
return trace.TraceError(err)
if err := os.WriteFile(metadataConfigPath, data, os.FileMode(0766)); err != nil {
log.Errorf("write config file error: %v", err)
return err
}
} else {
// exists, read and set to config
data, err := os.ReadFile(svc.path)
data, err := os.ReadFile(metadataConfigPath)
if err != nil {
return trace.TraceError(err)
log.Errorf("read config file error: %v", err)
return err
}
if err := json.Unmarshal(data, svc.cfg); err != nil {
return trace.TraceError(err)
log.Errorf("unmarshal config error: %v", err)
return err
}
}
@@ -86,27 +90,12 @@ func (svc *Service) GetMaxRunners() (res int) {
return svc.cfg.MaxRunners
}
func (svc *Service) GetConfigPath() (path string) {
return svc.path
}
func (svc *Service) SetConfigPath(path string) {
svc.path = path
}
func newNodeConfigService() (svc2 interfaces.NodeConfigService, err error) {
// cfg
cfg := NewConfig(nil)
// config service
svc := &Service{
cfg: cfg,
cfg: newConfig(),
}
// normalize config path
cfgPath := config.GetConfigPath()
svc.SetConfigPath(cfgPath)
// init
if err := svc.Init(); err != nil {
return nil, err