refactor: move source function to own function
This commit is contained in:
parent
23bae0a5f8
commit
ceba57ffd8
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
pb "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -40,43 +39,21 @@ func Listen(ctx context.Context, listenAddr string, source models.Source) error
|
||||
var err error
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("source_display_name", string(source.DisplayName)),
|
||||
attribute.String("source_display_name", source.DisplayName),
|
||||
attribute.String("source_domain", string(source.Domain)),
|
||||
)
|
||||
|
||||
sourceFields := log.Fields{
|
||||
"source_display_name": source.DisplayName,
|
||||
"source_domain": source.Domain,
|
||||
}
|
||||
|
||||
serverFields := log.Fields{
|
||||
"address": listenAddr,
|
||||
}
|
||||
|
||||
source, err = database.GetSourceByDomain(ctx, source.Domain)
|
||||
source, err = upsertSource(ctx, source)
|
||||
|
||||
if err != nil {
|
||||
if err.Error() == "Database error: NoDataFound" {
|
||||
span.AddEvent("No Source found, initializing source")
|
||||
|
||||
log.WithContext(ctx).WithFields(sourceFields).Info("No Source found, initializing source!")
|
||||
|
||||
source, err = database.CreateSource(ctx, source)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
log.WithError(err).WithFields(sourceFields).Error("Failed to create source")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
span.AddEvent("Source created")
|
||||
log.WithContext(ctx).WithError(err).WithFields(sourceFields).WithField("source_id", source.ID).Info("Source created!")
|
||||
|
||||
} else {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
log.WithContext(ctx).WithError(err).WithFields(sourceFields).Error("Failed to get source by domain")
|
||||
panic(err)
|
||||
}
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
log.WithContext(ctx).WithError(err).WithFields(serverFields).Error("Failed to upsert Source")
|
||||
return err
|
||||
}
|
||||
|
||||
span.SetAttributes(
|
||||
|
43
pkg/plug/source.go
Normal file
43
pkg/plug/source.go
Normal file
@ -0,0 +1,43 @@
|
||||
package plug
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
|
||||
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
)
|
||||
|
||||
func upsertSource(ctx context.Context, source models.Source) (models.Source, error) {
|
||||
ctx, span := tracer.Start(ctx, "upsertSource")
|
||||
|
||||
var err error
|
||||
|
||||
source, err = database.GetSourceByDomain(ctx, source.Domain)
|
||||
if err != nil {
|
||||
if err.Error() == "Database error: NoDataFound" {
|
||||
span.AddEvent("No Source found, initializing source")
|
||||
|
||||
log.WithContext(ctx).WithField("source_domain", source.Domain).Info("No Source found, initializing source!")
|
||||
|
||||
source, err = database.CreateSource(ctx, source)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
log.WithError(err).WithField("source_domain", source.Domain).Error("Failed to create source")
|
||||
return models.Source{}, err
|
||||
}
|
||||
|
||||
span.AddEvent("Source created")
|
||||
log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).WithField("source_id", source.ID).Info("Source created!")
|
||||
|
||||
} else {
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).Error("Failed to get source by domain")
|
||||
return models.Source{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return source, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user