From 69ef555716d69d51675155d3ac76ffd7e89bd6ca Mon Sep 17 00:00:00 2001 From: bay1ts Date: Thu, 23 Nov 2017 09:57:25 +0800 Subject: [PATCH] [REV] optimize event ,in one event,but multi action,allow pass throth --- .../docker/chaos/plugins/build_work.py | 3 +- pkg/api/controller/serviceAction.go | 9 +++- pkg/builder/exector/exector.go | 48 +++++++++++++++---- pkg/db/mysql/dao/version.go | 10 ++-- 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/hack/contrib/docker/chaos/plugins/build_work.py b/hack/contrib/docker/chaos/plugins/build_work.py index 11450e87a..315f57da0 100644 --- a/hack/contrib/docker/chaos/plugins/build_work.py +++ b/hack/contrib/docker/chaos/plugins/build_work.py @@ -402,6 +402,7 @@ class RepoBuilder(): return True def feedback(self): + time.sleep(2) body = { "deploy_version": self.deploy_version, "event_id": self.event_id @@ -446,7 +447,7 @@ class RepoBuilder(): success = build_func() if success: # self.log.info("构建完成。", step="build-worker") - self.log.info("构建完成。", step="callback",status="success") + self.log.info("构建完成。", step="build-worker",status="success") ok = self.feedback() if not ok: diff --git a/pkg/api/controller/serviceAction.go b/pkg/api/controller/serviceAction.go index 4a8a45e3c..49e4569a4 100644 --- a/pkg/api/controller/serviceAction.go +++ b/pkg/api/controller/serviceAction.go @@ -79,7 +79,7 @@ func createEvent(eventID, serviceID, optType, tenantID, deployVersion string) (* event.DeployVersion = version event.OldDeployVersion = oldDeployVersion - status, err := checkCanAddEvent(serviceID) + status, err := checkCanAddEvent(serviceID,event.EventID) if err != nil { logrus.Errorf("error check event") return nil, status, nil @@ -119,8 +119,9 @@ func autoTimeOut(event *dbmodel.ServiceEvent) { } } } -func checkCanAddEvent(s string) (int, error) { +func checkCanAddEvent(s ,eventID string) (int, error) { events, err := db.GetManager().ServiceEventDao().GetEventByServiceID(s) + if err != nil { return 3, err } @@ -131,6 +132,9 @@ func checkCanAddEvent(s string) (int, error) { return 0, nil } latestEvent := events[0] + if latestEvent.EventID==eventID { + return 0,nil + } if latestEvent.FinalStatus == "" { //未完成 timeOut, err := checkEventTimeOut(latestEvent) @@ -514,6 +518,7 @@ func (t *TenantStruct) BuildService(w http.ResponseWriter, r *http.Request) { version.DeliveredType=build.Body.Kind version.CodeVersion="" version.BuildVersion=build.Body.DeployVersion + db.GetManager().VersionInfoDao().AddModel(&version) //save //version.DeliveredPath //version.FinalStatus diff --git a/pkg/builder/exector/exector.go b/pkg/builder/exector/exector.go index fb9d11e3e..24e082a24 100644 --- a/pkg/builder/exector/exector.go +++ b/pkg/builder/exector/exector.go @@ -26,6 +26,8 @@ import ( "github.com/goodrain/rainbond/pkg/db/config" "github.com/goodrain/rainbond/pkg/event" "github.com/goodrain/rainbond/pkg/mq/api/grpc/pb" + "github.com/goodrain/rainbond/pkg/db" + "github.com/goodrain/rainbond/pkg/db/model" "github.com/Sirupsen/logrus" "github.com/docker/docker/client" @@ -92,10 +94,12 @@ const pluginDockerfile = "plugins/plugin_dockerfile.pyc" func (e *exectorManager) appImage(in []byte) { eventID := gjson.GetBytes(in, "event_id").String() + v,_:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID) + v.FinalStatus="success" logger := event.GetManager().GetLogger(eventID) logger.Info("应用镜像构建任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"}) w := NewWorker(appImage, "", nil, in) - go func() { + go func(v *model.VersionInfo) { logrus.Info("start exec app image worker") defer event.GetManager().ReleaseLogger(logger) for i := 0; i < 3; i++ { @@ -106,19 +110,25 @@ func (e *exectorManager) appImage(in []byte) { logger.Info("应用镜像构建任务执行失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"}) } else { logger.Info("应用镜像构建任务执行失败", map[string]string{"step": "callback", "status": "failure"}) + v.FinalStatus="failure" + db.GetManager().VersionInfoDao().UpdateModel(v) } } else { break } } - }() + }(v) + db.GetManager().VersionInfoDao().UpdateModel(v) } func (e *exectorManager) appSlug(in []byte) { eventID := gjson.GetBytes(in, "event_id").String() + v,_:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID) + v.FinalStatus="success" + logger := event.GetManager().GetLogger(eventID) logger.Info("应用代码包构建任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"}) w := NewWorker(appSlug, "", nil, in) - go func() { + go func(v *model.VersionInfo) { logrus.Info("start exec app slug worker") defer event.GetManager().ReleaseLogger(logger) for i := 0; i < 3; i++ { @@ -129,40 +139,51 @@ func (e *exectorManager) appSlug(in []byte) { logger.Info("应用代码包构建任务执行失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"}) } else { logger.Info("应用代码包构建任务执行失败", map[string]string{"step": "callback", "status": "failure"}) + v.FinalStatus="failure" + db.GetManager().VersionInfoDao().UpdateModel(v) } } else { break } } - }() + }(v) + db.GetManager().VersionInfoDao().UpdateModel(v) } func (e *exectorManager) imageManual(in []byte) { eventID := gjson.GetBytes(in, "event_id").String() logger := event.GetManager().GetLogger(eventID) + + v,_:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID) + v.FinalStatus="success" + logger.Info("应用镜像构建任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"}) w := NewWorker(imageManual, "", nil, in) - go func() { + go func(v *model.VersionInfo) { defer event.GetManager().ReleaseLogger(logger) logrus.Info("start exec image manual worker") for i := 0; i < 3; i++ { - c, err := w.run(time.Minute * 30) - fmt.Printf("out put is ------------%s",string(c)) + _, err := w.run(time.Minute * 30) if err != nil { logrus.Errorf("exec image manual python shell error:%s", err.Error()) if i < 3 { logger.Info("应用镜像构建任务执行失败,开始重试", map[string]string{"step": "builder-exector", "status": "failure"}) } else { logger.Info("应用镜像构建任务执行失败", map[string]string{"step": "callback", "status": "failure"}) + v.FinalStatus="failure" + db.GetManager().VersionInfoDao().UpdateModel(v) } } else { break } } - }() + }(v) + db.GetManager().VersionInfoDao().UpdateModel(v) } func (e *exectorManager) codeCheck(in []byte) { eventID := gjson.GetBytes(in, "event_id").String() logger := event.GetManager().GetLogger(eventID) + + logger.Info("应用代码检测任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"}) w := NewWorker(codeCheck, "", nil, in) go func() { @@ -185,11 +206,15 @@ func (e *exectorManager) codeCheck(in []byte) { } func (e *exectorManager) appBuild(in []byte) { eventID := gjson.GetBytes(in, "event_id").String() + + v,_:=db.GetManager().VersionInfoDao().GetVersionByEventID(eventID) + v.FinalStatus="success" + logger := event.GetManager().GetLogger(eventID) logger.Info("应用编译构建任务开始执行", map[string]string{"step": "builder-exector", "status": "starting"}) w := NewWorker(appBuild, "", nil, in) - go func() { + go func(v *model.VersionInfo) { logrus.Info("start exec build app worker") defer event.GetManager().ReleaseLogger(logger) for i := 0; i < 3; i++ { @@ -204,10 +229,13 @@ func (e *exectorManager) appBuild(in []byte) { } else { logrus.Infof("build task success,eventID is %s",eventID) //logger.Info("应用编译构建任务执行成功", map[string]string{"step": "callback", "status": "success"}) + v.FinalStatus="failure" + db.GetManager().VersionInfoDao().UpdateModel(v) break } } - }() + }(v) + db.GetManager().VersionInfoDao().UpdateModel(v) } func (e *exectorManager) pluginImageBuild1(in []byte) { diff --git a/pkg/db/mysql/dao/version.go b/pkg/db/mysql/dao/version.go index 05df750b7..446f63e9d 100644 --- a/pkg/db/mysql/dao/version.go +++ b/pkg/db/mysql/dao/version.go @@ -29,14 +29,14 @@ import ( //AddModel AddModel func (c *VersionInfoDaoImpl) AddModel(mo model.Interface) error { - result := mo.(*model.ServiceEvent) - var oldResult model.ServiceEvent + result := mo.(*model.VersionInfo) + var oldResult model.VersionInfo if ok := c.DB.Where("event_id=?", result.EventID).Find(&oldResult).RecordNotFound(); ok { if err := c.DB.Create(result).Error; err != nil { return err } } else { - fmt.Errorf("event result is exist") + fmt.Errorf("version is exist") return nil } return nil @@ -45,6 +45,10 @@ func (c *VersionInfoDaoImpl) AddModel(mo model.Interface) error { //UpdateModel UpdateModel func (c *VersionInfoDaoImpl) UpdateModel(mo model.Interface) error { + result := mo.(*model.VersionInfo) + if err := c.DB.Save(result).Error; err != nil { + return err + } return nil }