[add] start tasks and schedules
This commit is contained in:
parent
c38ceb9a22
commit
106631b106
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/kun/log"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/engine"
|
"github.com/yaoapp/yao/engine"
|
||||||
|
ischedule "github.com/yaoapp/yao/schedule"
|
||||||
"github.com/yaoapp/yao/service"
|
"github.com/yaoapp/yao/service"
|
||||||
"github.com/yaoapp/yao/setup"
|
"github.com/yaoapp/yao/setup"
|
||||||
"github.com/yaoapp/yao/share"
|
"github.com/yaoapp/yao/share"
|
||||||
"github.com/yaoapp/yao/studio"
|
"github.com/yaoapp/yao/studio"
|
||||||
|
itask "github.com/yaoapp/yao/task"
|
||||||
)
|
)
|
||||||
|
|
||||||
var startDebug = false
|
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)
|
srv, err := service.Start(config.Conf)
|
||||||
defer func() {
|
defer func() {
|
||||||
service.Stop(srv)
|
service.Stop(srv)
|
||||||
|
|
@ -145,7 +156,16 @@ var startCmd = &cobra.Command{
|
||||||
os.Exit(1)
|
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" {
|
if mode == "production" {
|
||||||
printApis(true)
|
printApis(true)
|
||||||
printTasks(true)
|
printTasks(true)
|
||||||
|
|
@ -154,16 +174,6 @@ var startCmd = &cobra.Command{
|
||||||
printStores(true)
|
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 {
|
for {
|
||||||
select {
|
select {
|
||||||
case v := <-srv.Event():
|
case v := <-srv.Event():
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,49 @@
|
||||||
package schedule
|
package schedule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/yaoapp/gou/application"
|
"github.com/yaoapp/gou/application"
|
||||||
"github.com/yaoapp/gou/schedule"
|
"github.com/yaoapp/gou/schedule"
|
||||||
|
"github.com/yaoapp/kun/log"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/share"
|
"github.com/yaoapp/yao/share"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load load schedule
|
// Load load schedule
|
||||||
func Load(cfg config.Config) error {
|
func Load(cfg config.Config) error {
|
||||||
|
messages := []string{}
|
||||||
exts := []string{"*.sch.yao", "*.sch.json", "*.sch.jsonc"}
|
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 {
|
if isdir {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, err := schedule.Load(file, share.ID(root, file))
|
_, err := schedule.Load(file, share.ID(root, file))
|
||||||
|
if err != nil {
|
||||||
|
messages = append(messages, err.Error())
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}, exts...)
|
}, 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)
|
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) {
|
func check(t *testing.T) {
|
||||||
ids := map[string]bool{}
|
ids := map[string]bool{}
|
||||||
for id := range schedule.Schedules {
|
for id := range schedule.Schedules {
|
||||||
|
|
|
||||||
31
task/task.go
31
task/task.go
|
|
@ -1,20 +1,49 @@
|
||||||
package task
|
package task
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/yaoapp/gou/application"
|
"github.com/yaoapp/gou/application"
|
||||||
"github.com/yaoapp/gou/task"
|
"github.com/yaoapp/gou/task"
|
||||||
|
"github.com/yaoapp/kun/log"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
"github.com/yaoapp/yao/share"
|
"github.com/yaoapp/yao/share"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load load task
|
// Load load task
|
||||||
func Load(cfg config.Config) error {
|
func Load(cfg config.Config) error {
|
||||||
|
messages := []string{}
|
||||||
exts := []string{"*.yao", "*.json", "*.jsonc"}
|
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 {
|
if isdir {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, err := task.Load(file, share.ID(root, file))
|
_, err := task.Load(file, share.ID(root, file))
|
||||||
|
if err != nil {
|
||||||
|
messages = append(messages, err.Error())
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}, exts...)
|
}, 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)
|
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) {
|
func check(t *testing.T) {
|
||||||
ids := map[string]bool{}
|
ids := map[string]bool{}
|
||||||
for id := range task.Tasks {
|
for id := range task.Tasks {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue