Merge pull request #349 from trheyi/main
[add] start tasks and schedules
This commit is contained in:
commit
36f94a7ddc
5 changed files with 99 additions and 13 deletions
32
cmd/start.go
32
cmd/start.go
|
|
@ -22,10 +22,12 @@ import (
|
|||
"github.com/yaoapp/kun/log"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/engine"
|
||||
ischedule "github.com/yaoapp/yao/schedule"
|
||||
"github.com/yaoapp/yao/service"
|
||||
"github.com/yaoapp/yao/setup"
|
||||
"github.com/yaoapp/yao/share"
|
||||
"github.com/yaoapp/yao/studio"
|
||||
itask "github.com/yaoapp/yao/task"
|
||||
)
|
||||
|
||||
var startDebug = false
|
||||
|
|
@ -134,6 +136,15 @@ var startCmd = &cobra.Command{
|
|||
|
||||
}
|
||||
|
||||
// Start Tasks
|
||||
itask.Start()
|
||||
defer itask.Stop()
|
||||
|
||||
// Start Schedules
|
||||
ischedule.Start()
|
||||
defer ischedule.Stop()
|
||||
|
||||
// Start HTTP Server
|
||||
srv, err := service.Start(config.Conf)
|
||||
defer func() {
|
||||
service.Stop(srv)
|
||||
|
|
@ -145,7 +156,16 @@ var startCmd = &cobra.Command{
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
// print the messages under the production mode
|
||||
// Start watching
|
||||
watchDone := make(chan uint8, 1)
|
||||
if mode == "development" && !startDisableWatching {
|
||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Watching")))
|
||||
fmt.Println(color.WhiteString("---------------------------------"))
|
||||
go service.Watch(srv, watchDone)
|
||||
}
|
||||
|
||||
// Print the messages under the production mode
|
||||
if mode == "production" {
|
||||
printApis(true)
|
||||
printTasks(true)
|
||||
|
|
@ -154,16 +174,6 @@ var startCmd = &cobra.Command{
|
|||
printStores(true)
|
||||
}
|
||||
|
||||
// start watching
|
||||
watchDone := make(chan uint8, 1)
|
||||
if mode == "development" && !startDisableWatching {
|
||||
// Watching
|
||||
fmt.Println(color.WhiteString("\n---------------------------------"))
|
||||
fmt.Println(color.WhiteString(L("Watching")))
|
||||
fmt.Println(color.WhiteString("---------------------------------"))
|
||||
go service.Watch(srv, watchDone)
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case v := <-srv.Event():
|
||||
|
|
|
|||
|
|
@ -1,20 +1,49 @@
|
|||
package schedule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/yaoapp/gou/application"
|
||||
"github.com/yaoapp/gou/schedule"
|
||||
"github.com/yaoapp/kun/log"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/share"
|
||||
)
|
||||
|
||||
// Load load schedule
|
||||
func Load(cfg config.Config) error {
|
||||
messages := []string{}
|
||||
exts := []string{"*.sch.yao", "*.sch.json", "*.sch.jsonc"}
|
||||
return application.App.Walk("schedules", func(root, file string, isdir bool) error {
|
||||
err := application.App.Walk("schedules", func(root, file string, isdir bool) error {
|
||||
if isdir {
|
||||
return nil
|
||||
}
|
||||
_, err := schedule.Load(file, share.ID(root, file))
|
||||
if err != nil {
|
||||
messages = append(messages, err.Error())
|
||||
}
|
||||
return err
|
||||
}, exts...)
|
||||
|
||||
if len(messages) > 0 {
|
||||
return fmt.Errorf(strings.Join(messages, ";\n"))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Start schedules
|
||||
func Start() {
|
||||
for name, sch := range schedule.Schedules {
|
||||
sch.Start()
|
||||
log.Info("[Schedule] %s start", name)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop schedules
|
||||
func Stop() {
|
||||
for name, sch := range schedule.Schedules {
|
||||
sch.Stop()
|
||||
log.Info("[Schedule] %s stop", name)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,15 @@ func TestLoad(t *testing.T) {
|
|||
check(t)
|
||||
}
|
||||
|
||||
func TestStartStop(t *testing.T) {
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
|
||||
Load(config.Conf)
|
||||
Start()
|
||||
defer Stop()
|
||||
}
|
||||
|
||||
func check(t *testing.T) {
|
||||
ids := map[string]bool{}
|
||||
for id := range schedule.Schedules {
|
||||
|
|
|
|||
31
task/task.go
31
task/task.go
|
|
@ -1,20 +1,49 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/yaoapp/gou/application"
|
||||
"github.com/yaoapp/gou/task"
|
||||
"github.com/yaoapp/kun/log"
|
||||
"github.com/yaoapp/yao/config"
|
||||
"github.com/yaoapp/yao/share"
|
||||
)
|
||||
|
||||
// Load load task
|
||||
func Load(cfg config.Config) error {
|
||||
messages := []string{}
|
||||
exts := []string{"*.yao", "*.json", "*.jsonc"}
|
||||
return application.App.Walk("tasks", func(root, file string, isdir bool) error {
|
||||
err := application.App.Walk("tasks", func(root, file string, isdir bool) error {
|
||||
if isdir {
|
||||
return nil
|
||||
}
|
||||
_, err := task.Load(file, share.ID(root, file))
|
||||
if err != nil {
|
||||
messages = append(messages, err.Error())
|
||||
}
|
||||
return err
|
||||
}, exts...)
|
||||
|
||||
if len(messages) > 0 {
|
||||
return fmt.Errorf(strings.Join(messages, ";\n"))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Start tasks
|
||||
func Start() {
|
||||
for name, t := range task.Tasks {
|
||||
go t.Start()
|
||||
log.Info("[Task] %s start", name)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop tasks
|
||||
func Stop() {
|
||||
for name, t := range task.Tasks {
|
||||
t.Stop()
|
||||
log.Info("[Task] %s stop", name)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,15 @@ func TestLoad(t *testing.T) {
|
|||
check(t)
|
||||
}
|
||||
|
||||
func TestStartStop(t *testing.T) {
|
||||
test.Prepare(t, config.Conf)
|
||||
defer test.Clean()
|
||||
|
||||
Load(config.Conf)
|
||||
Start()
|
||||
defer Stop()
|
||||
}
|
||||
|
||||
func check(t *testing.T) {
|
||||
ids := map[string]bool{}
|
||||
for id := range task.Tasks {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue