From 5f07bc2d7c1cc1aba62a1a1766acdb3a00bbe5e2 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Wed, 19 Jun 2024 12:40:56 +0000 Subject: [PATCH] Check if producer was registered Signed-off-by: Gabriel Adrian Samfira --- database/sql/instances.go | 7 +++++-- database/sql/util.go | 11 +++++++++-- database/watcher/watcher_store_test.go | 1 - 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/database/sql/instances.go b/database/sql/instances.go index 3f113669..864e7ba2 100644 --- a/database/sql/instances.go +++ b/database/sql/instances.go @@ -17,6 +17,7 @@ package sql import ( "context" "encoding/json" + "log/slog" "github.com/google/uuid" "github.com/pkg/errors" @@ -153,13 +154,15 @@ func (s *sqlDatabase) DeleteInstance(_ context.Context, poolID string, instanceN if instance.ProviderID != nil { providerID = *instance.ProviderID } - s.sendNotify(common.InstanceEntityType, common.DeleteOperation, params.Instance{ + if notifyErr := s.sendNotify(common.InstanceEntityType, common.DeleteOperation, params.Instance{ ID: instance.ID.String(), Name: instance.Name, ProviderID: providerID, AgentID: instance.AgentID, PoolID: instance.PoolID.String(), - }) + }); notifyErr != nil { + slog.With(slog.Any("error", notifyErr)).Error("failed to send notify") + } } }() diff --git a/database/sql/util.go b/database/sql/util.go index 5814483d..dd861197 100644 --- a/database/sql/util.go +++ b/database/sql/util.go @@ -488,11 +488,18 @@ func (s *sqlDatabase) unsealAndUnmarshal(data []byte, target interface{}) error return nil } -func (s *sqlDatabase) sendNotify(entityType dbCommon.DatabaseEntityType, op dbCommon.OperationType, payload interface{}) { +func (s *sqlDatabase) sendNotify(entityType dbCommon.DatabaseEntityType, op dbCommon.OperationType, payload interface{}) error { + if s.producer == nil { + // no producer was registered. Not sending notifications. + return nil + } + if payload == nil { + return errors.New("missing payload") + } message := dbCommon.ChangePayload{ Operation: op, Payload: payload, EntityType: entityType, } - s.producer.Notify(message) + return s.producer.Notify(message) } diff --git a/database/watcher/watcher_store_test.go b/database/watcher/watcher_store_test.go index 80f71325..f74f836d 100644 --- a/database/watcher/watcher_store_test.go +++ b/database/watcher/watcher_store_test.go @@ -491,7 +491,6 @@ func (s *WatcherStoreTestSuite) TestGithubCredentialsWatcher() { ghCred, err := s.store.CreateGithubCredentials(s.ctx, ghCredParams) s.Require().NoError(err) s.Require().NotEmpty(ghCred.ID) - s.T().Cleanup(func() { s.store.DeleteGithubCredentials(s.ctx, ghCred.ID) }) select { case event := <-consumer.Watch():