54185df4 by astaxie

change admin to toolbox & support task

1 parent 16352579
...@@ -2,7 +2,7 @@ package beego ...@@ -2,7 +2,7 @@ package beego
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/astaxie/beego/admin" 5 "github.com/astaxie/beego/toolbox"
6 "net/http" 6 "net/http"
7 "time" 7 "time"
8 ) 8 )
...@@ -32,8 +32,9 @@ func init() { ...@@ -32,8 +32,9 @@ func init() {
32 BeeAdminApp.Route("/", AdminIndex) 32 BeeAdminApp.Route("/", AdminIndex)
33 BeeAdminApp.Route("/qps", QpsIndex) 33 BeeAdminApp.Route("/qps", QpsIndex)
34 BeeAdminApp.Route("/prof", ProfIndex) 34 BeeAdminApp.Route("/prof", ProfIndex)
35 BeeAdminApp.Route("/healthcheck", admin.Healthcheck) 35 BeeAdminApp.Route("/healthcheck", toolbox.Healthcheck)
36 BeeAdminApp.Route("/task", admin.TaskStatus) 36 BeeAdminApp.Route("/task", toolbox.TaskStatus)
37 BeeAdminApp.Route("/runtask", toolbox.RunTask)
37 FilterMonitorFunc = func(string, string, time.Duration) bool { return true } 38 FilterMonitorFunc = func(string, string, time.Duration) bool { return true }
38 } 39 }
39 40
...@@ -42,14 +43,14 @@ func AdminIndex(rw http.ResponseWriter, r *http.Request) { ...@@ -42,14 +43,14 @@ func AdminIndex(rw http.ResponseWriter, r *http.Request) {
42 } 43 }
43 44
44 func QpsIndex(rw http.ResponseWriter, r *http.Request) { 45 func QpsIndex(rw http.ResponseWriter, r *http.Request) {
45 admin.StatisticsMap.GetMap(rw) 46 toolbox.StatisticsMap.GetMap(rw)
46 } 47 }
47 48
48 func ProfIndex(rw http.ResponseWriter, r *http.Request) { 49 func ProfIndex(rw http.ResponseWriter, r *http.Request) {
49 r.ParseForm() 50 r.ParseForm()
50 command := r.Form.Get("command") 51 command := r.Form.Get("command")
51 if command != "" { 52 if command != "" {
52 admin.ProcessInput(command, rw) 53 toolbox.ProcessInput(command, rw)
53 } else { 54 } else {
54 rw.Write([]byte("request url like '/prof?command=lookup goroutine'")) 55 rw.Write([]byte("request url like '/prof?command=lookup goroutine'"))
55 } 56 }
......
1 package admin
2
3 import (
4 "fmt"
5 "net/http"
6 )
7
8 var AdminTaskList map[string]Tasker
9
10 type Tasker interface {
11 GetStatus() string
12 Run() error
13 }
14
15 type Task struct {
16 Taskname string
17 Spec Schedule
18 Errlist []map[uint64]string //errtime:errinfo
19 ErrLimit int //max length for the errlist 0 stand for there' no limit
20 }
21
22 func (t *Task) GetStatus() string {
23 return ""
24 }
25
26 func (t *Task) Run() error {
27 return nil
28 }
29
30 func (t *Task) SetCron(spec string) {
31
32 }
33
34 type Schedule struct {
35 Second uint64
36 Minute uint64
37 Hour uint64
38 DOM uint64
39 Month uint64
40 DOW uint64
41 }
42
43 func StartTask() {
44
45 }
46
47 func StopTask() {
48
49 }
50
51 func AddTask(taskname string, t Tasker) {
52 AdminTaskList[taskname] = t
53 }
54
55 func TaskStatus(rw http.ResponseWriter, req *http.Request) {
56 for tname, t := range AdminTaskList {
57 fmt.Fprintf(rw, "%s:%s", tname, t.GetStatus())
58 }
59 }
60
61 //to run a Task by http from the querystring taskname
62 //url like /task?taskname=sendmail
63 func RunTask(rw http.ResponseWriter, req *http.Request) {
64 req.ParseForm()
65 taskname := req.Form.Get("taskname")
66 if t, ok := AdminTaskList[taskname]; ok {
67 err := t.Run()
68 if err != nil {
69 fmt.Fprintf(rw, "%v", err)
70 }
71 fmt.Fprintf(rw, "%s run success,Now the Status is %s", t.GetStatus())
72 } else {
73 fmt.Fprintf(rw, "there's no task which named:%s", taskname)
74 }
75 }
76
77 func init() {
78 AdminTaskList = make(map[string]Tasker)
79 }
...@@ -2,9 +2,9 @@ package beego ...@@ -2,9 +2,9 @@ package beego
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/astaxie/beego/admin"
6 beecontext "github.com/astaxie/beego/context" 5 beecontext "github.com/astaxie/beego/context"
7 "github.com/astaxie/beego/middleware" 6 "github.com/astaxie/beego/middleware"
7 "github.com/astaxie/beego/toolbox"
8 "net/http" 8 "net/http"
9 "net/url" 9 "net/url"
10 "os" 10 "os"
...@@ -861,9 +861,9 @@ Admin: ...@@ -861,9 +861,9 @@ Admin:
861 timeend := time.Since(starttime) 861 timeend := time.Since(starttime)
862 if FilterMonitorFunc(r.Method, requestPath, timeend) { 862 if FilterMonitorFunc(r.Method, requestPath, timeend) {
863 if runrouter != nil { 863 if runrouter != nil {
864 go admin.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.controllerType.Name(), timeend) 864 go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, runrouter.controllerType.Name(), timeend)
865 } else { 865 } else {
866 go admin.StatisticsMap.AddStatistics(r.Method, requestPath, "", timeend) 866 go toolbox.StatisticsMap.AddStatistics(r.Method, requestPath, "", timeend)
867 } 867 }
868 } 868 }
869 } 869 }
......
1 package admin 1 package toolbox
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
......
1 package admin 1 package toolbox
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
......
1 package admin 1 package toolbox
2 2
3 import ( 3 import (
4 "os" 4 "os"
......
1 package admin 1 package toolbox
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
......
1 package admin 1 package toolbox
2 2
3 import ( 3 import (
4 "os" 4 "os"
......
1 package toolbox
...\ No newline at end of file ...\ No newline at end of file
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!