Skip to content

Commit

Permalink
Fix heartbeat events (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
keliramu authored Oct 23, 2024
2 parents 583ad32 + 9caa799 commit dcaac6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
5 changes: 1 addition & 4 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,11 @@ func main() {
log.Println(internal.WarningPrefix, err)
}
}
heartBeatSubject.Subscribe(analytics.NotifyHeartBeat)
daemonEvents.Subscribe(analytics)
daemonEvents.Service.Connect.Subscribe(loggerSubscriber.NotifyConnect)
daemonEvents.Settings.Publish(cfg)

// Subscribing after initial settings publishing ensures that heartbeat is not sent with
// the first `NotifyMeshnet` call but will be sent on the first heartbeat job.
heartBeatSubject.Subscribe(analytics.NotifyHeartBeat)

if fsystem.NewInstallation {
daemonEvents.Service.UiItemsClick.Publish(events.UiItemsAction{ItemName: "first_open", ItemType: "button", ItemValue: "first_open", FormReference: "daemon"})
}
Expand Down
2 changes: 1 addition & 1 deletion daemon/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *RPC) StartJobs(
log.Println(internal.WarningPrefix, "job version schedule error:", err)
}

if _, err := r.scheduler.NewJob(gocron.DurationJob(heartBeatPeriod), gocron.NewTask(JobHeartBeat(heartBeatPublisher, heartBeatPeriod), gocron.WithName("job heart beat"))); err != nil {
if _, err := r.scheduler.NewJob(gocron.DurationJob(heartBeatPeriod), gocron.NewTask(JobHeartBeat(heartBeatPublisher, heartBeatPeriod)), gocron.WithName("job heart beat")); err != nil {
log.Println(internal.WarningPrefix, "job heart beat schedule error:", err)
}
if _, err := r.scheduler.NewJob(gocron.DurationJob(7*24*time.Hour), gocron.NewTask(func() {
Expand Down
18 changes: 15 additions & 3 deletions events/moose/moose.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Subscriber struct {
connectionStartTime time.Time
connectionToMeshnetPeer bool
enabled bool
initialHeartbeatSent bool
mux sync.RWMutex
}

Expand Down Expand Up @@ -347,7 +348,15 @@ func (s *Subscriber) NotifyUiItemsClick(data events.UiItemsAction) error {
}

func (s *Subscriber) NotifyHeartBeat(period time.Duration) error {
return s.response(moose.NordvpnappSendServiceQualityStatusHeartbeat(int32(period.Minutes())))
if err := s.response(moose.NordvpnappSendServiceQualityStatusHeartbeat(int32(period.Minutes()))); err != nil {
return err
}
if !s.initialHeartbeatSent {
s.mux.Lock()
defer s.mux.Unlock()
s.initialHeartbeatSent = true
}
return nil
}

func (s *Subscriber) NotifyDeviceLocation(insights core.Insights) error {
Expand All @@ -372,8 +381,11 @@ func (s *Subscriber) NotifyMeshnet(data bool) error {
if err := s.response(moose.NordvpnappSetContextApplicationNordvpnappConfigUserPreferencesMeshnetEnabledValue(data)); err != nil {
return err
}
// 0 duration indicates that this is not a periodic heart beat
return s.NotifyHeartBeat(time.Duration(0))
if s.initialHeartbeatSent {
// 0 duration indicates that this is not a periodic heart beat
return s.NotifyHeartBeat(time.Duration(0))
}
return nil
}

func (s *Subscriber) NotifyObfuscate(data bool) error {
Expand Down

0 comments on commit dcaac6f

Please sign in to comment.