From 57d12cb2de84e5806b1d4e4a635ca3c5c8b08442 Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Tue, 31 Aug 2021 21:46:04 +0800 Subject: [PATCH] Refactor: dependency initialize order --- bootstrap/init.go | 91 ++++++++++++++++--- models/setting.go | 12 ++- pkg/filesystem/driver/shadow/slave/handler.go | 2 +- pkg/task/slavetask/transfer.go | 2 +- 4 files changed, 89 insertions(+), 18 deletions(-) diff --git a/bootstrap/init.go b/bootstrap/init.go index 729a577..6b43adb 100644 --- a/bootstrap/init.go +++ b/bootstrap/init.go @@ -23,18 +23,85 @@ func Init(path string) { gin.SetMode(gin.ReleaseMode) } - cache.Init() - if conf.SystemConfig.Mode == "master" { - model.Init() - task.Init() - cluster.Init() - aria2.Init(false) - email.Init() - crontab.Init() - InitStatic() - } else { - slave.Init() + dependencies := []struct { + mode string + factory func() + }{ + { + "both", + func() { + cache.Init() + }, + }, + { + "master", + func() { + model.Init() + }, + }, + { + "both", + func() { + task.Init() + }, + }, + { + "master", + func() { + cluster.Init() + }, + }, + { + "master", + func() { + aria2.Init(false) + }, + }, + { + "master", + func() { + email.Init() + }, + }, + { + "master", + func() { + crontab.Init() + }, + }, + { + "master", + func() { + InitStatic() + }, + }, + { + "slave", + func() { + slave.Init() + }, + }, + { + "both", + func() { + auth.Init() + }, + }, + } + + for _, dependency := range dependencies { + switch dependency.mode { + case "master": + if conf.SystemConfig.Mode == "master" { + dependency.factory() + } + case "slave": + if conf.SystemConfig.Mode == "slave" { + dependency.factory() + } + default: + dependency.factory() + } } - auth.Init() } diff --git a/models/setting.go b/models/setting.go index f8157cf..1738c1d 100644 --- a/models/setting.go +++ b/models/setting.go @@ -30,12 +30,16 @@ func GetSettingByName(name string) string { if optionValue, ok := cache.Get(cacheKey); ok { return optionValue.(string) } + // 尝试数据库中查找 - result := DB.Where("name = ?", name).First(&setting) - if result.Error == nil { - _ = cache.Set(cacheKey, setting.Value, -1) - return setting.Value + if DB != nil { + result := DB.Where("name = ?", name).First(&setting) + if result.Error == nil { + _ = cache.Set(cacheKey, setting.Value, -1) + return setting.Value + } } + return "" } diff --git a/pkg/filesystem/driver/shadow/slave/handler.go b/pkg/filesystem/driver/shadow/slave/handler.go index 515d9e2..d1713dc 100644 --- a/pkg/filesystem/driver/shadow/slave/handler.go +++ b/pkg/filesystem/driver/shadow/slave/handler.go @@ -65,7 +65,7 @@ func (d Driver) Put(ctx context.Context, file io.ReadCloser, dst string, size ui return err } - res, err := d.client.Request("PUT", "task、transfer", bytes.NewReader(body)). + res, err := d.client.Request("PUT", "task/transfer", bytes.NewReader(body)). CheckHTTPResponse(200). DecodeResponse() if err != nil { diff --git a/pkg/task/slavetask/transfer.go b/pkg/task/slavetask/transfer.go index 0210fe9..bfdc0e7 100644 --- a/pkg/task/slavetask/transfer.go +++ b/pkg/task/slavetask/transfer.go @@ -59,5 +59,5 @@ func (job *TransferTask) GetError() *task.JobError { // Do 开始执行任务 func (job *TransferTask) Do() { - util.Log().Println("job", "") + util.Log().Debug("job") }