From dea570c55bf2d9b9ed5d6eadd27721ccf5fc0380 Mon Sep 17 00:00:00 2001 From: soxx Date: Tue, 20 Feb 2024 21:12:49 +0100 Subject: [PATCH] feat: working implementation of base features --- .gitignore | 3 ++- pkg/plug/grpc.go | 14 ++++++++------ pkg/plug/server.go | 22 +++++++++++++++------- scripts/generate_grpc_files.sh | 0 4 files changed, 25 insertions(+), 14 deletions(-) mode change 100644 => 100755 scripts/generate_grpc_files.sh diff --git a/.gitignore b/.gitignore index b807c2a..148b1c3 100644 --- a/.gitignore +++ b/.gitignore @@ -191,4 +191,5 @@ $RECYCLE.BIN/ /.run/* *.env *pb.go -!**/.gitkeep \ No newline at end of file +!**/.gitkeep +main.go \ No newline at end of file diff --git a/pkg/plug/grpc.go b/pkg/plug/grpc.go index 36d3cfc..178ebfc 100644 --- a/pkg/plug/grpc.go +++ b/pkg/plug/grpc.go @@ -12,14 +12,16 @@ import ( type server struct { gRPC.UnimplementedPlugConnectorServer - ctx map[string]context.CancelFunc - database graph.OtterSpace + ctx map[string]context.CancelFunc + database graph.OtterSpace + taskExecutionFunction TaskExecution } -func NewGrpcServer(database graph.OtterSpace) gRPC.PlugConnectorServer { +func NewGrpcServer(database graph.OtterSpace, taskExecutionFunction TaskExecution) gRPC.PlugConnectorServer { return &server{ - ctx: make(map[string]context.CancelFunc), - database: database, + ctx: make(map[string]context.CancelFunc), + database: database, + taskExecutionFunction: taskExecutionFunction, } } @@ -46,7 +48,7 @@ func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation) go func() { // FIXME: better implement this methode, works for now but needs refactoring - err := service.ScrapeUser(ctx, s.database, &s.e621Client, creation.UserSourceName, &anthroveUser, creation.DeepScrape, func() { + err := s.taskExecutionFunction(ctx, s.database, creation.UserSourceName, anthroveUser, creation.DeepScrape, func() { s.removeTask(id) }) if err != nil { diff --git a/pkg/plug/server.go b/pkg/plug/server.go index 4a31cc6..d78af9f 100644 --- a/pkg/plug/server.go +++ b/pkg/plug/server.go @@ -3,6 +3,7 @@ package plug import ( "context" "fmt" + "git.dragse.it/anthrove/otter-space-sdk/pkg/models" "net" "git.dragse.it/anthrove/otter-space-sdk/pkg/graph" @@ -10,11 +11,14 @@ import ( "google.golang.org/grpc" ) +type TaskExecution func(ctx context.Context, graph graph.OtterSpace, sourceUsername string, anthroveUser models.AnthroveUser, deepScrape bool, cancelFunction func()) error + type Server struct { - ctx context.Context - address string - port string - database graph.OtterSpace + address string + port string + ctx context.Context + database graph.OtterSpace + taskExecutionFunction TaskExecution } func NewServer(ctx context.Context, address string, port string) Server { @@ -25,7 +29,7 @@ func NewServer(ctx context.Context, address string, port string) Server { } } -func (s Server) Listen() error { +func (s *Server) Listen() error { var err error lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", s.address, s.port)) @@ -35,7 +39,7 @@ func (s Server) Listen() error { grpcServer := grpc.NewServer() - pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(s.database)) + pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(s.database, s.taskExecutionFunction)) err = grpcServer.Serve(lis) if err != nil { @@ -45,6 +49,10 @@ func (s Server) Listen() error { return nil } -func (s Server) WithGraphConnection(graph graph.OtterSpace) { +func (s *Server) WithGraphConnection(graph graph.OtterSpace) { s.database = graph } + +func (s *Server) TaskExecutionFunction(function TaskExecution) { + s.taskExecutionFunction = function +} diff --git a/scripts/generate_grpc_files.sh b/scripts/generate_grpc_files.sh old mode 100644 new mode 100755