From a0a3bea743f38ba91a5aa492527818d95561df47 Mon Sep 17 00:00:00 2001 From: ryanmao Date: Sat, 3 Oct 2020 22:28:16 +0800 Subject: [PATCH] runs ok --- comm/db.go | 5 +- doc/sys.db | Bin 65536 -> 65536 bytes doc/sys.sql | 5 +- mgr/exec.go | 11 ++ mgr/run.go | 109 +++++++++---- model/t_model.go | 1 + models/model.go | 3 +- models/model_run.go | 1 + models/plug_run.go | 37 +++++ route/route.go | 3 + route/server/model.go | 14 ++ route/server/plugin.go | 54 +++++++ service/dbService/model.go | 14 ++ service/dbService/plugin.go | 14 ++ uis/vue-admin/src/routes.js | 3 +- uis/vue-admin/src/views/base/List.vue | 1 + uis/vue-admin/src/views/ruis/ModelForm.vue | 12 +- uis/vue-admin/src/views/ruis/ModelList.vue | 9 +- uis/vue-admin/src/views/ruis/PlugList.vue | 12 +- uis/vue-admin/src/views/ruis/PlugRunList.vue | 158 +++++++++++++++++++ uis/vue-admin/src/views/ruis/RunList.vue | 51 +++++- 21 files changed, 465 insertions(+), 52 deletions(-) create mode 100644 models/plug_run.go create mode 100644 uis/vue-admin/src/views/ruis/PlugRunList.vue diff --git a/comm/db.go b/comm/db.go index 2997360..4b62d5e 100644 --- a/comm/db.go +++ b/comm/db.go @@ -40,7 +40,7 @@ const sqls = ` Target Server Version : 3030001 File Encoding : 65001 - Date: 02/10/2020 16:02:56 + Date: 02/10/2020 22:40:17 */ PRAGMA foreign_keys = false; @@ -99,7 +99,8 @@ CREATE TABLE "t_model" ( "times" datetime, "del" integer DEFAULT 0, "envs" text, - "wrkdir" text + "wrkdir" text, + "clrdir" integer DEFAULT 0 ); -- ---------------------------- diff --git a/doc/sys.db b/doc/sys.db index 657bf7be1d220d327c2de9cf2849f4f415d73d91..404c18344476555de3529940bb493139aad822c3 100644 GIT binary patch delta 163 zcmZo@U}g6LpNWJsI?@Kk~AkW#DCMV&?nK*URU~`-A5f zt02n}E@{qkwiC>q?9-T?a~Py1I*Lv1%hzC( K-Ml^Da{>T3oiDTi delta 163 zcmZo@U}qtoPZcu}@>}+$<>Yosot06r;n&i7bqpJDJ^?B!z(HAc4j-b#`%S zX~t&d$vsSWnYc7JpJ4Nh9*e#%5gsmXo$8jMn#x959K0000X BDpmjh diff --git a/doc/sys.sql b/doc/sys.sql index 3fc0451..4a3d84a 100644 --- a/doc/sys.sql +++ b/doc/sys.sql @@ -10,7 +10,7 @@ Target Server Version : 3030001 File Encoding : 65001 - Date: 02/10/2020 16:02:56 + Date: 02/10/2020 22:40:17 */ PRAGMA foreign_keys = false; @@ -69,7 +69,8 @@ CREATE TABLE "t_model" ( "times" datetime, "del" integer DEFAULT 0, "envs" text, - "wrkdir" text + "wrkdir" text, + "clrdir" integer DEFAULT 0 ); -- ---------------------------- diff --git a/mgr/exec.go b/mgr/exec.go index 10e8e14..7121c34 100644 --- a/mgr/exec.go +++ b/mgr/exec.go @@ -85,3 +85,14 @@ func (c *execManager) StopTask(id int) { //_, err := comm.Db.Cols("state").Where("id=?", v.Id).Update(v) //return err } +func (c *execManager) TaskRun(id int) { + c.lk.Lock() + defer c.lk.Unlock() + e, ok := c.tasks[id] + if ok { + if e.cncl == nil { + + } + } + +} diff --git a/mgr/run.go b/mgr/run.go index d728af5..2f27539 100644 --- a/mgr/run.go +++ b/mgr/run.go @@ -4,10 +4,13 @@ import ( "bytes" "context" "errors" + "fmt" "gokins/comm" "gokins/model" "gokins/service/dbService" + "os" "os/exec" + "path/filepath" "runtime" "strings" "time" @@ -35,34 +38,61 @@ func (c *RunTask) start() { return } c.ctx, c.cncl = context.WithCancel(context.Background()) - go func() { - defer ruisUtil.Recovers("RunTask start", nil) - c.plugs = nil - err := comm.Db.Where("del!='1' and tid=?", c.Mr.Tid).OrderBy("sort ASC,id ASC").Find(&c.plugs) - if err != nil { - c.end(2, "db err:"+err.Error()) - return - } - for _, v := range c.plugs { - select { - case <-c.ctx.Done(): - c.end(-1, "手动停止") - return - default: - err := c.run(v) - if err != nil { - println("cmd run err:", err.Error()) - c.end(2, err.Error()) - return - } - time.Sleep(time.Second) - } - } - c.end(4, "") - }() + go c.run() } -func (c *RunTask) run(pgn *model.TPlugin) (rterr error) { +func (c *RunTask) run() { + defer ruisUtil.Recovers("RunTask start", nil) + c.plugs = nil + if c.Md.Wrkdir != "" { + if ruisIo.PathExists(c.Md.Wrkdir) { + if c.Md.Clrdir == 1 { + err := rmDirFiles(c.Md.Wrkdir) + if err != nil { + c.end(2, "运行目录创建失败:"+err.Error()) + return + } + } + } else { + if c.Md.Clrdir != 1 { + c.end(2, "运行目录不存在") + return + } + err := os.MkdirAll(c.Md.Wrkdir, 0755) + if err != nil { + c.end(2, "运行目录创建失败:"+err.Error()) + return + } + } + } + err := comm.Db.Where("del!='1' and tid=?", c.Mr.Tid).OrderBy("sort ASC,id ASC").Find(&c.plugs) + if err != nil { + c.end(2, "db err:"+err.Error()) + return + } + if len(c.plugs) <= 0 { + c.end(2, "无插件") + return + } + for _, v := range c.plugs { + select { + case <-c.ctx.Done(): + c.end(-1, "手动停止") + return + default: + err := c.runs(v) + if err != nil { + println("cmd run err:", err.Error()) + c.end(2, err.Error()) + return + } + time.Sleep(time.Second) + } + } + c.end(4, "") +} + +func (c *RunTask) runs(pgn *model.TPlugin) (rterr error) { defer ruisUtil.Recovers("RunTask.run", func(errs string) { rterr = errors.New(errs) }) @@ -95,14 +125,14 @@ func (c *RunTask) run(pgn *model.TPlugin) (rterr error) { cmd := exec.CommandContext(c.ctx, name, par0, pgn.Cont) cmd.Stdout = c.stdout cmd.Stderr = c.stdout - if c.Md.Wrkdir != "" && ruisIo.PathExists(c.Md.Wrkdir) { - cmd.Dir = c.Md.Wrkdir - } if c.Md.Envs != "" { str := strings.ReplaceAll(c.Md.Envs, "\t", "") envs := strings.Split(str, "\n") cmd.Env = envs } + if c.Md.Wrkdir != "" { + cmd.Dir = c.Md.Wrkdir + } err := cmd.Run() rn.State = 4 if err != nil { @@ -120,7 +150,7 @@ func (c *RunTask) run(pgn *model.TPlugin) (rterr error) { return err } if pgn.Exend == 1 && rn.Excode != 0 { - return errors.New("cmd exit err") + return fmt.Errorf("程序执行错误:%d", rn.Excode) } return nil } @@ -142,3 +172,22 @@ func (c *RunTask) stop() { c.cncl = nil } } + +func rmDirFiles(dir string) error { + d, err := os.Open(dir) + if err != nil { + return err + } + defer d.Close() + names, err := d.Readdirnames(0) + if err != nil { + return err + } + for _, name := range names { + err = os.RemoveAll(filepath.Join(dir, name)) + if err != nil { + return err + } + } + return nil +} diff --git a/model/t_model.go b/model/t_model.go index fe64d65..5a6c927 100644 --- a/model/t_model.go +++ b/model/t_model.go @@ -13,6 +13,7 @@ type TModel struct { Del int Envs string Wrkdir string + Clrdir int } type TModelRun struct { diff --git a/models/model.go b/models/model.go index ca4498f..a0abf13 100644 --- a/models/model.go +++ b/models/model.go @@ -14,6 +14,7 @@ type Model struct { Times time.Time Envs string Wrkdir string + Clrdir int } func (Model) TableName() string { @@ -23,7 +24,7 @@ func (Model) TableName() string { func (c *Model) Save() error { var err error if c.Id > 0 { - _, err = comm.Db.Cols("title", "desc", "envs", "wrkdir").Where("id=?", c.Id).Update(c) + _, err = comm.Db.Cols("title", "desc", "envs", "wrkdir", "clrdir").Where("id=?", c.Id).Update(c) } else { c.Times = time.Now() _, err = comm.Db.Insert(c) diff --git a/models/model_run.go b/models/model_run.go index 05d80fa..805b3b5 100644 --- a/models/model_run.go +++ b/models/model_run.go @@ -13,6 +13,7 @@ type ModelRun struct { Times time.Time Timesd time.Time State int + Errs string Nick string `xorm:"-"` Times1 string `xorm:"-"` diff --git a/models/plug_run.go b/models/plug_run.go new file mode 100644 index 0000000..bce5c59 --- /dev/null +++ b/models/plug_run.go @@ -0,0 +1,37 @@ +package models + +import ( + "fmt" + "gokins/service/dbService" + "time" +) + +type PluginUI struct { + Id int `xorm:"pk autoincr"` + Tid int //model id + Type int + Title string + Times time.Time + Sort int + Exend int + + RunStat int `xorm:"-"` + Hstm string `xorm:"-"` +} + +func (PluginUI) TableName() string { + return "t_plugin" +} + +func (c *PluginUI) ToUI(mrid int) { + run := dbService.FindPluginRun(c.Tid, mrid, c.Id) + if run != nil { + c.RunStat = run.State + c.Hstm = "0" + if run.State >= 2 { + c.Hstm = fmt.Sprintf("%.3f", run.Timesd.Sub(run.Times).Seconds()) + } else if run.State == 1 { + c.Hstm = fmt.Sprintf("%.3f", time.Since(run.Times).Seconds()) + } + } +} diff --git a/route/route.go b/route/route.go index 65a7299..2fa810a 100644 --- a/route/route.go +++ b/route/route.go @@ -18,6 +18,7 @@ func Init() { gpModel := comm.Gin.Group("/model") gpModel.Use(utilService.MidNeedLogin) + gpModel.Any("/get", core.GinHandler(server.ModelGet)) gpModel.Any("/list", core.GinHandler(server.ModelList)) gpModel.Any("/edit", core.GinHandler(server.ModelEdit)) gpModel.Any("/del", core.GinHandler(server.ModelDel)) @@ -30,4 +31,6 @@ func Init() { gpPlug.Any("/list", core.GinHandler(server.PlugList)) gpPlug.Any("/edit", core.GinHandler(server.PlugEdit)) gpPlug.Any("/del", core.GinHandler(server.PlugDel)) + gpPlug.Any("/runs", core.GinHandler(server.PlugRuns)) + gpPlug.Any("/log", core.GinHandler(server.PlugLog)) } diff --git a/route/server/model.go b/route/server/model.go index 26d76e7..d9e4b3c 100644 --- a/route/server/model.go +++ b/route/server/model.go @@ -7,12 +7,26 @@ import ( "gokins/mgr" "gokins/model" "gokins/models" + "gokins/service/dbService" "gokins/service/utilService" "github.com/gin-gonic/gin" ruisUtil "github.com/mgr9525/go-ruisutil" ) +func ModelGet(c *gin.Context, req *ruisUtil.Map) { + id, err := req.GetInt("id") + if err != nil || id <= 0 { + c.String(500, "param err") + return + } + m := dbService.GetModel(int(id)) + if m == nil { + c.String(404, "not found") + return + } + c.JSON(200, m) +} func ModelList(c *gin.Context, req *ruisUtil.Map) { pg, _ := req.GetInt("page") q := req.GetString("q") diff --git a/route/server/plugin.go b/route/server/plugin.go index 637194e..1099862 100644 --- a/route/server/plugin.go +++ b/route/server/plugin.go @@ -5,6 +5,7 @@ import ( "gokins/comm" "gokins/model" "gokins/models" + "gokins/service/dbService" "github.com/gin-gonic/gin" ruisUtil "github.com/mgr9525/go-ruisutil" @@ -49,3 +50,56 @@ func PlugDel(c *gin.Context, req *ruisUtil.Map) { } c.String(200, fmt.Sprintf("%d", m.Id)) } +func PlugRuns(c *gin.Context, req *ruisUtil.Map) { + id, err := req.GetInt("id") + if err != nil || id <= 0 { + c.String(500, "param err") + return + } + + mr := dbService.GetModelRun(int(id)) + if mr == nil { + c.String(404, "not found") + return + } + + ls := make([]*models.PluginUI, 0) + ses := comm.Db.Where("del!='1' and tid=?", mr.Tid).OrderBy("sort ASC,id ASC") + err = ses.Find(&ls) + if err != nil { + c.String(500, "find err:"+err.Error()) + return + } + for _, v := range ls { + v.ToUI(mr.Id) + } + c.JSON(200, ls) +} +func PlugLog(c *gin.Context, req *ruisUtil.Map) { + tid, err := req.GetInt("tid") + if err != nil || tid <= 0 { + c.String(500, "param err") + return + } + pid, err := req.GetInt("pid") + if err != nil || pid <= 0 { + c.String(500, "param err") + return + } + mr := dbService.GetModelRun(int(tid)) + if mr == nil { + c.String(404, "not found") + return + } + e := dbService.FindPluginRun(mr.Tid, mr.Id, int(pid)) + res := ruisUtil.NewMap() + res.Set("up", false) + res.Set("text", "") + if e.State == 1 { + res.Set("up", true) + res.Set("text", e.Output) + } else if e.State >= 2 { + res.Set("text", e.Output) + } + c.JSON(200, res) +} diff --git a/service/dbService/model.go b/service/dbService/model.go index 021f49f..9cbcf19 100644 --- a/service/dbService/model.go +++ b/service/dbService/model.go @@ -19,3 +19,17 @@ func GetModel(id int) *model.TModel { } return nil } +func GetModelRun(id int) *model.TModelRun { + if id <= 0 { + return nil + } + e := new(model.TModelRun) + ok, err := comm.Db.Where("id=?", id).Get(e) + if err != nil { + return nil + } + if ok { + return e + } + return nil +} diff --git a/service/dbService/plugin.go b/service/dbService/plugin.go index 7d7ceac..383471d 100644 --- a/service/dbService/plugin.go +++ b/service/dbService/plugin.go @@ -19,6 +19,20 @@ func GetPlugin(id int) *model.TPlugin { } return nil } +func GetPluginRun(id int) *model.TPluginRun { + if id <= 0 { + return nil + } + e := new(model.TPluginRun) + ok, err := comm.Db.Where("id=?", id).Get(e) + if err != nil { + return nil + } + if ok { + return e + } + return nil +} func FindPluginRun(mid, tid, pid int) *model.TPluginRun { if mid <= 0 || tid <= 0 || pid <= 0 { return nil diff --git a/uis/vue-admin/src/routes.js b/uis/vue-admin/src/routes.js index 57c58d2..f7a0f0d 100644 --- a/uis/vue-admin/src/routes.js +++ b/uis/vue-admin/src/routes.js @@ -23,8 +23,9 @@ let routes = [ iconCls: 'el-icon-message',//图标样式class children: [ { path: '/models', component: require('@/views/ruis/ModelList'), name: '流水线' }, - { path: '/models/info', component: require('@/views/ruis/PlugList'), name: '流水线详情', hidden: true }, + { path: '/models/info', component: require('@/views/ruis/PlugList'), name: '流水线插件', hidden: true }, { path: '/models/runs', component: require('@/views/ruis/RunList'), name: '流水线运行', hidden: true }, + { path: '/models/plug/runs', component: require('@/views/ruis/PlugRunList'), name: '流水日志', hidden: true }, ] }, /*{ diff --git a/uis/vue-admin/src/views/base/List.vue b/uis/vue-admin/src/views/base/List.vue index 9698cc6..1275150 100644 --- a/uis/vue-admin/src/views/base/List.vue +++ b/uis/vue-admin/src/views/base/List.vue @@ -89,6 +89,7 @@ this.filters.page=res.data.Page; //NProgress.done(); }).catch(err=>{ + this.loading = false; this.$message({ message: err.response.data||'服务器错误', type: 'error' diff --git a/uis/vue-admin/src/views/ruis/ModelForm.vue b/uis/vue-admin/src/views/ruis/ModelForm.vue index ca1d30d..a5fbf66 100644 --- a/uis/vue-admin/src/views/ruis/ModelForm.vue +++ b/uis/vue-admin/src/views/ruis/ModelForm.vue @@ -8,9 +8,12 @@ - + + + + @@ -47,16 +50,18 @@ Id:'', Title: '', Desc: '', + Envs:'', Wrkdir:'', - Envs:'' + clrdir:false } if(e) this.formData={ Id:e.Id, Title: e.Title, Desc: e.Desc, + Envs:e.Envs, Wrkdir:e.Wrkdir, - Envs:e.Envs + clrdir:e.Clrdir==1 } },/*handleSelect:function(id){ this.tmpltCont=''; @@ -81,6 +86,7 @@ this.$refs.formd.validate((valid) => { if (valid) { this.formLoading = true; + this.formData.Clrdir=this.formData.clrdir?1:2; this.$post('/model/edit',this.formData).then(res=>{ console.log(res); this.$emit('submitOK'); diff --git a/uis/vue-admin/src/views/ruis/ModelList.vue b/uis/vue-admin/src/views/ruis/ModelList.vue index 6112044..14ac186 100644 --- a/uis/vue-admin/src/views/ruis/ModelList.vue +++ b/uis/vue-admin/src/views/ruis/ModelList.vue @@ -21,16 +21,16 @@ --> - + - +