2024-07-19 13:28:48 +00:00
|
|
|
package plug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-27 12:17:31 +00:00
|
|
|
"errors"
|
2024-10-15 14:08:34 +00:00
|
|
|
"time"
|
2024-08-26 08:51:00 +00:00
|
|
|
|
2024-08-29 13:26:58 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
|
2024-08-14 14:16:42 +00:00
|
|
|
gRPC "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc"
|
2024-07-19 13:28:48 +00:00
|
|
|
gonanoid "github.com/matoous/go-nanoid/v2"
|
2024-08-26 13:42:42 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2024-07-19 13:28:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
gRPC.UnimplementedPlugConnectorServer
|
|
|
|
ctx map[string]context.CancelFunc
|
|
|
|
taskExecutionFunction TaskExecution
|
|
|
|
sendMessageExecution SendMessageExecution
|
|
|
|
getMessageExecution GetMessageExecution
|
2024-08-15 07:56:40 +00:00
|
|
|
source models.Source
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 07:56:40 +00:00
|
|
|
func NewGrpcServer(source models.Source, taskExecutionFunction TaskExecution, sendMessageExecution SendMessageExecution, getMessageExecution GetMessageExecution) gRPC.PlugConnectorServer {
|
2024-07-19 13:28:48 +00:00
|
|
|
return &server{
|
|
|
|
ctx: make(map[string]context.CancelFunc),
|
|
|
|
taskExecutionFunction: taskExecutionFunction,
|
|
|
|
sendMessageExecution: sendMessageExecution,
|
|
|
|
getMessageExecution: getMessageExecution,
|
2024-08-15 07:56:40 +00:00
|
|
|
source: source,
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation) (*gRPC.PlugTaskStatus, error) {
|
2024-08-26 13:42:42 +00:00
|
|
|
ctx, span := tracer.Start(ctx, "TaskStart")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
var plugTaskState gRPC.PlugTaskStatus
|
|
|
|
|
2024-10-16 07:47:48 +00:00
|
|
|
id, err := gonanoid.New(25)
|
2024-07-19 13:28:48 +00:00
|
|
|
if err != nil {
|
2024-08-26 13:42:42 +00:00
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
2024-07-19 13:28:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-26 13:42:42 +00:00
|
|
|
span.AddEvent("Generated task ID", trace.WithAttributes(attribute.String("task_id", id)))
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-10-15 14:08:34 +00:00
|
|
|
scrapeTaskHistory := models.ScrapeHistory{
|
|
|
|
ScrapeTaskID: models.ScrapeTaskID(id),
|
|
|
|
UserSourceID: models.UserSourceID(creation.UserSourceId),
|
|
|
|
}
|
|
|
|
scrapeTaskHistory, err = database.CreateScrapeHistory(ctx, scrapeTaskHistory)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
span.AddEvent("Creates ScrapeTaskHistory", trace.WithAttributes(
|
|
|
|
attribute.String("user_source_id", creation.UserSourceId),
|
|
|
|
attribute.String("scrape_task_id", id),
|
|
|
|
))
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
plugTaskState.TaskId = id
|
|
|
|
plugTaskState.TaskState = gRPC.PlugTaskState_RUNNING
|
|
|
|
|
2024-08-17 18:23:11 +00:00
|
|
|
userSource, err := database.GetUserSourceByID(ctx, models.UserSourceID(creation.UserSourceId))
|
|
|
|
if err != nil {
|
2024-08-26 13:42:42 +00:00
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
2024-10-16 08:11:00 +00:00
|
|
|
|
|
|
|
err = database.UpdateScrapeHistory(ctx, models.ScrapeHistory{
|
|
|
|
ScrapeTaskID: models.ScrapeTaskID(id),
|
|
|
|
UserSourceID: userSource.ID,
|
|
|
|
FinishedAt: time.Now(),
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
|
2024-08-17 18:23:11 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-26 13:42:42 +00:00
|
|
|
span.AddEvent("Retrieved user source", trace.WithAttributes(attribute.String("user_source_id", creation.UserSourceId)))
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-27 12:17:31 +00:00
|
|
|
if !userSource.AccountValidate {
|
|
|
|
err = errors.New("user is not validated")
|
|
|
|
|
2024-08-27 12:21:33 +00:00
|
|
|
log.WithContext(ctx).WithError(err).WithField("task_id", id).Error("Task execution failed")
|
2024-08-27 12:17:31 +00:00
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
|
|
|
|
plugTaskState.TaskState = gRPC.PlugTaskState_STOPPED
|
|
|
|
|
2024-10-16 08:11:00 +00:00
|
|
|
err = database.UpdateScrapeHistory(ctx, models.ScrapeHistory{
|
|
|
|
ScrapeTaskID: models.ScrapeTaskID(id),
|
|
|
|
UserSourceID: userSource.ID,
|
|
|
|
FinishedAt: time.Now(),
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
|
2024-08-27 12:17:31 +00:00
|
|
|
return &plugTaskState, err
|
|
|
|
}
|
|
|
|
|
2024-08-27 12:21:33 +00:00
|
|
|
// gRPC closes the context after the call ended. So the whole scrapping stopped without waiting
|
|
|
|
// by using this method we assign a new context to each new request we get.
|
|
|
|
// This can be used for example to close the context with the given id
|
|
|
|
ctx = trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{TraceID: span.SpanContext().TraceID()}))
|
|
|
|
taskCtx, cancel := context.WithCancel(ctx)
|
|
|
|
s.ctx[id] = cancel
|
|
|
|
span.AddEvent("Created new context for task", trace.WithAttributes(attribute.String("task_id", id)))
|
|
|
|
|
2024-08-26 14:38:14 +00:00
|
|
|
log.WithContext(taskCtx).WithFields(log.Fields{
|
2024-08-26 13:42:42 +00:00
|
|
|
"task_id": id,
|
|
|
|
"user_source_id": creation.UserSourceId,
|
2024-09-04 12:53:01 +00:00
|
|
|
}).Debug("Starting task")
|
2024-07-19 13:28:48 +00:00
|
|
|
|
|
|
|
go func() {
|
2024-10-16 08:11:00 +00:00
|
|
|
var err error
|
2024-10-15 14:08:34 +00:00
|
|
|
taskSummery, err := s.taskExecutionFunction(taskCtx, userSource, creation.DeepScrape, creation.ApiKey)
|
2024-07-19 13:28:48 +00:00
|
|
|
if err != nil {
|
2024-08-26 13:42:42 +00:00
|
|
|
log.WithContext(taskCtx).WithError(err).WithField("task_id", id).Error("Task execution failed")
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
} else {
|
2024-09-04 12:53:01 +00:00
|
|
|
log.WithContext(taskCtx).WithField("task_id", id).Debug("Task completed successfully")
|
2024-08-26 13:42:42 +00:00
|
|
|
span.AddEvent("Task completed successfully", trace.WithAttributes(attribute.String("task_id", id)))
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
2024-10-15 14:08:34 +00:00
|
|
|
|
|
|
|
err = database.UpdateScrapeHistory(taskCtx, models.ScrapeHistory{
|
|
|
|
ScrapeTaskID: models.ScrapeTaskID(id),
|
|
|
|
UserSourceID: userSource.ID,
|
|
|
|
FinishedAt: time.Now(),
|
2024-10-16 08:22:35 +00:00
|
|
|
Error: errorString(err),
|
2024-10-15 14:08:34 +00:00
|
|
|
AddedPosts: taskSummery.AddedPosts,
|
|
|
|
DeletedPosts: taskSummery.DeletedPosts,
|
|
|
|
})
|
2024-10-16 08:22:35 +00:00
|
|
|
|
2024-10-15 14:08:34 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithContext(taskCtx).WithError(err).WithField("task_id", id).Error("Task execution failed")
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
|
|
|
s.removeTask(id)
|
2024-07-19 13:28:48 +00:00
|
|
|
}()
|
|
|
|
|
2024-08-26 13:42:42 +00:00
|
|
|
span.SetAttributes(attribute.String("task_id", id))
|
2024-07-19 13:28:48 +00:00
|
|
|
return &plugTaskState, nil
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:42:42 +00:00
|
|
|
func (s *server) TaskStatus(ctx context.Context, task *gRPC.PlugTask) (*gRPC.PlugTaskStatus, error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "TaskStatus")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
var plugTaskState gRPC.PlugTaskStatus
|
|
|
|
|
|
|
|
_, found := s.ctx[task.TaskId]
|
|
|
|
plugTaskState.TaskId = task.TaskId
|
|
|
|
|
|
|
|
plugTaskState.TaskState = gRPC.PlugTaskState_RUNNING
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
plugTaskState.TaskState = gRPC.PlugTaskState_UNKNOWN
|
|
|
|
}
|
2024-08-26 13:42:42 +00:00
|
|
|
span.AddEvent("Determined task state", trace.WithAttributes(attribute.String("task_id", task.TaskId), attribute.String("task_state", plugTaskState.TaskState.String())))
|
|
|
|
|
|
|
|
log.WithContext(ctx).WithFields(log.Fields{
|
|
|
|
"task_id": task.TaskId,
|
|
|
|
"task_state": plugTaskState.TaskState,
|
2024-09-04 12:53:01 +00:00
|
|
|
}).Debug("Task status requested")
|
2024-08-26 13:42:42 +00:00
|
|
|
|
|
|
|
span.SetAttributes(attribute.String("task_id", task.TaskId))
|
2024-07-19 13:28:48 +00:00
|
|
|
return &plugTaskState, nil
|
|
|
|
}
|
|
|
|
|
2024-08-26 13:42:42 +00:00
|
|
|
func (s *server) TaskCancel(ctx context.Context, task *gRPC.PlugTask) (*gRPC.PlugTaskStatus, error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "TaskCancel")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
var plugTaskState gRPC.PlugTaskStatus
|
|
|
|
|
|
|
|
plugTaskState.TaskState = gRPC.PlugTaskState_STOPPED
|
|
|
|
plugTaskState.TaskId = task.TaskId
|
|
|
|
|
|
|
|
s.removeTask(task.TaskId)
|
2024-08-26 13:42:42 +00:00
|
|
|
span.AddEvent("Removed task", trace.WithAttributes(attribute.String("task_id", task.TaskId)))
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-26 13:42:42 +00:00
|
|
|
log.WithContext(ctx).WithFields(log.Fields{
|
|
|
|
"task_id": task.TaskId,
|
|
|
|
"task_state": plugTaskState.TaskState,
|
2024-09-04 12:53:01 +00:00
|
|
|
}).Debug("Task cancellation requested")
|
2024-08-26 13:42:42 +00:00
|
|
|
|
|
|
|
span.SetAttributes(attribute.String("task_id", task.TaskId))
|
2024-07-19 13:28:48 +00:00
|
|
|
return &plugTaskState, nil
|
|
|
|
}
|
|
|
|
|
2024-09-04 11:41:29 +00:00
|
|
|
func (s *server) GetUserMessages(ctx context.Context, message *gRPC.GetMessagesRequest) (*gRPC.GetMessagesResponse, error) {
|
2024-09-04 12:01:04 +00:00
|
|
|
ctx, span := tracer.Start(ctx, "GetUserMessages")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-09-04 12:17:00 +00:00
|
|
|
userSourceID := models.UserSourceID(message.UserSourceId)
|
|
|
|
|
|
|
|
userSource, err := database.GetUserSourceByID(ctx, userSourceID)
|
2024-09-04 12:53:01 +00:00
|
|
|
|
2024-09-04 12:17:00 +00:00
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
2024-09-04 12:56:27 +00:00
|
|
|
log.WithContext(ctx).WithError(err).Error("Getting userSource")
|
2024-09-04 12:17:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-04 12:01:04 +00:00
|
|
|
|
|
|
|
messages, err := s.getMessageExecution(ctx, userSource)
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
2024-09-04 12:56:27 +00:00
|
|
|
log.WithContext(ctx).WithError(err).Error("Execution function")
|
2024-09-04 12:01:04 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response gRPC.GetMessagesResponse
|
|
|
|
for _, message := range messages {
|
|
|
|
response.Messages = append(response.Messages, &gRPC.Message{
|
|
|
|
FromUserSourceId: string(userSource.ID),
|
|
|
|
CreatedAt: message.CreatedAt,
|
|
|
|
Body: message.Body,
|
|
|
|
Title: message.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-04 12:17:00 +00:00
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("user_source_id", string(userSource.ID)),
|
|
|
|
attribute.String("user_id", string(userSource.UserID)),
|
|
|
|
attribute.String("source_id", string(userSource.SourceID)),
|
|
|
|
)
|
|
|
|
|
2024-09-04 12:53:01 +00:00
|
|
|
fields := log.Fields{
|
|
|
|
"user_source_id": userSource.ID,
|
|
|
|
"user_id": userSource.UserID,
|
|
|
|
"source_id": userSource.SourceID,
|
|
|
|
"len_messages": len(messages),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithContext(ctx).WithFields(fields).Debug("Got User messages")
|
|
|
|
|
2024-09-04 12:01:04 +00:00
|
|
|
return &response, err
|
2024-09-04 11:41:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) SendMessage(ctx context.Context, message *gRPC.SendMessageRequest) (*gRPC.SendMessageResponse, error) {
|
2024-09-04 12:01:04 +00:00
|
|
|
ctx, span := tracer.Start(ctx, "SendMessage")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
response := &gRPC.SendMessageResponse{
|
|
|
|
Success: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceID := models.UserSourceID(message.UserSourceId)
|
|
|
|
userSource := models.UserSource{BaseModel: models.BaseModel[models.UserSourceID]{ID: sourceID}}
|
|
|
|
|
|
|
|
err := s.sendMessageExecution(ctx, userSource, message.Message)
|
|
|
|
if err != nil {
|
2024-09-04 12:56:27 +00:00
|
|
|
log.WithContext(ctx).WithError(err).Error("Sending message execution")
|
2024-09-04 12:01:04 +00:00
|
|
|
span.RecordError(err)
|
|
|
|
span.SetStatus(codes.Error, err.Error())
|
|
|
|
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Success = true
|
|
|
|
return response, err
|
2024-09-04 11:41:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Ping(ctx context.Context, ping *gRPC.PingRequest) (*gRPC.PongResponse, error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "Ping")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
var pong gRPC.PongResponse
|
|
|
|
pong.Message = ping.Message
|
|
|
|
pong.Timestamp = ping.Timestamp
|
|
|
|
|
2024-09-04 12:53:01 +00:00
|
|
|
fields := log.Fields{
|
|
|
|
"messsage": ping.Message,
|
|
|
|
"timestamp": ping.Timestamp,
|
|
|
|
}
|
2024-09-04 12:56:27 +00:00
|
|
|
log.WithContext(ctx).WithFields(fields).Trace("Got pinged")
|
2024-09-04 12:53:01 +00:00
|
|
|
|
2024-09-04 11:41:29 +00:00
|
|
|
return &pong, nil
|
|
|
|
}
|
2024-10-15 14:08:34 +00:00
|
|
|
|
|
|
|
func (s *server) removeTask(taskID string) {
|
|
|
|
fn, exists := s.ctx[taskID]
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fn()
|
|
|
|
delete(s.ctx, taskID)
|
|
|
|
}
|
2024-10-16 08:22:35 +00:00
|
|
|
|
|
|
|
func errorString(err error) string {
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|