Skip to content

Commit

Permalink
Add Task.String()
Browse files Browse the repository at this point in the history
  • Loading branch information
gfr10598 authored and Gregory Russell committed Apr 21, 2018
1 parent 0113b8d commit b650fe6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package state
// NOTE: Avoid dependencies on any other m-lab code.
import (
"errors"
"fmt"
)

// State indicates the state of a single Task in flight.
Expand All @@ -21,6 +22,17 @@ const (
Done // Done all processing, ok to delete state.
)

// StateNames maps from State to string, for use in String()
var StateNames map[State]string = map[State]string{
Initializing: "Initializing",
Queuing: "Queuing",
Processing: "Processing",
Stabilizing: "Stabilizing",
Deduplicating: "Deduplicating",
Finishing: "Finishing",
Done: "Done",
}

// Saver provides API for saving Task state.
type Saver interface {
SaveTask(t Task) error
Expand Down Expand Up @@ -52,6 +64,10 @@ type Task struct {
saver Saver // Saver is used for Save operations. Stored locally, but not persisted.
}

func (t Task) String() string {
return fmt.Sprintf("{%s: %s, Q:%s, J:%s, E:%v (%s)}", t.Name, StateNames[t.State], t.Queue, t.JobID, t.Err, t.ErrInfo)
}

// ErrNoSaver is returned when saver has not been set.
var ErrNoSaver = errors.New("Task.saver is nil")

Expand Down
7 changes: 5 additions & 2 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/m-lab/etl-gardener/state"
)

func init() {
// Always prepend the filename and line number.
log.SetFlags(log.LstdFlags | log.Lshortfile)
}

type testSaver struct {
tasks map[string][]state.Task
delete map[string]struct{}
Expand All @@ -19,7 +24,6 @@ func (s *testSaver) SaveTask(t state.Task) error {
}

func (s *testSaver) DeleteTask(t state.Task) error {
log.Println(t.Name)
s.delete[t.Name] = struct{}{}
return nil
}
Expand Down Expand Up @@ -70,5 +74,4 @@ func TestTaskBasics(t *testing.T) {
if !ok {
t.Fatal("Should have called delete")
}

}

0 comments on commit b650fe6

Please sign in to comment.