mirror of
https://github.com/zrepl/zrepl.git
synced 2025-08-19 03:06:02 +02:00
status: infra for reporting jobs instead of just replication.Report
This commit is contained in:
@@ -2,7 +2,9 @@ package job
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/zrepl/zrepl/logger"
|
||||
)
|
||||
@@ -47,10 +49,73 @@ func WithWakeup(ctx context.Context) (context.Context, WakeupFunc) {
|
||||
type Job interface {
|
||||
Name() string
|
||||
Run(ctx context.Context)
|
||||
Status() interface{}
|
||||
Status() *Status
|
||||
RegisterMetrics(registerer prometheus.Registerer)
|
||||
}
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
TypeInternal Type = "internal"
|
||||
TypePush Type = "push"
|
||||
TypeSink Type = "sink"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Type Type
|
||||
JobSpecific interface{}
|
||||
}
|
||||
|
||||
func (s *Status) MarshalJSON() ([]byte, error) {
|
||||
typeJson, err := json.Marshal(s.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
jobJSON, err := json.Marshal(s.JobSpecific)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := map[string]json.RawMessage {
|
||||
"type": typeJson,
|
||||
string(s.Type): jobJSON,
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
func (s *Status) UnmarshalJSON(in []byte) (err error) {
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(in, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
tJSON, ok := m["type"]
|
||||
if !ok {
|
||||
return fmt.Errorf("field 'type' not found")
|
||||
}
|
||||
if err := json.Unmarshal(tJSON, &s.Type); err != nil {
|
||||
return err
|
||||
}
|
||||
key := string(s.Type)
|
||||
jobJSON, ok := m[key]
|
||||
if !ok {
|
||||
return fmt.Errorf("field '%s', not found", key)
|
||||
}
|
||||
switch s.Type {
|
||||
case TypePush:
|
||||
var st PushStatus
|
||||
err = json.Unmarshal(jobJSON, &st)
|
||||
s.JobSpecific = &st
|
||||
case TypeSink:
|
||||
var st SinkStatus
|
||||
err = json.Unmarshal(jobJSON, &st)
|
||||
s.JobSpecific = &st
|
||||
case TypeInternal:
|
||||
// internal jobs do not report specifics
|
||||
default:
|
||||
err = fmt.Errorf("unknown job type '%s'", key)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func WaitWakeup(ctx context.Context) <-chan struct{} {
|
||||
wc, ok := ctx.Value(contextKeyWakeup).(chan struct{})
|
||||
if !ok {
|
||||
|
@@ -89,7 +89,11 @@ func (j *Push) RegisterMetrics(registerer prometheus.Registerer) {
|
||||
|
||||
func (j *Push) Name() string { return j.name }
|
||||
|
||||
func (j *Push) Status() interface{} {
|
||||
type PushStatus struct {
|
||||
Replication *replication.Report
|
||||
}
|
||||
|
||||
func (j *Push) Status() *Status {
|
||||
rep := func() *replication.Replication {
|
||||
j.mtx.Lock()
|
||||
defer j.mtx.Unlock()
|
||||
@@ -98,10 +102,12 @@ func (j *Push) Status() interface{} {
|
||||
}
|
||||
return j.replication
|
||||
}()
|
||||
s := &PushStatus{}
|
||||
if rep == nil {
|
||||
return nil
|
||||
return &Status{Type: TypePush, JobSpecific: s}
|
||||
}
|
||||
return rep.Report()
|
||||
s.Replication = rep.Report()
|
||||
return &Status{Type: TypePush, JobSpecific: s}
|
||||
}
|
||||
|
||||
func (j *Push) Run(ctx context.Context) {
|
||||
|
@@ -41,9 +41,10 @@ func SinkFromConfig(g *config.Global, in *config.SinkJob) (s *Sink, err error) {
|
||||
|
||||
func (j *Sink) Name() string { return j.name }
|
||||
|
||||
func (*Sink) Status() interface{} {
|
||||
// FIXME
|
||||
return nil
|
||||
type SinkStatus struct {}
|
||||
|
||||
func (*Sink) Status() *Status {
|
||||
return &Status{Type: TypeSink} // FIXME SinkStatus
|
||||
}
|
||||
|
||||
func (*Sink) RegisterMetrics(registerer prometheus.Registerer) {}
|
||||
|
Reference in New Issue
Block a user