From 9caa799be7020471c8a683c68bde9cc41ba0651c Mon Sep 17 00:00:00 2001 From: Savolro Date: Wed, 23 Oct 2024 14:59:53 +0300 Subject: [PATCH] Fix heartbeat events 1. Fix invalid arguments to gocron.Scheduler.NewJob 2. Add initial heartbeat checking logic to moose subscriber Signed-off-by: Savolro --- cmd/daemon/main.go | 5 +---- daemon/jobs.go | 2 +- events/moose/moose.go | 18 +++++++++++++++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/daemon/main.go b/cmd/daemon/main.go index e9133a50..f3b06175 100644 --- a/cmd/daemon/main.go +++ b/cmd/daemon/main.go @@ -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"}) } diff --git a/daemon/jobs.go b/daemon/jobs.go index 403af04f..fd576214 100644 --- a/daemon/jobs.go +++ b/daemon/jobs.go @@ -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() { diff --git a/events/moose/moose.go b/events/moose/moose.go index 695e3d8b..5178bd2d 100644 --- a/events/moose/moose.go +++ b/events/moose/moose.go @@ -60,6 +60,7 @@ type Subscriber struct { connectionStartTime time.Time connectionToMeshnetPeer bool enabled bool + initialHeartbeatSent bool mux sync.RWMutex } @@ -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 { @@ -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 {