@@ -4,13 +4,13 @@ package manager
44import (
55 "encoding/json"
66 "fmt"
7+ "github.com/davecgh/go-spew/spew"
78 "io"
89 "os"
910 "time"
1011
1112 "github.com/sirupsen/logrus"
1213
13- "github.com/davecgh/go-spew/spew"
1414 "github.com/sjtug/lug/pkg/config"
1515 "github.com/sjtug/lug/pkg/worker"
1616)
@@ -51,7 +51,9 @@ type Status struct {
5151}
5252
5353type WorkerCheckPoint struct {
54- LastInvokeTime time.Time `json:"last_invoke_time"`
54+ LastInvokeTime time.Time `json:"last_invoke_time"`
55+ LastFinished * time.Time `json:"last_finished,omitempty"`
56+ Result bool `json:"result,omitempty"`
5557}
5658
5759type CheckPoint struct {
@@ -81,6 +83,21 @@ func fromCheckpoint(checkpointFile string) (*CheckPoint, error) {
8183 return & checkpoint , nil
8284}
8385
86+ func workerFromCheckpoint (repoConfig config.RepoConfig , checkpoint * CheckPoint , name string , lastInvokeTime time.Time ) (worker.Worker , error ) {
87+ if checkpoint == nil {
88+ return worker .NewWorker (repoConfig , lastInvokeTime , true )
89+ }
90+ info , ok := checkpoint .WorkerInfo [name ]
91+ if ! ok {
92+ return worker .NewWorker (repoConfig , lastInvokeTime , true )
93+ }
94+
95+ if info .LastFinished == nil {
96+ return worker .NewWorker (repoConfig , lastInvokeTime , info .Result )
97+ }
98+ return worker .NewWorker (repoConfig , * info .LastFinished , info .Result )
99+ }
100+
84101// NewManager creates a new manager with attached workers from config
85102func NewManager (config * config.Config ) (* Manager , error ) {
86103 logger := logrus .WithField ("manager" , "" )
@@ -110,7 +127,7 @@ func NewManager(config *config.Config) (*Manager, error) {
110127 if _ , ok := newManager .workersLastInvokeTime [name ]; ! ok {
111128 newManager .workersLastInvokeTime [name ] = time .Now ().AddDate (- 1 , 0 , 0 )
112129 }
113- w , err := worker . NewWorker (repoConfig , newManager .workersLastInvokeTime [name ])
130+ w , err := workerFromCheckpoint (repoConfig , checkpoint , name , newManager .workersLastInvokeTime [name ])
114131 if err != nil {
115132 return nil , err
116133 }
@@ -121,11 +138,21 @@ func NewManager(config *config.Config) (*Manager, error) {
121138
122139func (m * Manager ) checkpoint () error {
123140 ckptObj := & CheckPoint {WorkerInfo : make (map [string ]WorkerCheckPoint )}
124- for k , t := range m .workersLastInvokeTime {
125- ckptObj .WorkerInfo [k ] = WorkerCheckPoint {
126- LastInvokeTime : t ,
141+ for _ , w := range m .workers {
142+ name := w .GetConfig ()["name" ].(string )
143+ status := w .GetStatus ()
144+ lastInvokeTime , ok := m .workersLastInvokeTime [name ]
145+ if ! ok {
146+ lastInvokeTime = time .Now ().AddDate (- 1 , 0 , 0 )
147+ }
148+
149+ ckptObj .WorkerInfo [name ] = WorkerCheckPoint {
150+ LastInvokeTime : lastInvokeTime ,
151+ Result : status .Result ,
152+ LastFinished : & status .LastFinished ,
127153 }
128154 }
155+
129156 file , err := json .MarshalIndent (ckptObj , "" , " " )
130157 if err != nil {
131158 return err
0 commit comments