完成其它几个接口

pull/1/head
Doflatango 2017-01-16 14:30:55 +08:00 committed by miraclesu
parent 385022fa61
commit 533f0fab5d
7 changed files with 92 additions and 19 deletions

View File

@ -1,7 +1,6 @@
package models
import (
"errors"
"time"
"golang.org/x/net/context"
@ -64,8 +63,6 @@ func (c *Client) Put(key, val string, opts ...client.OpOption) (*client.PutRespo
return c.Client.Put(ctx, key, val, opts...)
}
var ErrValueMayChanged = errors.New("The value has been changed by others on this time.")
func (c *Client) PutWithRev(key, val string, rev int64) (*client.PutResponse, error) {
if rev == 0 {
return c.Put(key, val)

13
models/errors.go Normal file
View File

@ -0,0 +1,13 @@
package models
import "errors"
var (
ErrNotFound = errors.New("Record not found.")
ErrValueMayChanged = errors.New("The value has been changed by others on this time.")
ErrEmptyJobName = errors.New("Name of job is empty.")
ErrEmptyJobCommand = errors.New("Command of job is empty.")
ErrEmptyNodeGroupName = errors.New("Name of node group is empty.")
)

View File

@ -2,7 +2,6 @@ package models
import (
"encoding/json"
"errors"
"strings"
client "github.com/coreos/etcd/clientv3"
@ -56,12 +55,16 @@ func GetGroups() (groups map[string]*Group, err error) {
return
}
var (
ErrEmptyNodeGroupName = errors.New("Name of node group is empty.")
)
func DeleteGroupById(id string) (*client.DeleteResponse, error) {
return DefalutClient.Delete(GroupKey(id))
}
func GroupKey(id string) string {
return conf.Config.Group + id
}
func (g *Group) Key() string {
return conf.Config.Group + g.ID
return GroupKey(g.ID)
}
func (g *Group) Put(rev int64) (*client.PutResponse, error) {

View File

@ -2,7 +2,6 @@ package models
import (
"encoding/json"
"errors"
"strings"
client "github.com/coreos/etcd/clientv3"
@ -23,7 +22,7 @@ type Job struct {
Group string `json:"group"`
Command string `json:"cmd"`
Rule []*JobRule `json:"rule"`
Pause bool `json:"Pause"` // 可手工控制的状态,运行中/暂停
Pause bool `json:"pause"` // 可手工控制的状态
Schedules map[string][]string `json:"-"` // map[ip][]timer node 服务使用
}
@ -35,6 +34,25 @@ type JobRule struct {
ExcludeNodeIDs []string `json:"exclude_nids"`
}
func GetJob(group, id string) (job *Job, err error) {
resp, err := DefalutClient.Get(JobKey(group, id))
if err != nil {
return
}
if resp.Count == 0 {
err = ErrNotFound
return
}
err = json.Unmarshal(resp.Kvs[0].Value, &job)
return
}
func DeleteJob(group, id string) (resp *client.DeleteResponse, err error) {
return DefalutClient.Delete(JobKey(group, id))
}
func GetJobs() (jobs map[string]*Job, err error) {
resp, err := DefalutClient.Get(conf.Config.Cmd, client.WithPrefix())
if err != nil {
@ -98,13 +116,12 @@ func (j *Job) Schedule(id string) ([]string, bool) {
func (j *Job) Run() {
}
var (
ErrEmptyJobName = errors.New("Name of job is empty.")
ErrEmptyJobCommand = errors.New("Command of job is empty.")
)
func JobKey(group, id string) string {
return conf.Config.Cmd + group + "/" + id
}
func (j *Job) Key() string {
return conf.Config.Cmd + j.Group + "/" + j.ID
return JobKey(j.Group, j.ID)
}
func (j *Job) Check() error {

View File

@ -16,7 +16,35 @@ import (
type Job struct{}
func (j *Job) Update(w http.ResponseWriter, r *http.Request) {
func (j *Job) GetJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
job, err := models.GetJob(vars["group"], vars["id"])
var statusCode int
if err != nil {
if err == models.ErrNotFound {
statusCode = http.StatusNotFound
} else {
statusCode = http.StatusInternalServerError
}
outJSONError(w, statusCode, err.Error())
return
}
outJSON(w, job)
}
func (j *Job) DeleteJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := models.DeleteJob(vars["group"], vars["id"])
if err != nil {
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
outJSONWithCode(w, http.StatusNoContent, nil)
}
func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
job := &models.Job{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&job)

View File

@ -89,4 +89,12 @@ func (n *Node) GetGroupByGroupId(w http.ResponseWriter, r *http.Request) {
}
func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := models.DeleteGroupById(vars["id"])
if err != nil {
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
outJSONWithCode(w, http.StatusNoContent, nil)
}

View File

@ -16,14 +16,21 @@ func InitRouters() (s *http.Server, err error) {
r := mux.NewRouter()
subrouter := r.PathPrefix("/v1").Subrouter()
// get a job group list
h := BaseHandler{Handle: jobHandler.GetGroups}
subrouter.Handle("/job/groups", h).Methods("GET")
// get a job group by group name
h = BaseHandler{Handle: jobHandler.GetListByGroupName}
subrouter.Handle("/job/group/{name}", h).Methods("GET")
h = BaseHandler{Handle: jobHandler.Update}
// create/update a job
h = BaseHandler{Handle: jobHandler.UpdateJob}
subrouter.Handle("/job", h).Methods("PUT")
// get a job
h = BaseHandler{Handle: jobHandler.GetJob}
subrouter.Handle("/job/{group}-{id}", h).Methods("GET")
// remove a job
h = BaseHandler{Handle: jobHandler.DeleteJob}
subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE")
// get node group list
h = BaseHandler{Handle: nodeHandler.GetGroups}