|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + |
| 14 | + "neo-code/internal/runner" |
| 15 | +) |
| 16 | + |
| 17 | +var runRunnerCommandFn = defaultRunRunner |
| 18 | + |
| 19 | +type runnerCommandOptions struct { |
| 20 | + GatewayAddress string |
| 21 | + TokenFile string |
| 22 | + RunnerID string |
| 23 | + RunnerName string |
| 24 | + Workdir string |
| 25 | +} |
| 26 | + |
| 27 | +func newRunnerCommand() *cobra.Command { |
| 28 | + options := &runnerCommandOptions{} |
| 29 | + cmd := &cobra.Command{ |
| 30 | + Use: "runner", |
| 31 | + Short: "Start local runner daemon for remote task execution", |
| 32 | + SilenceUsage: true, |
| 33 | + Args: cobra.NoArgs, |
| 34 | + Annotations: map[string]string{ |
| 35 | + commandAnnotationSkipGlobalPreload: "true", |
| 36 | + commandAnnotationSkipSilentUpdateCheck: "true", |
| 37 | + }, |
| 38 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + return runRunnerCommandFn(cmd.Context(), *options) |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + cmd.Flags().StringVar(&options.GatewayAddress, "gateway-address", "", "gateway WebSocket address (e.g. 127.0.0.1:8080)") |
| 44 | + cmd.Flags().StringVar(&options.TokenFile, "token-file", "", "gateway token file path") |
| 45 | + cmd.Flags().StringVar(&options.RunnerID, "runner-id", "", "runner identifier (default: hostname)") |
| 46 | + cmd.Flags().StringVar(&options.RunnerName, "runner-name", "", "human-readable runner name") |
| 47 | + cmd.Flags().StringVar(&options.Workdir, "workdir", "", "runner working directory (default: current dir)") |
| 48 | + |
| 49 | + return cmd |
| 50 | +} |
| 51 | + |
| 52 | +func defaultRunRunner(ctx context.Context, options runnerCommandOptions) error { |
| 53 | + gatewayAddress := strings.TrimSpace(options.GatewayAddress) |
| 54 | + if gatewayAddress == "" { |
| 55 | + gatewayAddress = "127.0.0.1:8080" |
| 56 | + } |
| 57 | + |
| 58 | + workdir := strings.TrimSpace(options.Workdir) |
| 59 | + if workdir == "" { |
| 60 | + if wd, err := os.Getwd(); err == nil { |
| 61 | + workdir = wd |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + runnerID := strings.TrimSpace(options.RunnerID) |
| 66 | + if runnerID == "" { |
| 67 | + if hostname, err := os.Hostname(); err == nil { |
| 68 | + runnerID = hostname |
| 69 | + } else { |
| 70 | + runnerID = "local-runner" |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + token := "" |
| 75 | + if options.TokenFile != "" { |
| 76 | + data, err := os.ReadFile(options.TokenFile) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("read token file: %w", err) |
| 79 | + } |
| 80 | + token = strings.TrimSpace(string(data)) |
| 81 | + } |
| 82 | + |
| 83 | + r, err := runner.New(runner.Config{ |
| 84 | + RunnerID: runnerID, |
| 85 | + RunnerName: strings.TrimSpace(options.RunnerName), |
| 86 | + GatewayAddress: gatewayAddress, |
| 87 | + Token: token, |
| 88 | + Workdir: workdir, |
| 89 | + HeartbeatInterval: 10 * time.Second, |
| 90 | + ReconnectBackoffMin: 500 * time.Millisecond, |
| 91 | + ReconnectBackoffMax: 10 * time.Second, |
| 92 | + RequestTimeout: 30 * time.Second, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("create runner: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + runCtx, cancel := context.WithCancel(ctx) |
| 99 | + defer cancel() |
| 100 | + |
| 101 | + sigCh := make(chan os.Signal, 1) |
| 102 | + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 103 | + go func() { |
| 104 | + <-sigCh |
| 105 | + fmt.Fprintln(os.Stderr, "\nshutting down runner...") |
| 106 | + r.Stop() |
| 107 | + cancel() |
| 108 | + }() |
| 109 | + |
| 110 | + fmt.Fprintf(os.Stderr, "runner %s connecting to %s...\n", runnerID, gatewayAddress) |
| 111 | + if err := r.Run(runCtx); err != nil && err != context.Canceled { |
| 112 | + return fmt.Errorf("runner: %w", err) |
| 113 | + } |
| 114 | + return nil |
| 115 | +} |
0 commit comments