Skip to content

Commit

Permalink
make sure that uptime is fetched after substrate connection (#1956)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhamadazmy committed Apr 27, 2023
1 parent 9da4c22 commit fc6b894
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions pkg/power/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,33 @@ func NewUptime(sub substrate.Manager, id substrate.Identity) (*Uptime, error) {
}

func (u *Uptime) SendNow() (types.Hash, error) {
uptime, err := host.Uptime()
if err != nil {
return types.Hash{}, errors.Wrap(err, "failed to get uptime")
}
return u.send(uptime)
}

func (u *Uptime) send(uptime uint64) (types.Hash, error) {
// the mutex is to avoid race when SendNow is called
// while the times reporting is working
u.m.Lock()
defer u.m.Unlock()

// this can take sometime in case of connection problems
// hence we first establish a connection THEN get the node
// uptime.
// to make sure the uptime is correct at the time of reporting
sub, err := u.sub.Substrate()
if err != nil {
return types.Hash{}, err
}
defer sub.Close()

uptime, err := host.Uptime()
if err != nil {
return types.Hash{}, errors.Wrap(err, "failed to get uptime")
}

return sub.UpdateNodeUptime(u.id, uptime)
}

func (u *Uptime) uptime(ctx context.Context) error {
for {
uptime, err := host.Uptime()
if err != nil {
return errors.Wrap(err, "failed to get uptime")
}
log.Debug().Msg("updating node uptime")
hash, err := u.send(uptime)
hash, err := u.SendNow()
if err != nil {
return errors.Wrap(err, "failed to report uptime")
}
Expand Down

0 comments on commit fc6b894

Please sign in to comment.