Check if producer was registered

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2024-06-19 12:40:56 +00:00
parent 0c8c6f5668
commit 5f07bc2d7c
3 changed files with 14 additions and 5 deletions

View file

@ -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")
}
}
}()

View file

@ -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)
}