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" "sunteng/commons/log"
"github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/event" "github.com/shunfei/cronsun/event"
"github.com/shunfei/cronsun/models"
"github.com/shunfei/cronsun/node" "github.com/shunfei/cronsun/node"
) )
@ -24,7 +24,7 @@ func main() {
//set cpu usage //set cpu usage
runtime.GOMAXPROCS(*gomax) runtime.GOMAXPROCS(*gomax)
if err := models.Init(); err != nil { if err := cronsun.Init(); err != nil {
log.Error(err.Error()) log.Error(err.Error())
return return
} }
@ -40,7 +40,7 @@ func main() {
return 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()) 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()) 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() event.Wait()
// 处理退出事件 // 处理退出事件

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,10 +1,10 @@
package node package node
import ( 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 { type jobLink struct {
gname string gname string
@ -39,7 +39,7 @@ func (l link) add(gid, jid, rid, gname string) {
j.rules[rid] = true j.rules[rid] = true
} }
func (l link) addJob(job *models.Job) { func (l link) addJob(job *cronsun.Job) {
for _, r := range job.Rules { for _, r := range job.Rules {
for _, gid := range r.GroupIDs { for _, gid := range r.GroupIDs {
l.add(gid, job.ID, r.ID, job.Group) 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 _, r := range job.Rules {
for _, gid := range r.GroupIDs { for _, gid := range r.GroupIDs {
l.delGroupJob(gid, job.ID) l.delGroupJob(gid, job.ID)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@ import (
"sunteng/commons/log" "sunteng/commons/log"
"github.com/shunfei/cronsun/conf" "github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/models" "github.com/shunfei/cronsun"
"time" "time"
) )
@ -19,10 +19,10 @@ type Job struct{}
func (j *Job) GetJob(w http.ResponseWriter, r *http.Request) { func (j *Job) GetJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
job, err := models.GetJob(vars["group"], vars["id"]) job, err := cronsun.GetJob(vars["group"], vars["id"])
var statusCode int var statusCode int
if err != nil { if err != nil {
if err == models.ErrNotFound { if err == cronsun.ErrNotFound {
statusCode = http.StatusNotFound statusCode = http.StatusNotFound
} else { } else {
statusCode = http.StatusInternalServerError 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) { func (j *Job) DeleteJob(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
_, err := models.DeleteJob(vars["group"], vars["id"]) _, err := cronsun.DeleteJob(vars["group"], vars["id"])
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return 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) { func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
job := &models.Job{} job := &cronsun.Job{}
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&job) err := decoder.Decode(&job)
if err != nil { if err != nil {
@ -56,7 +56,7 @@ func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
r.Body.Close() r.Body.Close()
vars := mux.Vars(r) 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 { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
@ -69,7 +69,7 @@ func (j *Job) ChangeJobStatus(w http.ResponseWriter, r *http.Request) {
return return
} }
_, err = models.DefalutClient.PutWithModRev(originJob.Key(), string(b), rev) _, err = cronsun.DefalutClient.PutWithModRev(originJob.Key(), string(b), rev)
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return 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) { func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
var job = &struct { var job = &struct {
*models.Job *cronsun.Job
OldGroup string `json:"oldGroup"` OldGroup string `json:"oldGroup"`
}{} }{}
@ -101,11 +101,11 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
var successCode = http.StatusOK var successCode = http.StatusOK
if len(job.ID) == 0 { if len(job.ID) == 0 {
successCode = http.StatusCreated successCode = http.StatusCreated
job.ID = models.NextID() job.ID = cronsun.NextID()
} else { } else {
job.OldGroup = strings.TrimSpace(job.OldGroup) job.OldGroup = strings.TrimSpace(job.OldGroup)
if job.OldGroup != job.Group { 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 return
} }
_, err = models.DefalutClient.Put(job.Key(), string(b)) _, err = cronsun.DefalutClient.Put(job.Key(), string(b))
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
@ -123,7 +123,7 @@ func (j *Job) UpdateJob(w http.ResponseWriter, r *http.Request) {
// remove old key // remove old key
if len(deleteOldKey) > 0 { 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()) 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) { 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 { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
@ -164,24 +164,24 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
} }
type jobStatus struct { type jobStatus struct {
*models.Job *cronsun.Job
LatestStatus *models.JobLatestLog `json:"latestStatus"` 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 { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
} }
var nodeGroupMap map[string]*models.Group var nodeGroupMap map[string]*cronsun.Group
if len(node) > 0 { if len(node) > 0 {
nodeGrouplist, err := models.GetNodeGroups() nodeGrouplist, err := cronsun.GetNodeGroups()
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
} }
nodeGroupMap = map[string]*models.Group{} nodeGroupMap = map[string]*cronsun.Group{}
for i := range nodeGrouplist { for i := range nodeGrouplist {
nodeGroupMap[nodeGrouplist[i].ID] = nodeGrouplist[i] nodeGroupMap[nodeGrouplist[i].ID] = nodeGrouplist[i]
} }
@ -190,7 +190,7 @@ func (j *Job) GetList(w http.ResponseWriter, r *http.Request) {
var jobIds []string var jobIds []string
var jobList = make([]*jobStatus, 0, resp.Count) var jobList = make([]*jobStatus, 0, resp.Count)
for i := range resp.Kvs { for i := range resp.Kvs {
job := models.Job{} job := cronsun.Job{}
err = json.Unmarshal(resp.Kvs[i].Value, &job) err = json.Unmarshal(resp.Kvs[i].Value, &job)
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) 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) jobIds = append(jobIds, job.ID)
} }
m, err := models.GetJobLatestLogListByJobIds(jobIds) m, err := cronsun.GetJobLatestLogListByJobIds(jobIds)
if err != nil { if err != nil {
log.Error("GetJobLatestLogListByJobIds error:", err.Error()) log.Error("GetJobLatestLogListByJobIds error:", err.Error())
} else { } 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) { func (j *Job) GetJobNodes(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
job, err := models.GetJob(vars["group"], vars["id"]) job, err := cronsun.GetJob(vars["group"], vars["id"])
var statusCode int var statusCode int
if err != nil { if err != nil {
if err == models.ErrNotFound { if err == cronsun.ErrNotFound {
statusCode = http.StatusNotFound statusCode = http.StatusNotFound
} else { } else {
statusCode = http.StatusInternalServerError statusCode = http.StatusInternalServerError
@ -232,7 +232,7 @@ func (j *Job) GetJobNodes(w http.ResponseWriter, r *http.Request) {
var nodes []string var nodes []string
var exNodes []string var exNodes []string
groups, err := models.GetGroups("") groups, err := cronsun.GetGroups("")
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
@ -263,7 +263,7 @@ func (j *Job) JobExecute(w http.ResponseWriter, r *http.Request) {
} }
node := getStringVal("node", r) node := getStringVal("node", r)
err := models.PutOnce(group, id, node) err := cronsun.PutOnce(group, id, node)
if err != nil { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
@ -279,15 +279,15 @@ func (j *Job) GetExecutingJob(w http.ResponseWriter, r *http.Request) {
JobIds: getStringArrayFromQuery("jobs", ",", r), 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 { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())
return return
} }
var list = make([]*models.Process, 0, 8) var list = make([]*cronsun.Process, 0, 8)
for i := range gresp.Kvs { 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 { if err != nil {
log.Error("Failed to unmarshal Proc from key: ", err.Error()) log.Error("Failed to unmarshal Proc from key: ", err.Error())
continue continue
@ -310,7 +310,7 @@ type ProcFetchOptions struct {
JobIds []string 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) { if len(opt.Groups) > 0 && !InStringArray(proc.Group, opt.Groups) {
return false return false
} }
@ -327,7 +327,7 @@ func (opt *ProcFetchOptions) Match(proc *models.Process) bool {
return true return true
} }
type ByProcTime []*models.Process type ByProcTime []*cronsun.Process
func (a ByProcTime) Len() int { return len(a) } func (a ByProcTime) Len() int { return len(a) }
func (a ByProcTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 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" "gopkg.in/mgo.v2/bson"
"math" "math"
"github.com/shunfei/cronsun/models" "github.com/shunfei/cronsun"
) )
type JobLog struct{} type JobLog struct{}
@ -28,7 +28,7 @@ func (jl *JobLog) GetDetail(w http.ResponseWriter, r *http.Request) {
return return
} }
logDetail, err := models.GetJobLogById(bson.ObjectIdHex(id)) logDetail, err := cronsun.GetJobLogById(bson.ObjectIdHex(id))
if err != nil { if err != nil {
statusCode := http.StatusInternalServerError statusCode := http.StatusInternalServerError
if err == mgo.ErrNotFound { if err == mgo.ErrNotFound {
@ -90,18 +90,18 @@ func (jl *JobLog) GetList(w http.ResponseWriter, r *http.Request) {
var pager struct { var pager struct {
Total int `json:"total"` Total int `json:"total"`
List []*models.JobLog `json:"list"` List []*cronsun.JobLog `json:"list"`
} }
var err error var err error
if r.FormValue("latest") == "true" { if r.FormValue("latest") == "true" {
var latestLogList []*models.JobLatestLog var latestLogList []*cronsun.JobLatestLog
latestLogList, pager.Total, err = models.GetJobLatestLogList(query, page, pageSize, sort) latestLogList, pager.Total, err = cronsun.GetJobLatestLogList(query, page, pageSize, sort)
for i := range latestLogList { for i := range latestLogList {
latestLogList[i].JobLog.Id = bson.ObjectIdHex(latestLogList[i].RefLogId) latestLogList[i].JobLog.Id = bson.ObjectIdHex(latestLogList[i].RefLogId)
pager.List = append(pager.List, &latestLogList[i].JobLog) pager.List = append(pager.List, &latestLogList[i].JobLog)
} }
} else { } 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 { if err != nil {
outJSONWithCode(w, http.StatusInternalServerError, err.Error()) outJSONWithCode(w, http.StatusInternalServerError, err.Error())

View File

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

View File

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