Skip to content

Commit 72c6d0e

Browse files
tcm: add tt tcm stop command
@TarantoolBot document Title: add the tt tcm stop command. Add command tt tcm stop to gracefully terminate TCM: - Works in both modes(watchdog and interactive) - Stops all subprocesses Closes #TNTP-1993
1 parent 25cc926 commit 72c6d0e

File tree

11 files changed

+309
-465
lines changed

11 files changed

+309
-465
lines changed

CHANGELOG.md

Lines changed: 97 additions & 96 deletions
Large diffs are not rendered by default.

cli/cmd/tcm.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ import (
1313
"github.com/jedib0t/go-pretty/v6/text"
1414
"github.com/spf13/cobra"
1515
"github.com/tarantool/tt/cli/cmdcontext"
16-
"github.com/tarantool/tt/cli/modules"
1716
"github.com/tarantool/tt/cli/process_utils"
1817
tcmCmd "github.com/tarantool/tt/cli/tcm"
19-
"github.com/tarantool/tt/cli/util"
2018
libwatchdog "github.com/tarantool/tt/lib/watchdog"
2119
)
2220

2321
var tcmCtx = tcmCmd.TcmCtx{}
2422

2523
const (
26-
tcmPidFile = "tcmPidFile.pid"
27-
watchdogPidFile = "watchdogPidFile.pid"
24+
tcmPidFile = "tcm.pid"
25+
watchdogPidFile = "watchdog.pid"
2826
)
2927

3028
func newTcmStartCmd() *cobra.Command {
@@ -34,11 +32,7 @@ func newTcmStartCmd() *cobra.Command {
3432
Long: `Start to the tcm.
3533
tt tcm start --watchdog
3634
tt tcm start --path`,
37-
Run: func(cmd *cobra.Command, args []string) {
38-
cmdCtx.CommandName = cmd.Name()
39-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalStartTcm, args)
40-
util.HandleCmdErr(cmd, err)
41-
},
35+
Run: RunModuleFunc(internalStartTcm),
4236
}
4337
tcmCmd.Flags().StringVar(&tcmCtx.Executable, "path", "", "the path to the tcm binary file")
4438
tcmCmd.Flags().BoolVar(&tcmCtx.Watchdog, "watchdog", false, "enables the watchdog")
@@ -52,11 +46,7 @@ func newTcmStatusCmd() *cobra.Command {
5246
Short: "Status tcm application",
5347
Long: `Status to the tcm.
5448
tt tcm status`,
55-
Run: func(cmd *cobra.Command, args []string) {
56-
cmdCtx.CommandName = cmd.Name()
57-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalTcmStatus, args)
58-
util.HandleCmdErr(cmd, err)
59-
},
49+
Run: RunModuleFunc(internalTcmStatus),
6050
}
6151
return tcmCmd
6252
}
@@ -66,11 +56,7 @@ func newTcmStopCmd() *cobra.Command {
6656
Use: "stop",
6757
Short: "Stop tcm application",
6858
Long: `Stop to the tcm. tt tcm stop`,
69-
Run: func(cmd *cobra.Command, args []string) {
70-
cmdCtx.CommandName = cmd.Name()
71-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalTcmStop, args)
72-
util.HandleCmdErr(cmd, err)
73-
},
59+
Run: RunModuleFunc(internalTcmStop),
7460
}
7561
return tcmCmd
7662
}
@@ -178,13 +164,13 @@ func internalTcmStop(cmdCtx *cmdcontext.CmdCtx, args []string) error {
178164
if err != nil {
179165
return err
180166
}
181-
log.Println("Watchdog and TCM stoped")
167+
log.Println("Watchdog and TCM stopped")
182168
} else {
183169
_, err := process_utils.StopProcess(tcmPidFile)
184170
if err != nil {
185171
return err
186172
}
187-
log.Println("TCM stoped")
173+
log.Println("TCM stopped")
188174
}
189175

190176
return nil

cli/process_utils/process_utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ func ExistsAndRecord(pidFileName string) (bool, error) {
112112
// The PID file already exists. We have to check if the process is alive.
113113
pid, err := GetPIDFromFile(pidFileName)
114114
if err != nil {
115-
return false, fmt.Errorf(`pID file exists, but PID can't be read. Error: "%v"`, err)
115+
return false, fmt.Errorf(`PID file exists, but PID can't be read. Error: "%v"`, err)
116116
}
117117
if res, _ := IsProcessAlive(pid); res {
118118
return true, nil
119119
}
120120
} else if !os.IsNotExist(err) {
121-
return false, fmt.Errorf(`something went wrong while trying to read the PID file. Error: "%v"`,
122-
err)
121+
return false, fmt.Errorf(`something went wrong while trying to read the`+
122+
`PID file. Error: "%v"`, err)
123123
}
124124

125125
return false, nil
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package process_utils
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func Test_ExistsAndRecord(t *testing.T) {
12+
testFile := "test.pid"
13+
invalid := "invalid.pid"
14+
cmd := exec.Command("sleep", "10")
15+
16+
t.Cleanup(func() {
17+
os.Remove(testFile)
18+
})
19+
20+
err := cmd.Start()
21+
require.NoError(t, err)
22+
23+
err = CreatePIDFile(testFile, cmd.Process.Pid)
24+
require.NoError(t, err)
25+
26+
status, err := ExistsAndRecord(testFile)
27+
require.NoError(t, err)
28+
require.True(t, status)
29+
30+
err = cmd.Process.Kill()
31+
require.NoError(t, err)
32+
33+
statusInvalid, err := ExistsAndRecord(invalid)
34+
require.False(t, statusInvalid)
35+
require.NoError(t, err)
36+
}

cli/tcm/tcm.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package tcm
22

3+
// TcmCtx holds parameters and state for managing the TCM process and its watchdog.
34
type TcmCtx struct {
5+
// Path to the TCM executable file.
46
Executable string
7+
// Path to the file storing the TCM process PID.
58
TcmPidFile string
6-
7-
Watchdog bool
9+
// Flag indicating whether the watchdog is enabled.
10+
Watchdog bool
11+
// Path to the file storing the watchdog process PID.
812
WathdogPidFile string
913
}

cli/tcm/watchdog.go

Lines changed: 0 additions & 158 deletions
This file was deleted.

cli/tcm/watchdog_test.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)