提交 5569817c 编写于 作者: P Priya Wadhwa

Address code review comments

上级 291e5b69
...@@ -17,7 +17,6 @@ limitations under the License. ...@@ -17,7 +17,6 @@ limitations under the License.
package cmd package cmd
import ( import (
"log"
"os" "os"
"runtime" "runtime"
"time" "time"
...@@ -97,9 +96,7 @@ func runStop(cmd *cobra.Command, args []string) { ...@@ -97,9 +96,7 @@ func runStop(cmd *cobra.Command, args []string) {
} }
// Kill any existing scheduled stops // Kill any existing scheduled stops
if err := schedule.KillExisting(profilesToStop); err != nil { schedule.KillExisting(profilesToStop)
log.Printf("error killing existing scheduled stops: %v", err)
}
if scheduledStopDuration != 0 { if scheduledStopDuration != 0 {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
......
...@@ -31,30 +31,41 @@ import ( ...@@ -31,30 +31,41 @@ import (
"k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/localpath"
) )
// KillExisting kills existing scheduled stops // KillExisting kills existing scheduled stops by looking up the PID
func KillExisting(profiles []string) error { // of the scheduled stop from the PID file saved for the profile and killing the process
func KillExisting(profiles []string) {
for _, profile := range profiles { for _, profile := range profiles {
file := localpath.PID(profile) if err := killPIDForProfile(profile); err != nil {
f, err := ioutil.ReadFile(file) klog.Errorf("error killng PID for profile %s: %v", profile, err)
if os.IsNotExist(err) {
return nil
} }
defer os.Remove(file) }
if err != nil { }
return errors.Wrapf(err, "reading %s", file)
} func killPIDForProfile(profile string) error {
pid, err := strconv.Atoi(string(f)) file := localpath.PID(profile)
if err != nil { f, err := ioutil.ReadFile(file)
return errors.Wrapf(err, "converting %v to int", string(f)) if os.IsNotExist(err) {
} return nil
p, err := os.FindProcess(pid) }
if err != nil { defer func() {
return errors.Wrap(err, "finding process") if err := os.Remove(file); err != nil {
} klog.Errorf("error deleting %s: %v, you may have to delete in manually", file, err)
klog.Infof("killing process %v as it is an old scheduled stop", pid)
if err := p.Kill(); err != nil {
return errors.Wrapf(err, "killing %v", pid)
} }
}()
if err != nil {
return errors.Wrapf(err, "reading %s", file)
}
pid, err := strconv.Atoi(string(f))
if err != nil {
return errors.Wrapf(err, "converting %s to int", f)
}
p, err := os.FindProcess(pid)
if err != nil {
return errors.Wrap(err, "finding process")
}
klog.Infof("killing process %v as it is an old scheduled stop", pid)
if err := p.Kill(); err != nil {
return errors.Wrapf(err, "killing %v", pid)
} }
return nil return nil
} }
...@@ -72,7 +83,7 @@ func daemonize(profiles []string, duration time.Duration) error { ...@@ -72,7 +83,7 @@ func daemonize(profiles []string, duration time.Duration) error {
func savePIDs(pid int, profiles []string) error { func savePIDs(pid int, profiles []string) error {
for _, p := range profiles { for _, p := range profiles {
file := localpath.PID(p) file := localpath.PID(p)
if err := ioutil.WriteFile(file, []byte(fmt.Sprintf("%v", pid)), 0644); err != nil { if err := ioutil.WriteFile(file, []byte(fmt.Sprintf("%v", pid)), 0600); err != nil {
return err return err
} }
} }
......
...@@ -21,11 +21,13 @@ package schedule ...@@ -21,11 +21,13 @@ package schedule
import ( import (
"fmt" "fmt"
"time" "time"
"k8s.io/klog/v2"
) )
// KillExisting kills existing scheduled stops // KillExisting will kill existing scheduled stops
func KillExisting(profiles []string) error { func KillExisting(profiles []string) {
return fmt.Errorf("not yet implemented for windows") klog.Errorf("not yet implemented for windows")
} }
func daemonize(profiles []string, duration time.Duration) error { func daemonize(profiles []string, duration time.Duration) error {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册