From 937d9fc529325cc98d8c2e847763580d0c32ba10 Mon Sep 17 00:00:00 2001 From: Alex Chi Date: Mon, 4 Jan 2021 17:01:57 +0800 Subject: [PATCH] fix json rest time --- pkg/manager/.tmp | 3 +++ pkg/manager/manager.go | 10 +++++----- pkg/worker/worker.go | 4 ++-- pkg/worker/worker_test.go | 11 ++++++----- 4 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 pkg/manager/.tmp diff --git a/pkg/manager/.tmp b/pkg/manager/.tmp new file mode 100644 index 0000000..ba91b14 --- /dev/null +++ b/pkg/manager/.tmp @@ -0,0 +1,3 @@ +{ + "worker_info": {} +} \ No newline at end of file diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index 60c3a5c..8d9421c 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -106,15 +106,15 @@ func NewManager(config *config.Config) (*Manager, error) { if disabled, ok := repoConfig["disabled"].(bool); ok && disabled { continue } - w, err := worker.NewWorker(repoConfig) - if err != nil { - return nil, err - } - newManager.workers = append(newManager.workers, w) name, _ := repoConfig["name"].(string) if _, ok := newManager.workersLastInvokeTime[name]; !ok { newManager.workersLastInvokeTime[name] = time.Now().AddDate(-1, 0, 0) } + w, err := worker.NewWorker(repoConfig, newManager.workersLastInvokeTime[name]) + if err != nil { + return nil, err + } + newManager.workers = append(newManager.workers, w) } return &newManager, nil } diff --git a/pkg/worker/worker.go b/pkg/worker/worker.go index 16de5a9..4351eb2 100644 --- a/pkg/worker/worker.go +++ b/pkg/worker/worker.go @@ -34,7 +34,7 @@ type Status struct { } // NewWorker generates a worker by config and log. -func NewWorker(cfg config.RepoConfig) (Worker, error) { +func NewWorker(cfg config.RepoConfig, lastFinished time.Time) (Worker, error) { if syncType, ok := cfg["type"]; ok { switch syncType { case "rsync": @@ -45,7 +45,7 @@ func NewWorker(cfg config.RepoConfig) (Worker, error) { newShellScriptExecutor(cfg), Status{ Result: true, - LastFinished: time.Now().AddDate(-1, 0, 0), + LastFinished: lastFinished, Idle: true, Stdout: make([]string, 0), Stderr: make([]string, 0), diff --git a/pkg/worker/worker_test.go b/pkg/worker/worker_test.go index 243f651..6e365df 100644 --- a/pkg/worker/worker_test.go +++ b/pkg/worker/worker_test.go @@ -12,9 +12,10 @@ import ( "github.com/stretchr/testify/assert" "errors" + "sync/atomic" + "github.com/sirupsen/logrus" "github.com/sjtug/lug/pkg/config" - "sync/atomic" ) func TestNewExternalWorker(t *testing.T) { @@ -23,12 +24,12 @@ func TestNewExternalWorker(t *testing.T) { "blahblah": "foobar", "type": "external", } - _, err := NewWorker(c) + _, err := NewWorker(c, time.Now()) // worker with no name is not allowed asrt.NotNil(err) c["name"] = "test_external" - w, err := NewWorker(c) + w, err := NewWorker(c, time.Now()) // config with name and dummy kv pairs should be allowed asrt.Nil(err) @@ -47,7 +48,7 @@ func TestNewShellScriptWorker(t *testing.T) { asrt := assert.New(t) - w, _ := NewWorker(c) + w, _ := NewWorker(c, time.Now()) asrt.Equal(true, w.GetStatus().Result) asrt.Equal("shell_script", w.GetConfig()["type"]) @@ -186,7 +187,7 @@ func TestShellScriptWorkerArgParse(t *testing.T) { "name": "shell", "script": "wc -l /proc/stat", } - w, err := NewWorker(c) + w, err := NewWorker(c, time.Now()) asrt := assert.New(t) asrt.Nil(err)