Skip to content

Commit 6a1d18d

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 7143587 commit 6a1d18d

File tree

10 files changed

+152
-327
lines changed

10 files changed

+152
-327
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1616
- `tt tcm start` OR `tt tcm start --path /path/to/tcm`: added the capability to run TCM in interactive mode.
1717
- `tt tcm start --watchdog`: implemented Watchdog mode for automatic restarting of TCM upon unexpected termination.
1818
- `tt tcm status`: added command to check TCM runtime status (modes: `watchdog` or `interactive`).
19+
- `tt tcm stop`: add command for graceful termination of TCM processes (modes: `watchdog` or `interactive`).
1920

2021
### Changed
2122

cli/cmd/tcm.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ 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

@@ -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

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ func ExistsAndRecord(pidFileName string) (bool, error) {
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

cli/tcm/tcm.go

+6-2
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

-158
This file was deleted.

cli/tcm/watchdog_test.go

-69
This file was deleted.

lib/watchdog/watchdog.go

+19-9
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ import (
1616
)
1717

1818
type Watchdog struct {
19-
cmd *exec.Cmd
19+
// Command for launching and managing the child process
20+
cmd *exec.Cmd
21+
// Timeout before restarting a failed process
2022
restartTimeout time.Duration
21-
shouldStop atomic.Bool
22-
doneBarrier sync.WaitGroup
23-
pidFile string
24-
wdPidFile string
25-
26-
cmdMutex sync.Mutex
27-
pidFileMutex sync.Mutex
28-
signalChan chan os.Signal
23+
// Atomic flag for graceful shutdown
24+
shouldStop atomic.Bool
25+
// Waits for all watchdog goroutines to complete
26+
doneBarrier sync.WaitGroup
27+
// File to store the child process PID
28+
pidFile string
29+
// File to store the watchdog's own PID
30+
wdPidFile string
31+
// Protects access to cmd (concurrent Start/Stop operations)
32+
cmdMutex sync.Mutex
33+
// Protects PID file read/write operations
34+
pidFileMutex sync.Mutex
35+
// Channel for receiving system signals (SIGTERM etc.)
36+
signalChan chan os.Signal
37+
// Process Group ID (PGID) for proper process termination
2938
processGroupPID atomic.Int32
39+
// Closed when process successfully starts (synchronization)
3040
startupComplete chan struct{}
3141
}
3242

0 commit comments

Comments
 (0)