Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vc/export install status obj #25

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}