From 8c7e37304968cc401f5d7f3828cc2740f4267e6c Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 24 Dec 2017 15:34:41 +0100 Subject: [PATCH] daemon: DaemonStatus + JobStatus + dummy implementation refs #10 --- cmd/config_job_control.go | 4 ++++ cmd/config_job_local.go | 4 ++++ cmd/config_job_pull.go | 4 ++++ cmd/config_job_source.go | 4 ++++ cmd/daemon.go | 42 +++++++++++++++++++++++++++++++++++++-- 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/cmd/config_job_control.go b/cmd/config_job_control.go index ba9ff63..08cbf9c 100644 --- a/cmd/config_job_control.go +++ b/cmd/config_job_control.go @@ -32,6 +32,10 @@ func (j *ControlJob) JobName() string { return j.Name } +func (j *ControlJob) JobStatus(ctx context.Context) (*JobStatus, error) { + return &JobStatus{}, nil +} + const ( ControlJobEndpointProfile string = "/debug/pprof/profile" ControlJobEndpointVersion string = "/version" diff --git a/cmd/config_job_local.go b/cmd/config_job_local.go index 77bd5a1..75ae3d5 100644 --- a/cmd/config_job_local.go +++ b/cmd/config_job_local.go @@ -173,6 +173,10 @@ outer: } +func (j *LocalJob) JobStatus(ctxt context.Context) (*JobStatus, error) { + return &JobStatus{}, nil +} + func (j *LocalJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) { var dsfilter zfs.DatasetFilter diff --git a/cmd/config_job_pull.go b/cmd/config_job_pull.go index 0fb1a2b..4795628 100644 --- a/cmd/config_job_pull.go +++ b/cmd/config_job_pull.go @@ -149,6 +149,10 @@ start: } +func (j *PullJob) JobStatus(ctxt context.Context) (*JobStatus, error) { + return &JobStatus{}, nil +} + func (j *PullJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) { p = Pruner{ time.Now(), diff --git a/cmd/config_job_source.go b/cmd/config_job_source.go index a96b3ae..0ed2d55 100644 --- a/cmd/config_job_source.go +++ b/cmd/config_job_source.go @@ -108,6 +108,10 @@ outer: } +func (j *SourceJob) JobStatus(ctxt context.Context) (*JobStatus, error) { + return &JobStatus{}, nil +} + func (j *SourceJob) Pruner(side PrunePolicySide, dryRun bool) (p Pruner, err error) { p = Pruner{ time.Now(), diff --git a/cmd/daemon.go b/cmd/daemon.go index cd5390e..ad9a388 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -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 + }