2024-07-19 13:28:48 +00:00
|
|
|
package plug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
2024-08-29 13:26:58 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
|
2024-10-16 10:20:21 +00:00
|
|
|
pb "git.anthrove.art/Anthrove/plug-sdk/v4/pkg/grpc"
|
2024-08-26 11:05:50 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-08-30 13:20:32 +00:00
|
|
|
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
|
2024-08-26 11:01:31 +00:00
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
2024-07-19 13:28:48 +00:00
|
|
|
"google.golang.org/grpc"
|
2024-08-26 11:01:31 +00:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2024-07-19 13:28:48 +00:00
|
|
|
)
|
|
|
|
|
2024-10-16 10:20:21 +00:00
|
|
|
var tracer = otel.Tracer("git.anthrove.art/Anthrove/plug-sdk/v4/pkg/plug")
|
2024-08-26 14:33:40 +00:00
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
type Message struct {
|
|
|
|
Title string
|
|
|
|
Body string
|
2024-08-26 08:57:05 +00:00
|
|
|
CreatedAt *timestamppb.Timestamp
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 14:08:34 +00:00
|
|
|
type TaskSummery struct {
|
|
|
|
AddedPosts int
|
|
|
|
DeletedPosts int
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskExecution func(ctx context.Context, userSource models.UserSource, deepScrape bool, apiKey string) (TaskSummery, error)
|
2024-08-17 18:23:11 +00:00
|
|
|
type SendMessageExecution func(ctx context.Context, userSource models.UserSource, message string) error
|
|
|
|
type GetMessageExecution func(ctx context.Context, userSource models.UserSource) ([]Message, error)
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
var (
|
2024-07-19 13:28:48 +00:00
|
|
|
taskExecutionFunction TaskExecution
|
|
|
|
sendMessageExecution SendMessageExecution
|
|
|
|
getMessageExecution GetMessageExecution
|
2024-08-27 05:45:30 +00:00
|
|
|
)
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
func Listen(ctx context.Context, listenAddr string, source models.Source) error {
|
2024-08-27 05:37:19 +00:00
|
|
|
ctx, span := tracer.Start(ctx, "Listen")
|
2024-08-26 11:01:31 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
var err error
|
|
|
|
|
2024-08-26 11:01:31 +00:00
|
|
|
span.SetAttributes(
|
2024-08-27 05:50:36 +00:00
|
|
|
attribute.String("source_display_name", source.DisplayName),
|
2024-08-27 05:45:30 +00:00
|
|
|
attribute.String("source_domain", string(source.Domain)),
|
2024-08-26 11:01:31 +00:00
|
|
|
)
|
|
|
|
|
2024-08-26 11:05:50 +00:00
|
|
|
serverFields := log.Fields{
|
2024-08-27 05:45:30 +00:00
|
|
|
"address": listenAddr,
|
2024-08-26 11:01:31 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 05:50:36 +00:00
|
|
|
source, err = upsertSource(ctx, source)
|
2024-08-26 11:01:31 +00:00
|
|
|
|
2024-08-27 05:50:36 +00:00
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithError(err).WithFields(serverFields).Error("Failed to upsert Source")
|
|
|
|
return err
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-26 11:01:31 +00:00
|
|
|
span.SetAttributes(
|
2024-08-27 05:45:30 +00:00
|
|
|
attribute.String("source_id", string(source.ID)),
|
2024-08-26 11:01:31 +00:00
|
|
|
)
|
2024-08-17 18:23:11 +00:00
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
lis, err := net.Listen("tcp", listenAddr)
|
2024-07-19 13:28:48 +00:00
|
|
|
if err != nil {
|
2024-08-26 11:01:31 +00:00
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
2024-08-26 11:05:50 +00:00
|
|
|
log.WithContext(ctx).WithError(err).WithFields(serverFields).Error("Failed to listen on address")
|
2024-07-19 13:28:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-08-30 13:20:32 +00:00
|
|
|
grpcServer := grpc.NewServer(
|
|
|
|
grpc.StatsHandler(otelgrpc.NewServerHandler()),
|
|
|
|
)
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(source, taskExecutionFunction, sendMessageExecution, getMessageExecution))
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-27 05:37:19 +00:00
|
|
|
go func() {
|
|
|
|
err = grpcServer.Serve(lis)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
log.WithContext(ctx).WithError(err).Error("Failed to serve gRPC server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.WithContext(ctx).Info("Context stopped! Shutdown Plug")
|
|
|
|
grpcServer.GracefulStop()
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
func SetTaskExecutionFunction(function TaskExecution) {
|
|
|
|
taskExecutionFunction = function
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
func SetSendMessageExecutionFunction(function SendMessageExecution) {
|
|
|
|
sendMessageExecution = function
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 05:45:30 +00:00
|
|
|
func SetGetMessageExecutionFunction(function GetMessageExecution) {
|
|
|
|
getMessageExecution = function
|
2024-08-26 11:01:31 +00:00
|
|
|
}
|