Skip to content

Commit

Permalink
better signature
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza committed Jan 3, 2025
1 parent 208102b commit ae8ea2c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
27 changes: 26 additions & 1 deletion cmd/facts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package cmd

import (
"context"
"os"
"os/signal"
"syscall"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -108,7 +113,27 @@ func gather(cmd *cobra.Command, _ []string) {
},
}

value, err := g.Gather(cmd.Context(), factRequest)
ctx, cancel := context.WithCancel(cmd.Context())

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
cancelled := false
go func() {
<-signals
log.Info("Caught signal!")
cancelled = true
cancel()

}()

value, err := g.Gather(ctx, factRequest)

if cancelled {
gatherers.CleanupPlugins()
log.Info("Gathering cancelled")
return
}

if err != nil {
log.Errorf("Error gathering fact \"%s\" with argument \"%s\"", gatherer, argument)
cleanupAndFatal(err)
Expand Down
5 changes: 3 additions & 2 deletions internal/factsengine/gatherers/rpc_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package gatherers

import (
"context"
"os"
"os/exec"

"github.com/hashicorp/go-hclog"
"github.com/pkg/errors"

goplugin "github.com/hashicorp/go-plugin"
Expand Down Expand Up @@ -33,7 +33,8 @@ func (l *RPCPluginLoader) Load(pluginPath string) (FactGatherer, error) {
AllowedProtocols: []goplugin.Protocol{
goplugin.ProtocolNetRPC,
},
Logger: hclog.Default(),
SyncStdout: os.Stdout,
SyncStderr: os.Stderr,
})

rpcClient, err := client.Client()
Expand Down
29 changes: 16 additions & 13 deletions pkg/factsengine/plugininterface/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"net/rpc"

log "github.com/sirupsen/logrus"

"github.com/google/uuid"
"github.com/trento-project/agent/pkg/factsengine/entities"
)
Expand All @@ -14,23 +16,22 @@ func (g *GathererRPC) RequestGathering(ctx context.Context, factsRequest []entit
var resp []entities.Fact
var err error

requestId := uuid.New().String()
requestID := uuid.New().String()
args := GatheringArgs{
Facts: factsRequest,
RequestId: requestId,
RequestID: requestID,
}

gathering := make(chan error)
defer close(gathering)

go func() {
gathering <- g.client.Call("Plugin.ServeGathering", args, &resp)
}()

select {
case <-ctx.Done():
err = g.client.Call("Plugin.Cancel", requestId, nil)
return nil, err
err = g.client.Call("Plugin.Cancel", requestID, &resp)
return []entities.Fact{}, err
case err = <-gathering:
if err != nil {
return nil, err
Expand All @@ -46,28 +47,30 @@ type GathererRPCServer struct {

type GatheringArgs struct {
Facts []entities.FactRequest
RequestId string
RequestID string
}

func (s *GathererRPCServer) ServeGathering(args GatheringArgs, resp *[]entities.Fact) error {
var err error
func (s *GathererRPCServer) ServeGathering(args GatheringArgs, resp *[]entities.Fact) (err error) {

Check failure on line 53 in pkg/factsengine/plugininterface/rpc.go

View workflow job for this annotation

GitHub Actions / static-analysis

named return "err" with type "error" found (nonamedreturns)

ctx, cancel := context.WithCancel(context.Background())
if s.cancelMap == nil {
s.cancelMap = make(map[string]context.CancelFunc)
}
s.cancelMap[args.RequestId] = cancel
defer delete(s.cancelMap, args.RequestId)
s.cancelMap[args.RequestID] = cancel
defer delete(s.cancelMap, args.RequestID)

*resp, err = s.Impl.Gather(ctx, args.Facts)
return err
}

func (s *GathererRPCServer) Cancel(requestId string) error {
cancel, ok := s.cancelMap[requestId]
func (s *GathererRPCServer) Cancel(requestID string, _ *[]entities.Fact) (_ error) {
cancel, ok := s.cancelMap[requestID]
if ok {
cancel()
delete(s.cancelMap, requestId)
delete(s.cancelMap, requestID)
} else {
log.Warnf("Cannot find cancel function for request %s", requestID)
}

return nil
}

0 comments on commit ae8ea2c

Please sign in to comment.