diff --git a/conf/conf.go b/conf/conf.go index 83ad407..847f05d 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -7,6 +7,7 @@ import ( client "github.com/coreos/etcd/clientv3" "sunteng/commons/confutil" + "sunteng/commons/db/imgo" "sunteng/commons/log" "sunteng/commons/util" ) @@ -56,6 +57,7 @@ type Conf struct { Log log.Config Etcd client.Config + Mgo *imgo.Config Web webConfig } diff --git a/conf/files/db.json.sample b/conf/files/db.json.sample new file mode 100644 index 0000000..4d6e6a1 --- /dev/null +++ b/conf/files/db.json.sample @@ -0,0 +1,6 @@ +{ + "Mgo": { + "Host": "192.168.11.16:3000", + "Database": "cronsun" + } +} \ No newline at end of file diff --git a/models/client.go b/models/client.go index 45c5542..9c3048c 100644 --- a/models/client.go +++ b/models/client.go @@ -13,31 +13,8 @@ import ( var ( DefalutClient *Client - - initialized bool ) -func Init() (err error) { - if initialized { - return - } - - if err = initID(); err != nil { - return - } - - if err = conf.Init(); err != nil { - return - } - - if DefalutClient, err = NewClient(conf.Config); err != nil { - return - } - - initialized = true - return -} - type Client struct { *client.Client diff --git a/models/common.go b/models/common.go new file mode 100644 index 0000000..a4e910d --- /dev/null +++ b/models/common.go @@ -0,0 +1,38 @@ +package models + +import ( + "sunteng/commons/db/imgo" + + "sunteng/cronsun/conf" +) + +var ( + initialized bool +) + +func Init() (err error) { + if initialized { + return + } + + // init id creator + if err = initID(); err != nil { + return + } + + // init config + if err = conf.Init(); err != nil { + return + } + + // init etcd client + if DefalutClient, err = NewClient(conf.Config); err != nil { + return + } + + // init mongoDB + mgoDB = imgo.NewMdbWithConf(conf.Config.Mgo) + + initialized = true + return +} diff --git a/models/job.go b/models/job.go index e6ad79a..7b44e78 100644 --- a/models/job.go +++ b/models/job.go @@ -160,14 +160,16 @@ func (j *Job) String() string { return string(data) } +// Run 执行任务 func (j *Job) Run() { cmd := strings.Split(j.Command, " ") out, err := exec.Command(cmd[0], cmd[1:]...).Output() if err != nil { + j.fail(err) return } - fmt.Printf("%s\n", out) + j.success(out) } func JobKey(group, id string) string { @@ -205,3 +207,12 @@ func (j *Job) Check() error { return nil } + +// 执行结果写入 mongoDB +func (j *Job) success(out []byte) { + +} + +func (j *Job) fail(err error) { + +} diff --git a/models/mdb.go b/models/mdb.go new file mode 100644 index 0000000..3c9191b --- /dev/null +++ b/models/mdb.go @@ -0,0 +1,13 @@ +package models + +import ( + "sunteng/commons/db/imgo" +) + +var ( + mgoDB *imgo.Mdb +) + +func GetDb() *imgo.Mdb { + return mgoDB +}