refactor: move source function to own function

This commit is contained in:
Alphyron 2024-08-27 07:50:36 +02:00
parent 23bae0a5f8
commit ceba57ffd8
2 changed files with 50 additions and 30 deletions

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"net" "net"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
pb "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc" pb "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -40,43 +39,21 @@ func Listen(ctx context.Context, listenAddr string, source models.Source) error
var err error var err error
span.SetAttributes( span.SetAttributes(
attribute.String("source_display_name", string(source.DisplayName)), attribute.String("source_display_name", source.DisplayName),
attribute.String("source_domain", string(source.Domain)), attribute.String("source_domain", string(source.Domain)),
) )
sourceFields := log.Fields{
"source_display_name": source.DisplayName,
"source_domain": source.Domain,
}
serverFields := log.Fields{ serverFields := log.Fields{
"address": listenAddr, "address": listenAddr,
} }
source, err = database.GetSourceByDomain(ctx, source.Domain) source, err = upsertSource(ctx, source)
if err != nil { if err != nil {
if err.Error() == "Database error: NoDataFound" { span.RecordError(err)
span.AddEvent("No Source found, initializing source") span.SetStatus(codes.Error, err.Error())
log.WithContext(ctx).WithError(err).WithFields(serverFields).Error("Failed to upsert Source")
log.WithContext(ctx).WithFields(sourceFields).Info("No Source found, initializing source!") return err
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.SetAttributes( span.SetAttributes(

43
pkg/plug/source.go Normal file
View 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
}