mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 14:30:50 +00:00
add more feture in admin
1,增加QPS的限制 2,增加任务 3,增加healthcheck
This commit is contained in:
parent
ea513002c5
commit
b5b53b3849
3
admin.go
3
admin.go
@ -32,6 +32,8 @@ func init() {
|
||||
BeeAdminApp.Route("/", AdminIndex)
|
||||
BeeAdminApp.Route("/qps", QpsIndex)
|
||||
BeeAdminApp.Route("/prof", ProfIndex)
|
||||
BeeAdminApp.Route("/healthcheck", admin.Healthcheck)
|
||||
BeeAdminApp.Route("/task", admin.TaskStatus)
|
||||
FilterMonitorFunc = func(string, string, time.Duration) bool { return true }
|
||||
}
|
||||
|
||||
@ -42,6 +44,7 @@ func AdminIndex(rw http.ResponseWriter, r *http.Request) {
|
||||
func QpsIndex(rw http.ResponseWriter, r *http.Request) {
|
||||
admin.StatisticsMap.GetMap(rw)
|
||||
}
|
||||
|
||||
func ProfIndex(rw http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
command := r.Form.Get("command")
|
||||
|
43
admin/healthcheck.go
Normal file
43
admin/healthcheck.go
Normal file
@ -0,0 +1,43 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//type DatabaseCheck struct {
|
||||
//}
|
||||
|
||||
//func (dc *DatabaseCheck) Check() error {
|
||||
// if dc.isConnected() {
|
||||
// return nil
|
||||
// } else {
|
||||
// return errors.New("can't connect database")
|
||||
// }
|
||||
//}
|
||||
|
||||
//AddHealthCheck("database",&DatabaseCheck{})
|
||||
|
||||
var AdminCheckList map[string]HealthChecker
|
||||
|
||||
type HealthChecker interface {
|
||||
Check() error
|
||||
}
|
||||
|
||||
func AddHealthCheck(name string, hc HealthChecker) {
|
||||
AdminCheckList[name] = hc
|
||||
}
|
||||
|
||||
func Healthcheck(rw http.ResponseWriter, req *http.Request) {
|
||||
for name, h := range AdminCheckList {
|
||||
if err := h.Check(); err != nil {
|
||||
fmt.Fprintf(rw, "%s : ok\n", name)
|
||||
} else {
|
||||
fmt.Fprintf(rw, "%s : %s\n", name, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
AdminCheckList = make(map[string]HealthChecker)
|
||||
}
|
@ -17,8 +17,9 @@ type Statistics struct {
|
||||
}
|
||||
|
||||
type UrlMap struct {
|
||||
lock sync.RWMutex
|
||||
urlmap map[string]map[string]*Statistics
|
||||
lock sync.RWMutex
|
||||
LengthLimit int //limit the urlmap's length if it's equal to 0 there's no limit
|
||||
urlmap map[string]map[string]*Statistics
|
||||
}
|
||||
|
||||
func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController string, requesttime time.Duration) {
|
||||
@ -47,6 +48,9 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
|
||||
}
|
||||
|
||||
} else {
|
||||
if m.LengthLimit > 0 && m.LengthLimit <= len(m.urlmap) {
|
||||
return
|
||||
}
|
||||
methodmap := make(map[string]*Statistics)
|
||||
nb := &Statistics{
|
||||
RequestUrl: requestUrl,
|
||||
|
@ -1 +1,79 @@
|
||||
package admin
|
||||
package admin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var AdminTaskList map[string]Tasker
|
||||
|
||||
type Tasker interface {
|
||||
GetStatus() string
|
||||
Run() error
|
||||
}
|
||||
|
||||
type Task struct {
|
||||
Taskname string
|
||||
Spec Schedule
|
||||
Errlist []map[uint64]string //errtime:errinfo
|
||||
ErrLimit int //max length for the errlist 0 stand for there' no limit
|
||||
}
|
||||
|
||||
func (t *Task) GetStatus() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (t *Task) Run() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Task) SetCron(spec string) {
|
||||
|
||||
}
|
||||
|
||||
type Schedule struct {
|
||||
Second uint64
|
||||
Minute uint64
|
||||
Hour uint64
|
||||
DOM uint64
|
||||
Month uint64
|
||||
DOW uint64
|
||||
}
|
||||
|
||||
func StartTask() {
|
||||
|
||||
}
|
||||
|
||||
func StopTask() {
|
||||
|
||||
}
|
||||
|
||||
func AddTask(taskname string, t Tasker) {
|
||||
AdminTaskList[taskname] = t
|
||||
}
|
||||
|
||||
func TaskStatus(rw http.ResponseWriter, req *http.Request) {
|
||||
for tname, t := range AdminTaskList {
|
||||
fmt.Fprintf(rw, "%s:%s", tname, t.GetStatus())
|
||||
}
|
||||
}
|
||||
|
||||
//to run a Task by http from the querystring taskname
|
||||
//url like /task?taskname=sendmail
|
||||
func RunTask(rw http.ResponseWriter, req *http.Request) {
|
||||
req.ParseForm()
|
||||
taskname := req.Form.Get("taskname")
|
||||
if t, ok := AdminTaskList[taskname]; ok {
|
||||
err := t.Run()
|
||||
if err != nil {
|
||||
fmt.Fprintf(rw, "%v", err)
|
||||
}
|
||||
fmt.Fprintf(rw, "%s run success,Now the Status is %s", t.GetStatus())
|
||||
} else {
|
||||
fmt.Fprintf(rw, "there's no task which named:%s", taskname)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
AdminTaskList = make(map[string]Tasker)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user