diff options
Diffstat (limited to 'internal/cli/stop')
-rw-r--r-- | internal/cli/stop/command.go | 53 | ||||
-rw-r--r-- | internal/cli/stop/command_test.go | 27 |
2 files changed, 80 insertions, 0 deletions
diff --git a/internal/cli/stop/command.go b/internal/cli/stop/command.go new file mode 100644 index 00000000..8b4b589f --- /dev/null +++ b/internal/cli/stop/command.go @@ -0,0 +1,53 @@ +package stop + +import ( + "log" + "os" + "strconv" + "syscall" + + "github.com/roadrunner-server/errors" + "github.com/spf13/cobra" +) + +const ( + // sync with root.go + pidFileName string = ".pid" +) + +// NewCommand creates `serve` command. +func NewCommand(silent *bool) *cobra.Command { + return &cobra.Command{ + Use: "stop", + Short: "Stop RoadRunner server", + RunE: func(*cobra.Command, []string) error { + const op = errors.Op("rr_stop") + + data, err := os.ReadFile(pidFileName) + if err != nil { + return errors.Errorf("%v, to create a .pid file, you must run RR with the following options: './rr serve -p'", err) + } + + pid, err := strconv.Atoi(string(data)) + if err != nil { + return errors.E(op, err) + } + + process, err := os.FindProcess(pid) + if err != nil { + return errors.E(op, err) + } + + if !*silent { + log.Printf("stopping process with PID: %d", pid) + } + + err = process.Signal(syscall.SIGTERM) + if err != nil { + return errors.E(op, err) + } + + return nil + }, + } +} diff --git a/internal/cli/stop/command_test.go b/internal/cli/stop/command_test.go new file mode 100644 index 00000000..8bbb29ea --- /dev/null +++ b/internal/cli/stop/command_test.go @@ -0,0 +1,27 @@ +package stop_test + +import ( + "testing" + + "github.com/roadrunner-server/roadrunner/v2/internal/cli/stop" + + "github.com/stretchr/testify/assert" +) + +func TestCommandProperties(t *testing.T) { + cmd := stop.NewCommand(toPtr(false)) + + assert.Equal(t, "stop", cmd.Use) + assert.NotNil(t, cmd.RunE) +} + +func TestCommandTrue(t *testing.T) { + cmd := stop.NewCommand(toPtr(true)) + + assert.Equal(t, "stop", cmd.Use) + assert.NotNil(t, cmd.RunE) +} + +func toPtr[T any](val T) *T { + return &val +} |