pkg: mv models to cronsun

pull/1/head
miraclesu 2017-05-12 14:48:24 +08:00
parent 5a25adf84f
commit dcc1c421eb
26 changed files with 116 additions and 116 deletions

View File

@ -8,9 +8,9 @@ import (
"sunteng/commons/log"
"github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun/node"
)
@ -24,7 +24,7 @@ func main() {
//set cpu usage
runtime.GOMAXPROCS(*gomax)
if err := models.Init(); err != nil {
if err := cronsun.Init(); err != nil {
log.Error(err.Error())
return
}
@ -40,7 +40,7 @@ func main() {
return
}
if err = models.StartProc(); err != nil {
if err = cronsun.StartProc(); err != nil {
log.Warnf("[process key will not timeout]proc lease id set err: %s", err.Error())
}
@ -51,9 +51,9 @@ func main() {
log.Noticef("cronsun %s service started, Ctrl+C or send kill sign to exit", n.String())
// 注册退出事件
event.On(event.EXIT, n.Stop, conf.Exit, models.Exit)
event.On(event.EXIT, n.Stop, conf.Exit, cronsun.Exit)
// 注册监听配置更新事件
event.On(event.WAIT, models.Reload)
event.On(event.WAIT, cronsun.Reload)
// 监听退出信号
event.Wait()
// 处理退出事件

View File

@ -10,12 +10,12 @@ import (
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/web"
)
func main() {
if err := models.Init(); err != nil {
if err := cronsun.Init(); err != nil {
log.Error(err.Error())
return
}
@ -36,19 +36,19 @@ func main() {
}
if conf.Config.Mail.Enable {
var noticer models.Noticer
var noticer cronsun.Noticer
if len(conf.Config.Mail.HttpAPI) > 0 {
noticer = &models.HttpAPI{}
noticer = &cronsun.HttpAPI{}
} else {
mailer, err := models.NewMail(10 * time.Second)
mailer, err := cronsun.NewMail(10 * time.Second)
if err != nil {
log.Error(err.Error())
return
}
noticer = mailer
}
go models.StartNoticer(noticer)
go cronsun.StartNoticer(noticer)
}
go func() {

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"strings"

View File

@ -1,8 +1,8 @@
package models
package cronsun
import (
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models/db"
"github.com/shunfei/cronsun/db"
)
var (

View File

@ -12,7 +12,7 @@ import (
"sunteng/commons/log"
"github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/models/db"
"github.com/shunfei/cronsun/db"
"github.com/shunfei/cronsun/utils"
)

View File

@ -1,4 +1,4 @@
package models
package cronsun
import "errors"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"encoding/hex"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"bytes"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"time"

View File

@ -1,7 +1,7 @@
package models
package cronsun
import (
"github.com/shunfei/cronsun/models/db"
"github.com/shunfei/cronsun/db"
)
var (

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"encoding/json"

View File

@ -1,10 +1,10 @@
package node
import (
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
)
type Groups map[string]*models.Group
type Groups map[string]*cronsun.Group
type jobLink struct {
gname string
@ -39,7 +39,7 @@ func (l link) add(gid, jid, rid, gname string) {
j.rules[rid] = true
}
func (l link) addJob(job *models.Job) {
func (l link) addJob(job *cronsun.Job) {
for _, r := range job.Rules {
for _, gid := range r.GroupIDs {
l.add(gid, job.ID, r.ID, job.Group)
@ -64,7 +64,7 @@ func (l link) del(gid, jid, rid string) {
}
}
func (l link) delJob(job *models.Job) {
func (l link) delJob(job *cronsun.Job) {
for _, r := range job.Rules {
for _, gid := range r.GroupIDs {
l.delGroupJob(gid, job.ID)

View File

@ -1,7 +1,7 @@
package node
import (
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
)
type Jobs map[string]*models.Job
type Jobs map[string]*cronsun.Job

View File

@ -11,20 +11,20 @@ import (
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/node/cron"
"github.com/shunfei/cronsun/utils"
)
// Node 执行 cron 命令服务的结构体
type Node struct {
*models.Client
*models.Node
*cronsun.Client
*cronsun.Node
*cron.Cron
jobs Jobs // 和结点相关的任务
groups Groups
cmds map[string]*models.Cmd
cmds map[string]*cronsun.Cmd
link
// 删除的 job id用于 group 更新
@ -42,15 +42,15 @@ func NewNode(cfg *conf.Conf) (n *Node, err error) {
}
n = &Node{
Client: models.DefalutClient,
Node: &models.Node{
Client: cronsun.DefalutClient,
Node: &cronsun.Node{
ID: ip.String(),
PID: strconv.Itoa(os.Getpid()),
},
Cron: cron.New(),
jobs: make(Jobs, 8),
cmds: make(map[string]*models.Cmd),
cmds: make(map[string]*cronsun.Cmd),
link: newLink(8),
delIDs: make(map[string]bool, 8),
@ -120,11 +120,11 @@ func (n *Node) keepAlive() {
}
func (n *Node) loadJobs() (err error) {
if n.groups, err = models.GetGroups(""); err != nil {
if n.groups, err = cronsun.GetGroups(""); err != nil {
return
}
jobs, err := models.GetJobs()
jobs, err := cronsun.GetJobs()
if err != nil {
return
}
@ -141,7 +141,7 @@ func (n *Node) loadJobs() (err error) {
return
}
func (n *Node) addJob(job *models.Job, notice bool) {
func (n *Node) addJob(job *cronsun.Job, notice bool) {
n.link.addJob(job)
if job.IsRunOn(n.ID, n.groups) {
n.jobs[job.ID] = job
@ -180,7 +180,7 @@ func (n *Node) delJob(id string) {
return
}
func (n *Node) modJob(job *models.Job) {
func (n *Node) modJob(job *cronsun.Job) {
oJob, ok := n.jobs[job.ID]
// 之前此任务没有在当前结点执行,直接增加任务
if !ok {
@ -205,7 +205,7 @@ func (n *Node) modJob(job *models.Job) {
n.link.addJob(oJob)
}
func (n *Node) addCmd(cmd *models.Cmd, notice bool) {
func (n *Node) addCmd(cmd *cronsun.Cmd, notice bool) {
c, ok := n.cmds[cmd.GetID()]
if ok {
sch := c.JobRule.Timer
@ -230,13 +230,13 @@ func (n *Node) addCmd(cmd *models.Cmd, notice bool) {
return
}
func (n *Node) delCmd(cmd *models.Cmd) {
func (n *Node) delCmd(cmd *cronsun.Cmd) {
delete(n.cmds, cmd.GetID())
n.Cron.DelJob(cmd)
log.Noticef("job[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.JobRule.ID, cmd.JobRule.Timer)
}
func (n *Node) addGroup(g *models.Group) {
func (n *Node) addGroup(g *cronsun.Group) {
n.groups[g.ID] = g
}
@ -261,7 +261,7 @@ func (n *Node) delGroup(id string) {
return
}
func (n *Node) modGroup(g *models.Group) {
func (n *Node) modGroup(g *cronsun.Group) {
oGroup, ok := n.groups[g.ID]
if !ok {
n.addGroup(g)
@ -285,7 +285,7 @@ func (n *Node) modGroup(g *models.Group) {
return
}
func (n *Node) groupAddNode(g *models.Group) {
func (n *Node) groupAddNode(g *cronsun.Group) {
n.groups[g.ID] = g
jls := n.link[g.ID]
if len(jls) == 0 {
@ -302,7 +302,7 @@ func (n *Node) groupAddNode(g *models.Group) {
continue
}
if job, err = models.GetJob(jl.gname, jid); err != nil {
if job, err = cronsun.GetJob(jl.gname, jid); err != nil {
log.Warnf("get job[%s][%s] err: %s", jl.gname, jid, err.Error())
n.link.delGroupJob(g.ID, jid)
continue
@ -317,7 +317,7 @@ func (n *Node) groupAddNode(g *models.Group) {
return
}
func (n *Node) groupRmNode(g, og *models.Group) {
func (n *Node) groupRmNode(g, og *cronsun.Group) {
jls := n.link[g.ID]
if len(jls) == 0 {
n.groups[g.ID] = g
@ -351,12 +351,12 @@ func (n *Node) groupRmNode(g, og *models.Group) {
}
func (n *Node) watchJobs() {
rch := models.WatchJobs()
rch := cronsun.WatchJobs()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsCreate():
job, err := models.GetJobFromKv(ev.Kv)
job, err := cronsun.GetJobFromKv(ev.Kv)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
@ -365,7 +365,7 @@ func (n *Node) watchJobs() {
job.RunOn(n.ID)
n.addJob(job, true)
case ev.IsModify():
job, err := models.GetJobFromKv(ev.Kv)
job, err := cronsun.GetJobFromKv(ev.Kv)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
@ -374,7 +374,7 @@ func (n *Node) watchJobs() {
job.RunOn(n.ID)
n.modJob(job)
case ev.Type == client.EventTypeDelete:
n.delJob(models.GetIDFromKey(string(ev.Kv.Key)))
n.delJob(cronsun.GetIDFromKey(string(ev.Kv.Key)))
default:
log.Warnf("unknown event type[%v] from job[%s]", ev.Type, string(ev.Kv.Key))
}
@ -383,12 +383,12 @@ func (n *Node) watchJobs() {
}
func (n *Node) watchGroups() {
rch := models.WatchGroups()
rch := cronsun.WatchGroups()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsCreate():
g, err := models.GetGroupFromKv(ev.Kv)
g, err := cronsun.GetGroupFromKv(ev.Kv)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
@ -396,7 +396,7 @@ func (n *Node) watchGroups() {
n.addGroup(g)
case ev.IsModify():
g, err := models.GetGroupFromKv(ev.Kv)
g, err := cronsun.GetGroupFromKv(ev.Kv)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
@ -404,7 +404,7 @@ func (n *Node) watchGroups() {
n.modGroup(g)
case ev.Type == client.EventTypeDelete:
n.delGroup(models.GetIDFromKey(string(ev.Kv.Key)))
n.delGroup(cronsun.GetIDFromKey(string(ev.Kv.Key)))
default:
log.Warnf("unknown event type[%v] from group[%s]", ev.Type, string(ev.Kv.Key))
}
@ -413,7 +413,7 @@ func (n *Node) watchGroups() {
}
func (n *Node) watchOnce() {
rch := models.WatchOnce()
rch := cronsun.WatchOnce()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
@ -422,7 +422,7 @@ func (n *Node) watchOnce() {
continue
}
job, ok := n.jobs[models.GetIDFromKey(string(ev.Kv.Key))]
job, ok := n.jobs[cronsun.GetIDFromKey(string(ev.Kv.Key))]
if !ok || !job.IsRunOn(n.ID, n.groups) {
continue
}

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"bytes"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
client "github.com/coreos/etcd/clientv3"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"fmt"

View File

@ -1,4 +1,4 @@
package models
package cronsun
import (
"fmt"

View File

@ -7,7 +7,7 @@ import (
v3 "github.com/coreos/etcd/clientv3"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
)
type Info struct{}
@ -15,14 +15,14 @@ type Info struct{}
func (inf *Info) Overview(w http.ResponseWriter, r *http.Request) {
var overview = struct {
TotalJobs int64 `json:"totalJobs"`
JobExecuted *models.StatExecuted `json:"jobExecuted"`
JobExecutedDaily *models.StatExecuted `json:"jobExecutedDaily"`
JobExecuted *cronsun.StatExecuted `json:"jobExecuted"`
JobExecutedDaily *cronsun.StatExecuted `json:"jobExecutedDaily"`
}{}
overview.JobExecuted, _ = models.JobLogStat()
overview.JobExecutedDaily, _ = models.JobLogDayStat(time.Now())
overview.JobExecuted, _ = cronsun.JobLogStat()
overview.JobExecutedDaily, _ = cronsun.JobLogDayStat(time.Now())
gresp, err := models.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix(), v3.WithCountOnly())
gresp, err := cronsun.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix(), v3.WithCountOnly())
if err == nil {
overview.TotalJobs = gresp.Count
}

View File

@ -11,7 +11,7 @@ import (
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
"time"
)
@ -19,10 +19,10 @@ type Job struct{}
func (j *Job) GetJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
job, err := models.GetJob(vars["group"], vars["id"])
job, err := cronsun.GetJob(vars["group"], vars["id"])
var statusCode int
if err != nil {
if err == models.ErrNotFound {
if err == cronsun.ErrNotFound {
statusCode = http.StatusNotFound
} else {
statusCode = http.StatusInternalServerError
@ -36,7 +36,7 @@ func (j *Job) GetJob(w http.ResponseWriter, r *http.Request) {
func (j *Job) DeleteJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := models.DeleteJob(vars["group"], vars["id"])
_, err := cronsun.DeleteJob(vars["group"], vars["id"])
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -46,7 +46,7 @@ func (j *Job) DeleteJob(w http.ResponseWriter, r *http.Request) {
}
func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
job := &models.Job{}
job := &cronsun.Job{}
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&job)
if err != nil {
@ -56,7 +56,7 @@ func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
r.Body.Close()
vars := mux.Vars(r)
originJob, rev, err := models.GetJobAndRev(vars["group"], vars["id"])
originJob, rev, err := cronsun.GetJobAndRev(vars["group"], vars["id"])
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -69,7 +69,7 @@ func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
return
}
_, err = models.DefalutClient.PutWithModRev(originJob.Key(), string(b), rev)
_, err = cronsun.DefalutClient.PutWithModRev(originJob.Key(), string(b), rev)
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -80,7 +80,7 @@ func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
var job = &struct {
*models.Job
*cronsun.Job
OldGroup string `json:"oldGroup"`
}{}
@ -101,11 +101,11 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
var successCode = http.StatusOK
if len(job.ID) == 0 {
successCode = http.StatusCreated
job.ID = models.NextID()
job.ID = cronsun.NextID()
} else {
job.OldGroup = strings.TrimSpace(job.OldGroup)
if job.OldGroup != job.Group {
deleteOldKey = models.JobKey(job.OldGroup, job.ID)
deleteOldKey = cronsun.JobKey(job.OldGroup, job.ID)
}
}
@ -115,7 +115,7 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
return
}
_, err = models.DefalutClient.Put(job.Key(), string(b))
_, err = cronsun.DefalutClient.Put(job.Key(), string(b))
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -123,7 +123,7 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
// remove old key
if len(deleteOldKey) > 0 {
if _, err = models.DefalutClient.Delete(deleteOldKey); err != nil {
if _, err = cronsun.DefalutClient.Delete(deleteOldKey); err != nil {
log.Errorf("failed to remove old job key[%s], err: %s.", deleteOldKey, err.Error())
}
}
@ -132,7 +132,7 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
}
func (j *Job) GetGroups(w http.ResponseWriter, r *http.Request) {
resp, err := models.DefalutClient.Get(conf.Config.Cmd, clientv3.WithPrefix(), clientv3.WithKeysOnly())
resp, err := cronsun.DefalutClient.Get(conf.Config.Cmd, clientv3.WithPrefix(), clientv3.WithKeysOnly())
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -164,24 +164,24 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
}
type jobStatus struct {
*models.Job
LatestStatus *models.JobLatestLog `json:"latestStatus"`
*cronsun.Job
LatestStatus *cronsun.JobLatestLog `json:"latestStatus"`
}
resp, err := models.DefalutClient.Get(prefix, clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
resp, err := cronsun.DefalutClient.Get(prefix, clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend))
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
var nodeGroupMap map[string]*models.Group
var nodeGroupMap map[string]*cronsun.Group
if len(node) > 0 {
nodeGrouplist, err := models.GetNodeGroups()
nodeGrouplist, err := cronsun.GetNodeGroups()
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
nodeGroupMap = map[string]*models.Group{}
nodeGroupMap = map[string]*cronsun.Group{}
for i := range nodeGrouplist {
nodeGroupMap[nodeGrouplist[i].ID] = nodeGrouplist[i]
}
@ -190,7 +190,7 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
var jobIds []string
var jobList = make([]*jobStatus, 0, resp.Count)
for i := range resp.Kvs {
job := models.Job{}
job := cronsun.Job{}
err = json.Unmarshal(resp.Kvs[i].Value, &job)
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
@ -204,7 +204,7 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
jobIds = append(jobIds, job.ID)
}
m, err := models.GetJobLatestLogListByJobIds(jobIds)
m, err := cronsun.GetJobLatestLogListByJobIds(jobIds)
if err != nil {
log.Error("GetJobLatestLogListByJobIds error:", err.Error())
} else {
@ -218,10 +218,10 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
func (j *Job) GetJobNodes(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
job, err := models.GetJob(vars["group"], vars["id"])
job, err := cronsun.GetJob(vars["group"], vars["id"])
var statusCode int
if err != nil {
if err == models.ErrNotFound {
if err == cronsun.ErrNotFound {
statusCode = http.StatusNotFound
} else {
statusCode = http.StatusInternalServerError
@ -232,7 +232,7 @@ func (j *Job) GetJobNodes(w http.ResponseWriter, r *http.Request) {
var nodes []string
var exNodes []string
groups, err := models.GetGroups("")
groups, err := cronsun.GetGroups("")
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -263,7 +263,7 @@ func (j *Job) JobExecute(w http.ResponseWriter, r *http.Request) {
}
node := getStringVal("node", r)
err := models.PutOnce(group, id, node)
err := cronsun.PutOnce(group, id, node)
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -279,15 +279,15 @@ func (j *Job) GetExecutingJob(w http.ResponseWriter, r *http.Request) {
JobIds: getStringArrayFromQuery("jobs", ",", r),
}
gresp, err := models.DefalutClient.Get(conf.Config.Proc, clientv3.WithPrefix())
gresp, err := cronsun.DefalutClient.Get(conf.Config.Proc, clientv3.WithPrefix())
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
var list = make([]*models.Process, 0, 8)
var list = make([]*cronsun.Process, 0, 8)
for i := range gresp.Kvs {
proc, err := models.GetProcFromKey(string(gresp.Kvs[i].Key))
proc, err := cronsun.GetProcFromKey(string(gresp.Kvs[i].Key))
if err != nil {
log.Error("Failed to unmarshal Proc from key: ", err.Error())
continue
@ -310,7 +310,7 @@ type ProcFetchOptions struct {
JobIds []string
}
func (opt *ProcFetchOptions) Match(proc *models.Process) bool {
func (opt *ProcFetchOptions) Match(proc *cronsun.Process) bool {
if len(opt.Groups) > 0 && !InStringArray(proc.Group, opt.Groups) {
return false
}
@ -327,7 +327,7 @@ func (opt *ProcFetchOptions) Match(proc *models.Process) bool {
return true
}
type ByProcTime []*models.Process
type ByProcTime []*cronsun.Process
func (a ByProcTime) Len() int { return len(a) }
func (a ByProcTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

View File

@ -10,7 +10,7 @@ import (
"gopkg.in/mgo.v2/bson"
"math"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
)
type JobLog struct{}
@ -28,7 +28,7 @@ func (jl *JobLog) GetDetail(w http.ResponseWriter, r *http.Request) {
return
}
logDetail, err := models.GetJobLogById(bson.ObjectIdHex(id))
logDetail, err := cronsun.GetJobLogById(bson.ObjectIdHex(id))
if err != nil {
statusCode := http.StatusInternalServerError
if err == mgo.ErrNotFound {
@ -90,18 +90,18 @@ func (jl *JobLog) GetList(w http.ResponseWriter, r *http.Request) {
var pager struct {
Total int `json:"total"`
List []*models.JobLog `json:"list"`
List []*cronsun.JobLog `json:"list"`
}
var err error
if r.FormValue("latest") == "true" {
var latestLogList []*models.JobLatestLog
latestLogList, pager.Total, err = models.GetJobLatestLogList(query, page, pageSize, sort)
var latestLogList []*cronsun.JobLatestLog
latestLogList, pager.Total, err = cronsun.GetJobLatestLogList(query, page, pageSize, sort)
for i := range latestLogList {
latestLogList[i].JobLog.Id = bson.ObjectIdHex(latestLogList[i].RefLogId)
pager.List = append(pager.List, &latestLogList[i].JobLog)
}
} else {
pager.List, pager.Total, err = models.GetJobLogList(query, page, pageSize, sort)
pager.List, pager.Total, err = cronsun.GetJobLogList(query, page, pageSize, sort)
}
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())

View File

@ -11,7 +11,7 @@ import (
"fmt"
"sunteng/commons/log"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
)
type Node struct{}
@ -19,7 +19,7 @@ type Node struct{}
var ngKeyDeepLen = len(conf.Config.Group)
func (n *Node) UpdateGroup(w http.ResponseWriter, r *http.Request) {
g := models.Group{}
g := cronsun.Group{}
de := json.NewDecoder(r.Body)
var err error
if err = de.Decode(&g); err != nil {
@ -32,7 +32,7 @@ func (n *Node) UpdateGroup(w http.ResponseWriter, r *http.Request) {
g.ID = strings.TrimSpace(g.ID)
if len(g.ID) == 0 {
successCode = http.StatusCreated
g.ID = models.NextID()
g.ID = cronsun.NextID()
}
if err = g.Check(); err != nil {
@ -51,7 +51,7 @@ func (n *Node) UpdateGroup(w http.ResponseWriter, r *http.Request) {
}
func (n *Node) GetGroups(w http.ResponseWriter, r *http.Request) {
list, err := models.GetNodeGroups()
list, err := cronsun.GetNodeGroups()
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -62,7 +62,7 @@ func (n *Node) GetGroups(w http.ResponseWriter, r *http.Request) {
func (n *Node) GetGroupByGroupId(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
g, err := models.GetGroupById(vars["id"])
g, err := cronsun.GetGroupById(vars["id"])
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
@ -83,13 +83,13 @@ func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
return
}
_, err := models.DeleteGroupById(groupId)
_, err := cronsun.DeleteGroupById(groupId)
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
gresp, err := models.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix())
gresp, err := cronsun.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix())
if err != nil {
errstr := fmt.Sprintf("failed to fetch jobs from etcd after deleted node group[%s]: %s", groupId, err.Error())
log.Error(errstr)
@ -99,7 +99,7 @@ func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
// update rule's node group
for i := range gresp.Kvs {
job := models.Job{}
job := cronsun.Job{}
err = json.Unmarshal(gresp.Kvs[i].Value, &job)
key := string(gresp.Kvs[i].Key)
if err != nil {
@ -127,7 +127,7 @@ func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
log.Errorf("failed to marshal job[%s]: %s", key, err.Error())
continue
}
_, err = models.DefalutClient.PutWithModRev(key, string(v), gresp.Kvs[i].ModRevision)
_, err = cronsun.DefalutClient.PutWithModRev(key, string(v), gresp.Kvs[i].ModRevision)
if err != nil {
log.Errorf("failed to update job[%s]: %s", key, err.Error())
continue
@ -139,13 +139,13 @@ func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
}
func (n *Node) GetNodes(w http.ResponseWriter, r *http.Request) {
nodes, err := models.GetNodes()
nodes, err := cronsun.GetNodes()
if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return
}
gresp, err := models.DefalutClient.Get(conf.Config.Node, v3.WithPrefix(), v3.WithKeysOnly())
gresp, err := cronsun.DefalutClient.Get(conf.Config.Node, v3.WithPrefix(), v3.WithKeysOnly())
if err == nil {
connecedMap := make(map[string]bool, gresp.Count)
for i := range gresp.Kvs {

View File

@ -7,11 +7,11 @@ import (
"github.com/gorilla/mux"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun"
)
func GetVersion(w http.ResponseWriter, r *http.Request) {
outJSON(w, models.Version)
outJSON(w, cronsun.Version)
}
func InitRouters() (s *http.Server, err error) {