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

feat: add hot reloading on dev mode #33

Merged
merged 1 commit into from
Feb 7, 2024
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
103 changes: 36 additions & 67 deletions cmd/world/cardinal/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import (

"github.com/magefile/mage/sh"
"github.com/spf13/cobra"
"github.com/zulkhair/fresh/runner"
)

const (
CardinalPort = "3333"
RedisPort = "6379"
)

// StopChan is used to signal graceful shutdown
var StopChan = make(chan struct{})

/////////////////
// Cobra Setup //
/////////////////
Expand All @@ -35,7 +39,7 @@ var devCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
logger.SetDebugMode(cmd)

fmt.Print(style.CLIHeader("Cardinal", "Running Cardinal in dev mode"), "\n")
fmt.Print(style.CLIHeader("Cardinal", "Running Cardinal in dev mode, cardinal supports hot reloading"), "\n")
fmt.Println(style.BoldText.Render("Press Ctrl+C to stop"))
fmt.Println()
fmt.Println(fmt.Sprintf("Redis: localhost:%s", RedisPort))
Expand Down Expand Up @@ -65,62 +69,37 @@ var devCmd = &cobra.Command{
isRedisRunning = true
}

// Run Cardinal
cardinalExecCmd, err := runCardinal()
if err != nil {
return err
}

// Create a channel to receive termination signals
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)

// Create a channel to receive errors from the command
cmdErr := make(chan error, 1)
// Run Cardinal Preparation
err = runCardinalPrep()
if err != nil {
return err
}

fmt.Println("Starting Cardinal...")
go runner.Start()

// Start a goroutine to listen for signals
go func() {
err := cardinalExecCmd.Wait()
cmdErr <- err
<-signalCh
close(StopChan)
}()

select {
case <-signalCh:
// Shutdown signal received, attempt to gracefully stop the command
errCleanup := cleanup()
if errCleanup != nil {
return errCleanup
}

isProcessRunning := func(cmd *exec.Cmd) bool {
return cardinalExecCmd.ProcessState == nil && cardinalExecCmd.Process != nil
}
// Wait for stop signal
<-StopChan
runner.Stop() // Stop Cardinal

//wait up to 10 seconds for it to quit.
for i := 0; i < 10; i++ {
if isProcessRunning(cardinalExecCmd) {
time.Sleep(1 * time.Second)
} else {
break
}
}
if isProcessRunning(cardinalExecCmd) {
err = cardinalExecCmd.Process.Signal(syscall.SIGTERM)
if err != nil {
return err
}
}
// Cleanup redis
errCleanup := cleanup()
if errCleanup != nil {
return errCleanup
}

return nil
return nil

case err := <-cmdErr:
fmt.Println(err)
errCleanup := cleanup()
if errCleanup != nil {
return errCleanup
}
return nil
}
},
}

Expand Down Expand Up @@ -152,36 +131,26 @@ func runRedis() error {
return nil
}

// runCardinal runs cardinal in dev mode.
// runCardinalPrep preparation for runs cardinal in dev mode.
// We run cardinal without docker to make it easier to debug and skip the docker image build step
func runCardinal() (*exec.Cmd, error) {
func runCardinalPrep() error {
err := os.Chdir("cardinal")
if err != nil {
return nil, errors.New("can't find cardinal directory. Are you in the root of a World Engine project")
return errors.New("can't find cardinal directory. Are you in the root of a World Engine project")
}

env := map[string]string{
"REDIS_MODE": "normal",
"CARDINAL_PORT": CardinalPort,
"REDIS_ADDR": fmt.Sprintf("localhost:%s", RedisPort),
"DEPLOY_MODE": "development",
}

cmd := exec.Command("go", "run", ".")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

cmd.Env = os.Environ()
for k, v := range env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
"REDIS_MODE": "normal",
"CARDINAL_PORT": CardinalPort,
"REDIS_ADDR": fmt.Sprintf("localhost:%s", RedisPort),
"DEPLOY_MODE": "development",
"RUNNER_IGNORED": "assets, tmp, vendor",
}

err = cmd.Start()
if err != nil {
return nil, err
for key, value := range env {
os.Setenv(key, value)
}

return cmd, nil
return nil
}

// cleanup stops and removes the Redis and Webdis containers
Expand Down
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
module pkg.world.dev/world-cli

go 1.21
go 1.21.1

require (
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/getsentry/sentry-go v0.26.0
github.com/guumaster/logsymbols v0.3.1
github.com/magefile/mage v1.15.0
github.com/pelletier/go-toml v1.9.5
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.7.0
github.com/zulkhair/fresh v0.0.0-20240206120148-20e4bcfbc141
gotest.tools/v3 v3.5.1
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/getsentry/sentry-go v0.26.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/howeyc/fsnotify v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a // indirect
github.com/pilu/miniassert v0.0.0-20140522125902-bee63581261a // indirect
)

require (
Expand Down
22 changes: 13 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/getsentry/sentry-go v0.26.0 h1:IX3++sF6/4B5JcevhdZfdKIHfyvMmAq/UnqcyT2H6mA=
github.com/getsentry/sentry-go v0.26.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/guumaster/logsymbols v0.3.1 h1:bnCE484dAQFvMWt2EfZAzF1oCgu8yo/Vp1QGQ0EmaAA=
github.com/guumaster/logsymbols v0.3.1/go.mod h1:1M5/1js2Z7Yo8DRB3QrPURwqsXeOfgsJv1Utjookknw=
github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand Down Expand Up @@ -49,6 +53,13 @@ github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs
github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a h1:Tg4E4cXPZSZyd3H1tJlYo6ZreXV0ZJvE/lorNqyw1AU=
github.com/pilu/config v0.0.0-20131214182432-3eb99e6c0b9a/go.mod h1:9Or9aIl95Kp43zONcHd5tLZGKXb9iLx0pZjau0uJ5zg=
github.com/pilu/miniassert v0.0.0-20140522125902-bee63581261a h1:U8Xgy85P2npR1jqpNF4q9rLSt6kzrOrWubkepc3lWbE=
github.com/pilu/miniassert v0.0.0-20140522125902-bee63581261a/go.mod h1:QKd9TvGj31l6g+MV2PqthhK68d5ZPv/Q2PvtsS0mM3I=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -64,15 +75,11 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/zulkhair/fresh v0.0.0-20240206120148-20e4bcfbc141 h1:XymlZUaPL97jSQRZ9dzbgAWIJJ/1Tjneoz/LlFo8Q9Q=
github.com/zulkhair/fresh v0.0.0-20240206120148-20e4bcfbc141/go.mod h1:21tH5jBa0HwMBoVsf4qBfImYjF2Qtfddsa2KY4y9zr4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -84,15 +91,12 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/gookit/color.v1 v1.1.6 h1:5fB10p6AUFjhd2ayq9JgmJWr9WlTrguFdw3qlYtKNHk=
gopkg.in/gookit/color.v1 v1.1.6/go.mod h1:IcEkFGaveVShJ+j8ew+jwe9epHyGpJ9IrptHmW3laVY=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
Expand Down
Loading