Skip to content

Commit 5482fb7

Browse files
Anoop GopalakrishnanJoe Kirwin
authored andcommitted
Calling start on running container doesn't restart
[#150536302] Signed-off-by: Joe Kirwin <jkirwin@pivotal.io>
1 parent daaf2c7 commit 5482fb7

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

src/bpm/commands/start.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package commands
1717

1818
import (
1919
"bpm/config"
20+
"bpm/runc/lifecycle"
2021
"fmt"
2122
"path/filepath"
2223

@@ -74,23 +75,34 @@ func start(cmd *cobra.Command, _ []string) error {
7475
}
7576

7677
runcLifecycle := newRuncLifecycle()
78+
state := getContainerState(runcLifecycle, bpmCfg.JobName())
7779

78-
err = runcLifecycle.StartJob(bpmCfg, procCfg)
79-
if err != nil {
80+
switch state {
81+
case lifecycle.ContainerStateRunning:
82+
logger.Info(fmt.Sprintf("container %s is already running", bpmCfg.JobName()))
83+
return nil
84+
case lifecycle.ContainerStateStopped:
8085
removeErr := runcLifecycle.RemoveJob(bpmCfg)
8186
if removeErr != nil {
8287
logger.Error("failed-to-cleanup", removeErr)
8388
return removeErr
8489
}
90+
return runcLifecycle.StartJob(bpmCfg, procCfg)
91+
default:
92+
return runcLifecycle.StartJob(bpmCfg, procCfg)
93+
}
94+
}
8595

86-
// Case of pre-existing stopped container
87-
// with same name that needed to be cleaned up
88-
err = runcLifecycle.StartJob(bpmCfg, procCfg)
96+
func getContainerState(runcLifecycle *lifecycle.RuncLifecycle, jobName string) string {
97+
jobs, err := runcLifecycle.ListJobs()
98+
if err != nil {
99+
logger.Error("failed-list-jobs", err)
100+
}
89101

90-
if err != nil {
91-
return err
102+
for _, job := range jobs {
103+
if job.Name == jobName {
104+
return job.Status
92105
}
93106
}
94-
95-
return nil
107+
return ""
96108
}

src/bpm/main_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,64 @@ var _ = Describe("bpm", func() {
613613
})
614614
})
615615

616+
Context("when a running container exist with the same name", func() {
617+
startContainer := func() *exec.Cmd {
618+
cfg.Executable = "/bin/bash"
619+
cfg.Args = []string{
620+
"-c",
621+
"sleep 10000",
622+
}
623+
624+
limit := int64(1000)
625+
cfg.Limits = &config.Limits{
626+
Processes: &limit,
627+
}
628+
629+
cfgPath = writeConfig(jobName, jobName, cfg)
630+
631+
start := exec.Command(bpmPath, "start", jobName)
632+
start.Env = append(start.Env, fmt.Sprintf("BPM_BOSH_ROOT=%s", boshConfigPath))
633+
return start
634+
}
635+
636+
// Currently assumes only one thing is running #NAIVE
637+
getContainerPid := func() int {
638+
command = exec.Command(bpmPath, "list")
639+
command.Env = append(command.Env, fmt.Sprintf("BPM_BOSH_ROOT=%s", boshConfigPath))
640+
session, err := gexec.Start(command, GinkgoWriter, GinkgoWriter)
641+
Expect(err).ShouldNot(HaveOccurred())
642+
Eventually(session).Should(gexec.Exit(0))
643+
Expect(session.Out).Should(gbytes.Say("running"))
644+
645+
re := regexp.MustCompile("\\s(\\d+)\\s")
646+
pids := re.FindSubmatch(session.Out.Contents())
647+
Expect(pids).ShouldNot(BeNil())
648+
Expect(len(pids)).Should(Equal(2))
649+
650+
pid, err := strconv.Atoi(string(pids[1]))
651+
Expect(err).NotTo(HaveOccurred())
652+
return pid
653+
}
654+
655+
BeforeEach(func() {
656+
start := startContainer()
657+
session, err := gexec.Start(start, GinkgoWriter, GinkgoWriter)
658+
Expect(err).ShouldNot(HaveOccurred())
659+
Eventually(session).Should(gexec.Exit(0))
660+
})
661+
662+
It("should not restart the container and logs", func() {
663+
origPid := getContainerPid()
664+
start := startContainer()
665+
session, err := gexec.Start(start, GinkgoWriter, GinkgoWriter)
666+
Expect(err).ShouldNot(HaveOccurred())
667+
Eventually(session).Should(gexec.Exit(0))
668+
Expect(session.Out).Should(gbytes.Say(fmt.Sprintf("container %s is already running", jobName)))
669+
newPid := getContainerPid()
670+
Expect(newPid).To(Equal(origPid))
671+
})
672+
})
673+
616674
Context("when a stopped container exists with the same name", func() {
617675
BeforeEach(func() {
618676
cfg.Executable = "/bin/bash"

src/bpm/runc/lifecycle/lifecycle.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import (
3636
const (
3737
ContainerSigQuitGracePeriod = 5 * time.Second
3838
ContainerStatePollInterval = 1 * time.Second
39+
ContainerStateRunning = "running"
40+
ContainerStatePaused = "paused"
41+
ContainerStateStopped = "stopped"
3942
)
4043

4144
var TimeoutError = errors.New("failed to stop job within timeout")

0 commit comments

Comments
 (0)