daemon: DaemonStatus + JobStatus + dummy implementation

refs #10
This commit is contained in:
Christian Schwarz
2017-12-24 15:34:41 +01:00
parent 2c87b15e83
commit 8c7e373049
5 changed files with 56 additions and 2 deletions
+40 -2
View File
@@ -29,6 +29,7 @@ func init() {
type Job interface {
JobName() string
JobStart(ctxt context.Context)
JobStatus(ctxt context.Context) (*JobStatus, error)
}
func doDaemon(cmd *cobra.Command, args []string) {
@@ -58,15 +59,18 @@ const (
)
type Daemon struct {
conf *Config
conf *Config
startedAt time.Time
}
func NewDaemon(initialConf *Config) *Daemon {
return &Daemon{initialConf}
return &Daemon{conf: initialConf}
}
func (d *Daemon) Loop(ctx context.Context) {
d.startedAt = time.Now()
log := ctx.Value(contextKeyLog).(Logger)
ctx, cancel := context.WithCancel(ctx)
@@ -114,6 +118,39 @@ outer:
}
// Representation of a Job's status that is composed of Tasks
type JobStatus struct {
// Statuses of all tasks of this job
Tasks []*TaskStatus
// Error != "" if JobStatus() returned an error
JobStatusError string
}
// Representation of a Daemon's status that is composed of Jobs
type DaemonStatus struct {
StartedAt time.Time
Jobs map[string]*JobStatus
}
func (d *Daemon) Status() (s *DaemonStatus) {
s = &DaemonStatus{}
s.StartedAt = d.startedAt
s.Jobs = make(map[string]*JobStatus, len(d.conf.Jobs))
for name, j := range d.conf.Jobs {
status, err := j.JobStatus(context.TODO())
if err != nil {
s.Jobs[name] = &JobStatus{nil, err.Error()}
continue
}
s.Jobs[name] = status
}
return
}
// Representation of a Task's status
type TaskStatus struct {
Name string
@@ -337,4 +374,5 @@ func (u *IOProgressUpdater) Read(p []byte) (n int, err error) {
n, err = u.r.Read(p)
u.p.UpdateIO(int64(n), 0)
return
}