Skip to content

Commit

Permalink
Vc/export install status obj (#25)
Browse files Browse the repository at this point in the history
* export the Flasher status value and version it

* change the facility separator in the key to period
  • Loading branch information
DoctorVin authored Jun 9, 2023
1 parent 42b35e4 commit 7e9c86e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
24 changes: 3 additions & 21 deletions internal/worker/kv_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
package worker

import (
"encoding/json"
"fmt"
"time"

sm "github.com/metal-toolbox/flasher/internal/statemachine"
"github.com/metal-toolbox/flasher/types"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"

Expand All @@ -30,7 +30,7 @@ type statusKVPublisher struct {

// Publish implements the statemachine Publisher interface.
func (s *statusKVPublisher) Publish(hCtx *sm.HandlerContext) {
key := fmt.Sprintf("%s/%s", hCtx.Asset.FacilityCode, hCtx.Task.ID.String())
key := fmt.Sprintf("%s.%s", hCtx.Asset.FacilityCode, hCtx.Task.ID.String())
payload := statusFromContext(hCtx)

var err error
Expand All @@ -51,26 +51,8 @@ func (s *statusKVPublisher) Publish(hCtx *sm.HandlerContext) {
hCtx.LastRev = rev
}

type statusValue struct {
UpdatedAt time.Time `json:"updated"`
WorkerID string `json:"worker"`
Target string `json:"target"`
State string `json:"state"`
Status json.RawMessage `json:"status"`
// WorkSpec json.RawMessage `json:"spec"`
}

// panic if we cannot serialize to JSON
func (v *statusValue) MustBytes() []byte {
byt, err := json.Marshal(v)
if err != nil {
panic("unable to serialize status value: " + err.Error())
}
return byt
}

func statusFromContext(hCtx *sm.HandlerContext) []byte {
sv := &statusValue{
sv := &types.StatusValue{
WorkerID: hCtx.WorkerID.String(),
Target: hCtx.Asset.ID.String(),
State: string(hCtx.Task.State()),
Expand Down
8 changes: 5 additions & 3 deletions internal/worker/kv_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/uuid"
"github.com/metal-toolbox/flasher/internal/model"
sm "github.com/metal-toolbox/flasher/internal/statemachine"
"github.com/metal-toolbox/flasher/types"
"github.com/nats-io/nats-server/v2/server"
srvtest "github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -88,19 +89,20 @@ func TestPublisher(t *testing.T) {
require.NotPanics(t, func() { pub.Publish(testContext) }, "publish initial")
require.NotEqual(t, 0, testContext.LastRev, "last rev - 1")

entry, err := readHandle.Get("fac13/" + taskID.String())
entry, err := readHandle.Get("fac13." + taskID.String())
require.Equal(t, entry.Revision(), testContext.LastRev, "last rev - 2")

sv := &statusValue{}
sv := &types.StatusValue{}
err = json.Unmarshal(entry.Value(), sv)
require.NoError(t, err, "unmarshal")

require.Equal(t, types.Version, sv.Version, "version check")
require.Equal(t, assetID.String(), sv.Target, "sv Target")
require.Equal(t, json.RawMessage(`{"msg":"some-status"}`), sv.Status, "sv Status")

testContext.Task.SetState(model.StateActive)
require.NotPanics(t, func() { pub.Publish(testContext) }, "publish revision")

entry, err = readHandle.Get("fac13/" + taskID.String())
entry, err = readHandle.Get("fac13." + taskID.String())
require.Equal(t, entry.Revision(), testContext.LastRev, "last rev - 3")
}
32 changes: 32 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package types

import (
"encoding/json"
"time"
)

const (
Version int32 = 1
)

// StatusValue is the canonical structure
type StatusValue struct {
UpdatedAt time.Time `json:"updated"`
WorkerID string `json:"worker"`
Target string `json:"target"`
State string `json:"state"`
Status json.RawMessage `json:"status"`
Version int32 `json:"version"`
// WorkSpec json.RawMessage `json:"spec"` XXX: for re-publish use-cases
}

// MustBytes sets the version field of the StatusValue so any callers don't have
// to deal with it. It will panic if we cannot serialize to JSON for some reason.
func (v *StatusValue) MustBytes() []byte {
v.Version = Version
byt, err := json.Marshal(v)
if err != nil {
panic("unable to serialize status value: " + err.Error())
}
return byt
}

0 comments on commit 7e9c86e

Please sign in to comment.