Send and receive SMS messages using your Android phone programmatically via a simple HTTP API
Add code to automatically do heartbeat checks
Acho Arnoldd4cd43d4ad54
diff --git a/api/pkg/di/container.go b/api/pkg/di/container.goindex 7763fbd8..a47771a2 100644--- a/api/pkg/di/container.go+++ b/api/pkg/di/container.go@@ -268,26 +268,10 @@ func (container *Container) DB() (db *gorm.DB) {container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Message{})))}- if err = db.AutoMigrate(&repositories.GormEvent{}); err != nil {- container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &repositories.GormEvent{})))- }-- if err = db.AutoMigrate(&entities.EventListenerLog{}); err != nil {- container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.EventListenerLog{})))- }-if err = db.AutoMigrate(&entities.MessageThread{}); err != nil {container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.MessageThread{})))}- if err = db.AutoMigrate(&entities.Heartbeat{}); err != nil {- container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Heartbeat{})))- }-- if err = db.AutoMigrate(&entities.HeartbeatMonitor{}); err != nil {- container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.HeartbeatMonitor{})))- }-if err = db.AutoMigrate(&entities.User{}); err != nil {container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.User{})))}@@ -693,7 +677,7 @@ func (container *Container) HeartbeatMonitorRepository() (repository repositoriereturn repositories.NewGormHeartbeatMonitorRepository(container.Logger(),container.Tracer(),- container.DB(),+ container.YugaByteDB(),)}diff --git a/api/pkg/entities/heartbeat_monitor.go b/api/pkg/entities/heartbeat_monitor.goindex bc4e6b1f..ef1e8e74 100644--- a/api/pkg/entities/heartbeat_monitor.go+++ b/api/pkg/entities/heartbeat_monitor.go@@ -16,3 +16,8 @@ type HeartbeatMonitor struct {CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"`}++// RequiresCheck returns true if the heartbeat monitor requires a check+func (h *HeartbeatMonitor) RequiresCheck() bool {+ return h.UpdatedAt.Add(2 * time.Hour).Before(time.Now())+}diff --git a/api/pkg/services/heartbeat_service.go b/api/pkg/services/heartbeat_service.goindex 18b86a73..5b14511a 100644--- a/api/pkg/services/heartbeat_service.go+++ b/api/pkg/services/heartbeat_service.go@@ -108,51 +108,67 @@ type HeartbeatMonitorStoreParams struct {// StoreMonitor a new entities.HeartbeatMonitorfunc (service *HeartbeatService) StoreMonitor(ctx context.Context, params *HeartbeatMonitorStoreParams) (*entities.HeartbeatMonitor, error) {- ctx, span := service.tracer.Start(ctx)+ ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)defer span.End()- ctxLogger := service.tracer.CtxLogger(service.logger, span)-- exists, err := service.monitorRepository.Exists(ctx, params.UserID, params.Owner)+ monitor, scheduleCheck, err := service.phoneMonitor(ctx, params)if err != nil {- msg := fmt.Sprintf("cannot check if monitor exists with userID [%s] and owner [%s]", params.UserID, params.Owner)+ msg := fmt.Sprintf("cannot create monitor for with userID [%s] and owner [%s]", params.UserID, params.Owner)return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))}- if exists {- ctxLogger.Info(fmt.Sprintf("heartbeat monitor exists for owner [%s] and user [%s]", params.Owner, params.UserID))- return nil, nil- }-- heartbeatMonitor := &entities.HeartbeatMonitor{- ID: uuid.New(),- PhoneID: params.PhoneID,- UserID: params.UserID,- Owner: params.Owner,- CreatedAt: time.Now().UTC(),- UpdatedAt: time.Now().UTC(),- }-- if err = service.monitorRepository.Store(ctx, heartbeatMonitor); err != nil {- msg := fmt.Sprintf("cannot save heartbeat monitor for owner [%s] and user [%s]", heartbeatMonitor.Owner, heartbeatMonitor.UserID)- return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))+ if !scheduleCheck {+ ctxLogger.Info(fmt.Sprintf("heartbeat monitor [%s] for owner [%s] does not need scheduling because it was updated at [%s]", monitor.ID, monitor.Owner, monitor.UpdatedAt))+ return monitor, nil}- ctxLogger.Info(fmt.Sprintf("heartbeat monitor saved with id [%s] for owner [%s] and user [%s]", heartbeatMonitor.ID, heartbeatMonitor.Owner, heartbeatMonitor.UserID))+ ctxLogger.Info(fmt.Sprintf("scheduling heartbeat monitor [%s] for owner [%s] and user [%s]", monitor.ID, monitor.Owner, monitor.UserID))monitorParams := &HeartbeatMonitorParams{- Owner: heartbeatMonitor.Owner,- PhoneID: heartbeatMonitor.PhoneID,- UserID: heartbeatMonitor.UserID,- MonitorID: heartbeatMonitor.ID,+ Owner: monitor.Owner,+ PhoneID: monitor.PhoneID,+ UserID: monitor.UserID,+ MonitorID: monitor.ID,Source: params.Source,}if err = service.scheduleHeartbeatCheck(ctx, time.Now().UTC(), monitorParams); err != nil {- msg := fmt.Sprintf("cannot schedule healthcheck for monitor with owner [%s] and userID [%s]", params.Owner, params.UserID)+ msg := fmt.Sprintf("cannot schedule healthcheck for monitor [%s] with owner [%s] and userID [%s]", monitor.ID, params.Owner, params.UserID)return nil, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))}- return heartbeatMonitor, nil+ return monitor, nil+}++func (service *HeartbeatService) phoneMonitor(ctx context.Context, params *HeartbeatMonitorStoreParams) (*entities.HeartbeatMonitor, bool, error) {+ ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)+ defer span.End()++ monitor, err := service.monitorRepository.Load(ctx, params.UserID, params.Owner)+ if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {+ monitor = &entities.HeartbeatMonitor{+ ID: uuid.New(),+ PhoneID: params.PhoneID,+ UserID: params.UserID,+ Owner: params.Owner,+ CreatedAt: time.Now().UTC(),+ UpdatedAt: time.Now().UTC(),+ }++ if err = service.monitorRepository.Store(ctx, monitor); err != nil {+ msg := fmt.Sprintf("cannot save heartbeat monitor for owner [%s] and user [%s]", monitor.Owner, monitor.UserID)+ return nil, false, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))+ }++ ctxLogger.Info(fmt.Sprintf("heartbeat monitor saved with id [%s] for owner [%s] and user [%s]", monitor.ID, monitor.Owner, monitor.UserID))+ return monitor, true, nil+ }++ if err != nil {+ msg := fmt.Sprintf("cannot check if monitor exists with userID [%s] and owner [%s]", params.UserID, params.Owner)+ return nil, false, service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))+ }++ return monitor, monitor.RequiresCheck(), nil}// DeleteMonitor an entities.HeartbeatMonitor