Skip to content

Commit

Permalink
move the location update to the register pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Oct 28, 2024
1 parent b4cae92 commit 1e76c1c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 136 deletions.
9 changes: 0 additions & 9 deletions cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/threefoldtech/zos/pkg/perf/publicip"
"github.com/threefoldtech/zos/pkg/registrar"
"github.com/threefoldtech/zos/pkg/stubs"
"github.com/threefoldtech/zos/pkg/updater"
"github.com/threefoldtech/zos/pkg/utils"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -158,14 +157,6 @@ func action(cli *cli.Context) error {

go registerationServer(ctx, msgBrokerCon, env, info)

log.Info().Msg("start node updater")
nodeUpdater := updater.NewUpdater(redis)
if err != nil {
return errors.Wrap(err, "failed to create new node updater")
}

go nodeUpdater.Start(ctx)

log.Info().Msg("start perf scheduler")

perfMon, err := perf.NewPerformanceMonitor(msgBrokerCon)
Expand Down
10 changes: 0 additions & 10 deletions pkg/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ package pkg

//go:generate zbusc -module registrar -version 0.0.1 -name registrar -package stubs github.com/threefoldtech/zos4/pkg+Registrar stubs/registrar_stub.go

type RegistrationState string

type State struct {
NodeID uint32
TwinID uint32
State RegistrationState
Msg string
}

type Registrar interface {
NodeID() (uint32, error)
TwinID() (uint32, error)
GetState() State
}
40 changes: 24 additions & 16 deletions pkg/registrar/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import (
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg"
"github.com/threefoldtech/zos/pkg/app"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/geoip"
"github.com/threefoldtech/zos/pkg/stubs"
"github.com/threefoldtech/zos4/pkg/geoip"
)

// should any of this be moved to pkg?
type RegistrationState string

const (
Failed pkg.RegistrationState = "Failed"
InProgress pkg.RegistrationState = "InProgress"
Done pkg.RegistrationState = "Done"
Failed RegistrationState = "Failed"
InProgress RegistrationState = "InProgress"
Done RegistrationState = "Done"

monitorAccountEvery = 30 * time.Minute
updateLocationInterval = 24 * time.Hour
Expand All @@ -36,26 +37,33 @@ var (
ErrFailed = errors.New("registration failed")
)

func FailedState(err error) pkg.State {
return pkg.State{
type State struct {
NodeID uint32
TwinID uint32
State RegistrationState
Msg string
}

func FailedState(err error) State {
return State{
0,
0,
Failed,
err.Error(),
}
}

func InProgressState() pkg.State {
return pkg.State{
func InProgressState() State {
return State{
0,
0,
InProgress,
"",
}
}

func DoneState(nodeID uint32, twinID uint32) pkg.State {
return pkg.State{
func DoneState(nodeID uint32, twinID uint32) State {
return State{
nodeID,
twinID,
Done,
Expand All @@ -64,13 +72,13 @@ func DoneState(nodeID uint32, twinID uint32) pkg.State {
}

type Registrar struct {
state pkg.State
state State
mutex sync.RWMutex
}

func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environment, info RegistrationInfo) *Registrar {
r := Registrar{
pkg.State{
state: State{
0,
0,
InProgress,
Expand All @@ -83,13 +91,13 @@ func NewRegistrar(ctx context.Context, cl zbus.Client, env environment.Environme
return &r
}

func (r *Registrar) setState(s pkg.State) {
func (r *Registrar) setState(s State) {
r.mutex.Lock()
defer r.mutex.Unlock()
r.state = s
}

func (r *Registrar) GetState() pkg.State {
func (r *Registrar) getState() State {
r.mutex.RLock()
defer r.mutex.RUnlock()
return r.state
Expand Down Expand Up @@ -205,11 +213,11 @@ func (r *Registrar) updateLocation(ctx context.Context, cl zbus.Client) error {
}

func (r *Registrar) NodeID() (uint32, error) {
return r.returnIfDone(r.GetState().NodeID)
return r.returnIfDone(r.getState().NodeID)
}

func (r *Registrar) TwinID() (uint32, error) {
return r.returnIfDone(r.GetState().TwinID)
return r.returnIfDone(r.getState().TwinID)
}

func (r *Registrar) returnIfDone(v uint32) (uint32, error) {
Expand Down
17 changes: 0 additions & 17 deletions pkg/stubs/registrar_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package stubs
import (
"context"
zbus "github.com/threefoldtech/zbus"
pkg "github.com/threefoldtech/zos/pkg"
)

type RegistrarStub struct {
Expand All @@ -27,22 +26,6 @@ func NewRegistrarStub(client zbus.Client) *RegistrarStub {
}
}

func (s *RegistrarStub) GetState(ctx context.Context) (ret0 pkg.State) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "GetState", args...)
if err != nil {
panic(err)
}
result.PanicOnError()
loader := zbus.Loader{
&ret0,
}
if err := result.Unmarshal(&loader); err != nil {
panic(err)
}
return
}

func (s *RegistrarStub) NodeID(ctx context.Context) (ret0 uint32, ret1 error) {
args := []interface{}{}
result, err := s.client.RequestContext(ctx, s.module, s.object, "NodeID", args...)
Expand Down
84 changes: 0 additions & 84 deletions pkg/updater/updater.go

This file was deleted.

0 comments on commit 1e76c1c

Please sign in to comment.