Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bcs-task mysql store add WithDebug option #3592

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 17 additions & 31 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 @@ -32,10 +30,21 @@ type mysqlStore struct {
db *gorm.DB
}

type option func(*mysqlStore)

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

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

// 是否显示sql语句
level := logger.Warn
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
Loading