1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-13 10:50:38 +00:00

design Command for governor module & decouple web module from task module

This commit is contained in:
Ming Deng
2020-09-20 14:18:13 +00:00
parent 089006525e
commit 44127edefc
6 changed files with 311 additions and 25 deletions

View File

@ -21,7 +21,6 @@ import (
"time"
"github.com/astaxie/beego/pkg/infrastructure/logs"
"github.com/astaxie/beego/pkg/task"
)
// BeeAdminApp is the default adminApp used by admin module.
@ -86,9 +85,12 @@ type adminApp struct {
// Route adds http.HandlerFunc to adminApp with url pattern.
func (admin *adminApp) Run() {
if len(task.AdminTaskList) > 0 {
task.StartTask()
}
// if len(task.AdminTaskList) > 0 {
// task.StartTask()
// }
logs.Warning("now we don't start tasks here, if you use task module," +
" please invoke task.StartTask, or task will not be executed")
addr := BConfig.Listen.AdminAddr
if BConfig.Listen.AdminPort != 0 {

View File

@ -16,7 +16,6 @@ package web
import (
"bytes"
context2 "context"
"encoding/json"
"fmt"
"net/http"
@ -26,7 +25,6 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/astaxie/beego/pkg/infrastructure/governor"
"github.com/astaxie/beego/pkg/task"
)
type adminController struct {
@ -90,19 +88,22 @@ func (a *adminController) TaskStatus() {
req.ParseForm()
taskname := req.Form.Get("taskname")
if taskname != "" {
if t, ok := task.AdminTaskList[taskname]; ok {
if err := t.Run(nil); err != nil {
data["Message"] = []string{"error", template.HTMLEscapeString(fmt.Sprintf("%s", err))}
}
data["Message"] = []string{"success", template.HTMLEscapeString(fmt.Sprintf("%s run success,Now the Status is <br>%s", taskname, t.GetStatus(nil)))}
cmd := governor.GetCommand("task", "run")
res := cmd.Execute(taskname)
if res.IsSuccess() {
data["Message"] = []string{"success",
template.HTMLEscapeString(fmt.Sprintf("%s run success,Now the Status is <br>%s",
taskname, res.Content.(string)))}
} else {
data["Message"] = []string{"warning", template.HTMLEscapeString(fmt.Sprintf("there's no task which named: %s", taskname))}
data["Message"] = []string{"error", template.HTMLEscapeString(fmt.Sprintf("%s", res.Error))}
}
}
// List Tasks
content := make(M)
resultList := new([][]string)
resultList := governor.GetCommand("task", "list").Execute().Content.([][]string)
var fields = []string{
"Task Name",
"Task Spec",
@ -110,15 +111,6 @@ func (a *adminController) TaskStatus() {
"Last Time",
"",
}
for tname, tk := range task.AdminTaskList {
result := []string{
template.HTMLEscapeString(tname),
template.HTMLEscapeString(tk.GetSpec(nil)),
template.HTMLEscapeString(tk.GetStatus(nil)),
template.HTMLEscapeString(tk.GetPrev(context2.Background()).String()),
}
*resultList = append(*resultList, result)
}
content["Fields"] = fields
content["Data"] = resultList