diff --git a/admin.go b/admin.go index 68779395..f89d0f08 100644 --- a/admin.go +++ b/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") diff --git a/admin/healthcheck.go b/admin/healthcheck.go new file mode 100644 index 00000000..82dc46b8 --- /dev/null +++ b/admin/healthcheck.go @@ -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) +} diff --git a/admin/statistics.go b/admin/statistics.go index 6991803e..704239ce 100644 --- a/admin/statistics.go +++ b/admin/statistics.go @@ -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, diff --git a/admin/task.go b/admin/task.go index bf48fd8c..b55fb929 100644 --- a/admin/task.go +++ b/admin/task.go @@ -1 +1,79 @@ -package admin \ No newline at end of file +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) +}