[add] migrate watch

This commit is contained in:
Max 2023-04-01 11:19:09 +08:00
parent 9a72f80bdd
commit 5f2b8fbf63
3 changed files with 65 additions and 149 deletions

View file

@ -129,13 +129,15 @@ var startCmd = &cobra.Command{
} }
// start watching srv, err := service.Start(config.Conf)
if mode == "development" && !startDisableWatching { defer func() {
// Watching service.Stop(srv)
fmt.Println(color.WhiteString("\n---------------------------------")) fmt.Println(color.GreenString(L("✨EXITED✨")))
fmt.Println(color.WhiteString(L("Watching"))) }()
fmt.Println(color.WhiteString("---------------------------------"))
// service.Watch(config.Conf) if err != nil {
fmt.Println(color.RedString(L("Fatal: %s"), err.Error()))
os.Exit(1)
} }
// print the messages under the production mode // print the messages under the production mode
@ -147,11 +149,15 @@ var startCmd = &cobra.Command{
printStores(true) printStores(true)
} }
srv, err := service.Start(config.Conf) // start watching
defer func() { watchDone := make(chan uint8, 1)
service.Stop(srv) if mode == "development" && !startDisableWatching {
fmt.Println(color.GreenString(L("✨EXITED✨"))) // Watching
}() fmt.Println(color.WhiteString("\n---------------------------------"))
fmt.Println(color.WhiteString(L("Watching")))
fmt.Println(color.WhiteString("---------------------------------"))
go service.Watch(srv, watchDone)
}
for { for {
select { select {
@ -170,6 +176,7 @@ var startCmd = &cobra.Command{
} }
case <-interrupt: case <-interrupt:
watchDone <- 1
return return
} }
} }

View file

@ -13,6 +13,8 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/yaoapp/gou/application"
"github.com/yaoapp/gou/server/http"
"github.com/yaoapp/kun/log" "github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/engine" "github.com/yaoapp/yao/engine"
@ -27,12 +29,41 @@ var handlers = map[string]func(root string, file string, event string, cfg confi
} }
// Watch the application code change for hot update // Watch the application code change for hot update
func Watch(cfg config.Config) (err error) { func Watch(srv *http.Server, interrupt chan uint8) (err error) {
go func() { err = watchStart(cfg) }()
select { if application.App == nil {
case <-watchReady: return fmt.Errorf("Application is not initialized")
return nil
} }
return application.App.Watch(func(event, name string) {
if strings.Contains(event, "CHMOD") {
return
}
// Reload
err = engine.Reload(config.Conf)
if err != nil {
fmt.Println(color.RedString("[Watch] Reload: %s", err.Error()))
return
}
fmt.Println(color.GreenString("[Watch] Reload Completed"))
// Model
if strings.HasPrefix(name, "/models") {
fmt.Println(color.GreenString("[Watch] Model: %s changed (Please run yao migrate manually)", name))
}
// Restart
if strings.HasPrefix(name, "/apis") {
err = srv.Restart()
if err != nil {
fmt.Println(color.RedString("[Watch] Restart: %s", err.Error()))
return
}
fmt.Println(color.GreenString("[Watch] Restart Completed"))
}
}, interrupt)
} }
// StopWatch stop watching the code change // StopWatch stop watching the code change

View file

@ -1,153 +1,31 @@
package service package service
import ( import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing" "testing"
"time" "time"
"github.com/yaoapp/yao/config" "github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/share" "github.com/yaoapp/yao/engine"
) )
func TestWatch(t *testing.T) { func TestWatch(t *testing.T) {
err := engine.Load(config.Conf)
share.DBConnect(config.Conf.DB)
err := Watch(config.Conf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer StopWatch()
createDir(t) srv, err := Start(config.Conf)
renameDir(t)
createModel(t)
changeModel(t)
renameModel(t)
removeModel(t)
createModel(t)
removeDir(t)
}
func TestWatchReload(t *testing.T) {
go Start(config.Conf)
// defer Stop(func() {})
share.DBConnect(config.Conf.DB)
watchReload("", "", "", config.Conf)
}
func createDir(t *testing.T) {
root := config.Conf.Root
file := filepath.Join(root, "models", "watch", "test")
fmt.Println("CREATE-DIR", file)
err := os.MkdirAll(file, os.ModePerm)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
time.Sleep(1 * time.Second) defer Stop(srv)
}
func renameDir(t *testing.T) { done := make(chan uint8, 1)
root := config.Conf.Root go Watch(srv, done)
file := filepath.Join(root, "models", "watch", "test")
new := filepath.Join(root, "models", "watch", "test_new")
fmt.Println("RENAME-DIR", file)
err := os.Rename(file, new)
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
}
func removeDir(t *testing.T) { select {
root := config.Conf.Root case <-time.After(200 * time.Millisecond):
file := filepath.Join(root, "models", "watch") done <- 1
fmt.Println("REMOVE-DIR", file) return
err := os.RemoveAll(file)
if err != nil {
t.Fatal(err)
} }
time.Sleep(1 * time.Second)
}
func createModel(t *testing.T) {
dsl := `
{
"name": "watch-test",
"table": {
"name": "watch_test",
"comment": "WatchTest",
"engine": "InnoDB"
},
"columns": [
{ "name": "id", "type": "ID" },
{ "label": "Name", "name": "name", "type": "string", "index": true }
],
"relations": {},
"option": { "timestamps": true, "soft_deletes": true }
}
`
root := config.Conf.Root
file := filepath.Join(root, "models", "watch", "test_new", "watch.mod.json")
fmt.Println("CREATE", file)
err := ioutil.WriteFile(file, []byte(dsl), 0644)
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
}
func changeModel(t *testing.T) {
dsl := `
{
"name": "watch-test",
"table": {
"name": "watch_test",
"comment": "WatchTest",
"engine": "InnoDB"
},
"columns": [
{ "name": "id", "type": "ID" },
{ "label": "Name", "name": "name", "type": "string", "index": true },
{ "label": "Data", "name": "data", "type": "json", "nullable": true }
],
"relations": {},
"option": { "timestamps": true, "soft_deletes": true }
}
`
root := config.Conf.Root
file := filepath.Join(root, "models", "watch", "test_new", "watch.mod.json")
fmt.Println("CHANGE", file)
err := ioutil.WriteFile(file, []byte(dsl), 0644)
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
}
func renameModel(t *testing.T) {
root := config.Conf.Root
file := filepath.Join(root, "models", "watch", "test_new", "watch.mod.json")
new := filepath.Join(root, "models", "watch", "test_new", "watch_new.mod.json")
fmt.Println("RENAME", new)
err := os.Rename(file, new)
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
}
func removeModel(t *testing.T) {
root := config.Conf.Root
file := filepath.Join(root, "models", "watch", "test_new", "watch_new.mod.json")
fmt.Println("REMOVE", file)
err := os.Remove(file)
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
} }