Skip to content

Commit

Permalink
feat: bcs-task mysql store add WithDebug option (#3592)
Browse files Browse the repository at this point in the history
* feat: bcs-task mysql store add WithDebug option

* feat: add bcs-task mysql store add createdAt
  • Loading branch information
ifooth authored Oct 24, 2024
1 parent b2a0fc9 commit f38a1c1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 47 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-services/bcs-webconsole
bcs-monitor:
Expand All @@ -38,7 +38,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-services/bcs-monitor
bcs-bscp:
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m --out-format=colored-line-number
working-directory: bcs-services/bcs-project-manager
bcs-user-manager:
Expand All @@ -139,7 +139,7 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
args: --timeout=30m
args: --timeout=30m --out-format=colored-line-number
working-directory: bcs-services/bcs-user-manager
bcs-cluster-resources:
name: bcs-cluster-resources
Expand All @@ -158,7 +158,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-services/cluster-resources

Expand All @@ -179,7 +179,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.61.0
args: --timeout=30m
working-directory: bcs-common/common/task
- name: Test
Expand Down
4 changes: 2 additions & 2 deletions bcs-common/common/task/stores/iface/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type ListOption struct {
CurrentStep string
Status string
Creator string
StartGte *time.Time // StartGte start time greater or equal to
StartLte *time.Time // StartLte start time less or equal to
CreatedGte *time.Time // CreatedGte create time greater or equal to
CreatedLte *time.Time // CreatedLte create time less or equal to
Sort map[string]int // Sort map for sort list results
Offset int64 // Offset offset for list results
Limit int64 // Limit limit for list results
Expand Down
3 changes: 2 additions & 1 deletion bcs-common/common/task/stores/mysql/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func toTask(task *TaskRecord, steps []*StepRecord) *types.Task {
End: task.End,
ExecutionTime: task.ExecutionTime,
MaxExecutionSeconds: task.MaxExecutionSeconds,
Creator: task.Creator,
CreatedAt: task.CreatedAt,
LastUpdate: task.UpdatedAt,
Creator: task.Creator,
Updater: task.Updater,
}

Expand Down
58 changes: 22 additions & 36 deletions bcs-common/common/task/stores/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package mysql

import (
"context"
"net/url"
"strconv"

"gorm.io/driver/mysql"
"gorm.io/gorm"
Expand All @@ -27,19 +25,30 @@ import (
)

type mysqlStore struct {
dsn string
showDebug bool
db *gorm.DB
dsn string
debug bool
db *gorm.DB
}

type option func(*mysqlStore)

// WithDebug 是否显示sql语句
func WithDebug(debug bool) option {
return func(s *mysqlStore) {
s.debug = debug
}
}

// New init mysql iface.Store
func New(dsn string) (iface.Store, error) {
store := &mysqlStore{dsn: dsn, showDebug: false}
store.initDsn(dsn)
func New(dsn string, opts ...option) (iface.Store, error) {
store := &mysqlStore{dsn: dsn, debug: false}
for _, opt := range opts {
opt(store)
}

// 是否显示sql语句
level := logger.Warn
if store.showDebug {
if store.debug {
level = logger.Info
}

Expand All @@ -54,29 +63,6 @@ func New(dsn string) (iface.Store, error) {
return store, nil
}

// initDsn 解析debug参数是否开启sql显示, 任意异常都原样不动
func (s *mysqlStore) initDsn(raw string) {
u, err := url.Parse(raw)
if err != nil {
return
}
query := u.Query()

// 是否开启debug
debugStr := query.Get("debug")
if debugStr != "" {
debug, err := strconv.ParseBool(debugStr)
if err != nil {
return
}
s.showDebug = debug
query.Del("debug")
u.RawQuery = query.Encode()
}

s.dsn = u.String()
}

// EnsureTable implement istore EnsureTable interface
func (s *mysqlStore) EnsureTable(ctx context.Context, dst ...any) error {
// 没有自定义数据, 使用默认表结构
Expand Down Expand Up @@ -120,11 +106,11 @@ func (s *mysqlStore) ListTask(ctx context.Context, opt *iface.ListOption) (*ifac
})

// mysql store 使用创建时间过滤
if opt.StartGte != nil {
tx = tx.Where("created_at >= ?", opt.StartGte)
if opt.CreatedGte != nil {
tx = tx.Where("created_at >= ?", opt.CreatedGte)
}
if opt.StartLte != nil {
tx = tx.Where("created_at <= ?", opt.StartLte)
if opt.CreatedLte != nil {
tx = tx.Where("created_at <= ?", opt.CreatedLte)
}

// 只使用id排序
Expand Down
1 change: 1 addition & 0 deletions bcs-common/common/task/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Task struct {
Updater string `json:"updater"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
CreatedAt time.Time `json:"createdAt"`
LastUpdate time.Time `json:"lastUpdate"`
}

Expand Down
4 changes: 2 additions & 2 deletions bcs-services/bcs-user-manager/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func parseConfig(op *options.UserManagerOptions) (*config.UserMgrConfig, error)

// RedisDSN 没有配置,检查 RedisConfig
if userMgrConfig.RedisDSN == "" {
redisConfig, err := parseRedisConfig(op.RedisConfig)
redisConfig, aErr := parseRedisConfig(op.RedisConfig)
if err != nil {
return nil, fmt.Errorf("error parsing redis config and exit: %s", err.Error())
return nil, fmt.Errorf("error parsing redis config and exit: %s", aErr.Error())
}
userMgrConfig.RedisConfig = redisConfig
}
Expand Down

0 comments on commit f38a1c1

Please sign in to comment.