github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/bob/playbook/status.go (about)

     1  package playbook
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/Benchkram/bob/bobtask"
     8  )
     9  
    10  // Status holds the state of a task
    11  // inside a playbook.
    12  type Status struct {
    13  	bobtask.Task
    14  
    15  	stateMu sync.RWMutex
    16  	state   State
    17  
    18  	Start time.Time
    19  	End   time.Time
    20  
    21  	Error error
    22  }
    23  
    24  func NewStatus(task bobtask.Task) *Status {
    25  	return &Status{
    26  		Task:  task,
    27  		state: StatePending,
    28  		Start: time.Now(),
    29  	}
    30  }
    31  
    32  func (ts *Status) State() State {
    33  	ts.stateMu.RLock()
    34  	defer ts.stateMu.RUnlock()
    35  	return ts.state
    36  }
    37  
    38  func (ts *Status) SetState(s State, err error) {
    39  	ts.stateMu.Lock()
    40  	defer ts.stateMu.Unlock()
    41  	ts.state = s
    42  	ts.Error = err
    43  }
    44  
    45  func (ts *Status) ExecutionTime() time.Duration {
    46  	return ts.End.Sub(ts.Start)
    47  }