startAgent starts the keploy agent process and handles its lifecycle
(ctx context.Context, isDockerCmd bool, opts models.SetupOptions)
| 817 | |
| 818 | // startAgent starts the keploy agent process and handles its lifecycle |
| 819 | func (a *AgentClient) startAgent(ctx context.Context, isDockerCmd bool, opts models.SetupOptions) error { |
| 820 | // Get the errgroup from context |
| 821 | grp, ok := ctx.Value(models.ErrGroupKey).(*errgroup.Group) |
| 822 | if !ok { |
| 823 | return fmt.Errorf("failed to get errorgroup from the context") |
| 824 | } |
| 825 | |
| 826 | // Create a context for the agent that can be cancelled independently |
| 827 | agentCtx, cancel := context.WithCancel(ctx) |
| 828 | a.agentCancel = cancel |
| 829 | if a.conf.Record.Synchronous { |
| 830 | opts.Synchronous = true |
| 831 | } |
| 832 | opts.ExtraArgs = agent.StartupAgentHook.GetArgs(ctx) |
| 833 | if isDockerCmd { |
| 834 | // Helper check to ensure the binary running inside docker has the required capabilities |
| 835 | if err := utils.CheckRequiredPermissions(); err != nil { |
| 836 | a.logger.Error("Failed to start Keploy Agent", zap.Error(err)) |
| 837 | return err |
| 838 | } |
| 839 | // Start the agent in Docker container using errgroup |
| 840 | grp.Go(func() error { |
| 841 | defer cancel() // Cancel agent context when Docker agent stops |
| 842 | if err := a.startInDocker(agentCtx, a.logger, opts); err != nil && !errors.Is(agentCtx.Err(), context.Canceled) { |
| 843 | a.logger.Error("failed to start Docker agent", zap.Error(err)) |
| 844 | return err |
| 845 | } |
| 846 | return nil |
| 847 | }) |
| 848 | } else { |
| 849 | // Start the agent as a native process |
| 850 | err := a.startNativeAgent(agentCtx, opts) |
| 851 | if err != nil { |
| 852 | cancel() |
| 853 | return err |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // Monitor agent process and cancel client context if agent stops using errgroup |
| 858 | grp.Go(func() error { |
| 859 | a.monitorAgent(ctx, agentCtx) |
| 860 | return nil |
| 861 | }) |
| 862 | |
| 863 | return nil |
| 864 | } |
| 865 | |
| 866 | // startNativeAgent starts the keploy agent as a native process |
| 867 | func (a *AgentClient) startNativeAgent(ctx context.Context, opts models.SetupOptions) error { |
no test coverage detected