Skip to content

Commit

Permalink
Add default timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Savin committed Dec 8, 2021
1 parent 41f1269 commit 05a7280
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/spf13/cobra"
)

const Version string = "v1.0.0"
// Version of release
const Version string = "v0.2.0"

// rootCmd – default root command
// rootCmd show help message
Expand Down
15 changes: 10 additions & 5 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ go-monkill watch --pid 12345 --command "ping jtprog.ru -c 4"
`,
RunE: func(cmd *cobra.Command, args []string) error {
l := zerolog.New(os.Stderr)
l.Level(zerolog.TraceLevel)
return watcher(WatcherConfig.pid, WatcherConfig.command, WatcherConfig.timeout, waiter.Waiter{}, executor.Executor{}, l)
},
}

// Verbose flag
//var Verbose bool
// defaultPid

// defaultPid is -1 for
var defaultPid int = -1

// defaultTimeOut
// defaultTimeOut is 250 milliseconds
var defaultTimeOut int64 = 250

// WatcherConfig provides config for watcher
Expand All @@ -47,16 +49,19 @@ var WatcherConfig struct {
//
// &WatcherConfig pid as PID for monitoring – defined in flag --pid
// &WatcherConfig command as command for running - defined in flag --command
// &WatcherConfig timeout as timeout for check process - defined in flag --timeout
func init() {
rootCmd.AddCommand(watchCmd)
// TODO: Implement verbose log output by flag
//rootCmd.InheritedFlags().BoolVar(&Verbose, "verbose", false, "Enable debug logging")
// TODO: Implement verbose log output by flag --verbose
// rootCmd.InheritedFlags().BoolVar(&Verbose, "verbose", false, "Enable debug logging")
// TODO: Implement output to logfile by flag --logfile
// rootCmd.InheritedFlags().StringVar(&WatcherConfig.logfile, "logfile", "/tmp/go-monkill.log", "Enable debug logging")
watchCmd.PersistentFlags().IntVar(&WatcherConfig.pid, "pid", defaultPid, "PID for watching")
watchCmd.PersistentFlags().StringVar(&WatcherConfig.command, "command", "ping jtprog.ru -c 2", "Command for running")
watchCmd.PersistentFlags().Int64Var(&WatcherConfig.timeout, "timeout", defaultTimeOut, "Set timeout for check status of process")
}

// Waiter interface
// Waiter interface for monitor process PID every timeout milliseconds
type Waiter interface {
Wait(pid int, timeout int64) (<-chan struct{}, error)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Executor struct{}

// Exec - running Executor with command (i.e.: "ping jtprog.ru -c 4" )
// TODO: Implement verbose logging for output
func (e Executor) Exec(command string) error {
cmds := strings.Split(command, " ")
if len(cmds) == 0 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
// Waiter struct
type Waiter struct{}

// Wait find process with defined PID and wait for process will finish or be killed
// Checking the liveliness of the process occurs with a timeout delay
// The timeout is set in milliseconds
// Wait find process with defined PID and wait for process will finish or be killed.
// Checking the liveliness of the process occurs with a timeout delay.
// The timeout is set in milliseconds.
func (w Waiter) Wait(pid int, timeout int64) (<-chan struct{}, error) {
_, err := ps.FindProcess(pid)
if err != nil {
Expand Down

0 comments on commit 05a7280

Please sign in to comment.