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

Fix heartbeat events #667

Merged
merged 1 commit into from
Oct 23, 2024
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
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
Loading