Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't halt project dev command if the sandbox is already running #471 #473

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/app/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"cli-enonic/internal/app/commands/auditlog"
"cli-enonic/internal/app/commands/cloud"
"cli-enonic/internal/app/commands/cms"
"cli-enonic/internal/app/commands/create"
"cli-enonic/internal/app/commands/dump"
"cli-enonic/internal/app/commands/export"
"cli-enonic/internal/app/commands/project"
Expand All @@ -19,7 +18,8 @@ import (

func All() []cli.Command {
return []cli.Command{
create.Create,
Create,
Dev,
{
Name: "snapshot",
Usage: "Create and restore snapshots",
Expand Down
3 changes: 3 additions & 0 deletions internal/app/commands/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const FORCE_COOKIE = "forceFlag"
const HTTP_PORT = 8080
const INFO_PORT = 2609
const MGMT_PORT = 4848
const MODE_DEV = "dev"
const MODE_DEFAULT = "default"

var spin *spinner.Spinner

Expand Down Expand Up @@ -71,6 +73,7 @@ type ProjectData struct {

type RuntimeData struct {
Running string `toml:"running"`
Mode string `toml:"mode"`
PID int `toml:"PID"`
SessionID string `toml:sessionID`
LatestVersion string `toml:latestVersion`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package create
package commands

import (
"cli-enonic/internal/app/commands/common"
Expand Down
19 changes: 19 additions & 0 deletions internal/app/commands/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package commands

import (
"cli-enonic/internal/app/commands/common"
"cli-enonic/internal/app/commands/project"
"github.com/urfave/cli"
)

var Dev = cli.Command{
Name: "dev",
Usage: "Start current project in dev mode",
Flags: []cli.Flag{common.FORCE_FLAG},
Action: func(c *cli.Context) error {

project.StartDevMode(c)

return nil
},
}
11 changes: 8 additions & 3 deletions internal/app/commands/project/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ var Deploy = cli.Command{
askToRunSandbox(c, projectData)
} else if rData := common.ReadRuntimeData(); rData.Running != "" {
// ask to stop sandbox running in detached mode
sandbox.AskToStopSandbox(rData, force)
if !sandbox.AskToStopSandbox(rData, force) {
os.Exit(1)
}
}
}
}
Expand All @@ -79,18 +81,21 @@ func askToRunSandbox(c *cli.Context, projectData *common.ProjectData) {
debug := c.Bool("debug")
continuous := c.Bool("continuous")

sandboxData := sandbox.ReadSandboxData(projectData.Sandbox)
if !processRunning {
if force || util.PromptBool(fmt.Sprintf("Do you want to start sandbox '%s'", projectData.Sandbox), true) {
// detach in continuous mode to release terminal window
sandbox.StartSandbox(c, sandbox.ReadSandboxData(projectData.Sandbox), continuous, devMode, debug, common.HTTP_PORT)
err, _ := sandbox.StartSandbox(c, sandboxData, continuous, devMode, debug, common.HTTP_PORT)
util.Fatal(err, "")
}

} else if rData.Running != projectData.Sandbox {
// Ask to stop running box if it differs from project selected only
if force || util.PromptBool(fmt.Sprintf("Do you want to stop running sandbox '%s' and start '%s' instead", rData.Running, projectData.Sandbox), true) {
sandbox.StopSandbox(rData)
// detach in continuous mode to release terminal window
sandbox.StartSandbox(c, sandbox.ReadSandboxData(projectData.Sandbox), continuous, devMode, debug, common.HTTP_PORT)
err, _ := sandbox.StartSandbox(c, sandboxData, continuous, devMode, debug, common.HTTP_PORT)
util.Fatal(err, "")
}

} else {
Expand Down
37 changes: 24 additions & 13 deletions internal/app/commands/project/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,34 @@ var Dev = cli.Command{
Flags: []cli.Flag{common.FORCE_FLAG},
Action: func(c *cli.Context) error {

if projectData := ensureProjectDataExists(c, ".", "", "A sandbox is required to run the project in dev mode, do you want to create one"); projectData != nil {
StartDevMode(c)

sbox := sandbox.ReadSandboxFromProjectOrAsk(c, false)
return nil
},
}

sandbox.StartSandbox(c, sbox, true, true, true, common.HTTP_PORT)
func StartDevMode(c *cli.Context) {
if projectData := ensureProjectDataExists(c, ".", "", "A sandbox is required to run the project in dev mode, do you want to create one"); projectData != nil {

devMessage := fmt.Sprintln("\nRunning project in dev mode...")
util.ListenForInterrupt(func() {
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Stopping project dev mode...")
fmt.Fprintln(os.Stderr)
sandbox.StopSandbox(common.ReadRuntimeData())
})
sbox := sandbox.ReadSandboxFromProjectOrAsk(c, false)

runGradleTask(projectData, devMessage, "dev")
err, usedExistingSandbox := sandbox.StartSandbox(c, sbox, true, true, true, common.HTTP_PORT)
if err != nil {
fmt.Fprintf(os.Stderr, "Restart sandbox '%s' in dev mode or stop it before running dev command\n", sbox.Name)
os.Exit(1)
}

return nil
},
devMessage := fmt.Sprintln("\nRunning project in dev mode...")
util.ListenForInterrupt(func() {
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Stopping project dev mode...")
fmt.Fprintln(os.Stderr)
if !usedExistingSandbox {
// we started the sandbox, so we need to stop it too
sandbox.StopSandbox(common.ReadRuntimeData())
}
})

runGradleTask(projectData, devMessage, "dev")
}
}
4 changes: 3 additions & 1 deletion internal/app/commands/sandbox/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ var Delete = cli.Command{
}

if rData := common.ReadRuntimeData(); rData.Running == sandbox.Name {
AskToStopSandbox(rData, force)
if !AskToStopSandbox(rData, force) {
os.Exit(1)
}
}

boxes := getSandboxesUsingDistro(sandbox.Distro)
Expand Down
37 changes: 22 additions & 15 deletions internal/app/commands/sandbox/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sandbox
import (
"cli-enonic/internal/app/commands/common"
"cli-enonic/internal/app/util"
"errors"
"fmt"
"github.com/urfave/cli"
"os"
Expand Down Expand Up @@ -36,7 +37,8 @@ var Start = cli.Command{

sandbox := ReadSandboxFromProjectOrAsk(c, true)

StartSandbox(c, sandbox, c.Bool("detach"), c.Bool("dev"), c.Bool("debug"), uint16(c.Uint("http.port")))
err, _ := StartSandbox(c, sandbox, c.Bool("detach"), c.Bool("dev"), c.Bool("debug"), uint16(c.Uint("http.port")))
util.Fatal(err, "")

return nil
},
Expand All @@ -46,7 +48,7 @@ func ReadSandboxFromProjectOrAsk(c *cli.Context, useArguments bool) *Sandbox {
var sandbox *Sandbox
var minDistroVersion string
// use configured sandbox if we're in a project folder
if c.NArg() == 0 && common.HasProjectData(".") {
if !useArguments || (c.NArg() == 0 && common.HasProjectData(".")) {
pData := common.ReadProjectData(".")
minDistroVersion = common.ReadProjectDistroVersion(".")
sandbox = ReadSandboxData(pData.Sandbox)
Expand All @@ -64,17 +66,16 @@ func ReadSandboxFromProjectOrAsk(c *cli.Context, useArguments bool) *Sandbox {
return sandbox
}

func StartSandbox(c *cli.Context, sandbox *Sandbox, detach, devMode, debug bool, httpPort uint16) {
func StartSandbox(c *cli.Context, sandbox *Sandbox, detach, devMode, debug bool, httpPort uint16) (error, bool) {
force := common.IsForceMode(c)
rData := common.ReadRuntimeData()
isSandboxRunning := common.VerifyRuntimeData(&rData)

if isSandboxRunning {
if rData.Running == sandbox.Name {
fmt.Fprintf(os.Stderr, "Sandbox '%s' is already running", rData.Running)
os.Exit(1)
} else {
AskToStopSandbox(rData, force)
if rData.Running == sandbox.Name && ((rData.Mode == common.MODE_DEV) == devMode) {
return nil, true
} else if !AskToStopSandbox(rData, force) {
return errors.New(fmt.Sprintf("Sandbox '%s' is already running in %s mode", rData.Running, rData.Mode)), true
}
} else {
ports := []uint16{httpPort, common.MGMT_PORT, common.INFO_PORT}
Expand All @@ -85,8 +86,7 @@ func StartSandbox(c *cli.Context, sandbox *Sandbox, detach, devMode, debug bool,
}
}
if len(unavailablePorts) > 0 {
fmt.Fprintf(os.Stderr, "Port(s) %v are not available, stop the app(s) using them first!\n", unavailablePorts)
os.Exit(1)
return errors.New(fmt.Sprintf("Port(s) %v are not available, stop the app(s) using them first!\n", unavailablePorts)), false
}
}

Expand All @@ -102,32 +102,39 @@ func StartSandbox(c *cli.Context, sandbox *Sandbox, detach, devMode, debug bool,
// current process will finish so use detached process' PID
pid = cmd.Process.Pid
}
writeRunningSandbox(sandbox.Name, pid)
writeRunningSandbox(sandbox.Name, pid, devMode)

if !detach {
util.ListenForInterrupt(func() {
fmt.Fprintln(os.Stderr)
fmt.Fprintf(os.Stderr, "Got interrupt signal, stopping sandbox '%s'\n", sandbox.Name)
fmt.Fprintln(os.Stderr)
writeRunningSandbox("", 0)
writeRunningSandbox("", 0, false)
})
cmd.Wait()
} else {
fmt.Fprintf(os.Stdout, "Started sandbox '%s' in detached mode.\n", sandbox.Name)
}
return nil, false
}

func AskToStopSandbox(rData common.RuntimeData, force bool) {
func AskToStopSandbox(rData common.RuntimeData, force bool) bool {
if force || util.PromptBool(fmt.Sprintf("Sandbox '%s' is running, do you want to stop it", rData.Running), true) {
StopSandbox(rData)
return true
} else {
os.Exit(1)
return false
}
}

func writeRunningSandbox(name string, pid int) {
func writeRunningSandbox(name string, pid int, dev bool) {
data := common.ReadRuntimeData()
data.Running = name
data.PID = pid
if dev {
data.Mode = common.MODE_DEV
} else {
data.Mode = common.MODE_DEFAULT
}
common.WriteRuntimeData(data)
}
2 changes: 1 addition & 1 deletion internal/app/commands/sandbox/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var Stop = cli.Command{
func StopSandbox(rData common.RuntimeData) {
pId := rData.PID
stopDistro(pId)
writeRunningSandbox("", 0)
writeRunningSandbox("", 0, false)

common.StartSpinner(fmt.Sprintf("Stopping sandbox '%s'", rData.Running))
if err := util.WaitUntilProcessStopped(pId, 30); err != nil {
Expand Down
Loading