change event judge

This commit is contained in:
曾庆国 2019-08-24 19:02:24 +08:00
parent 69e0fe2aa0
commit 2e602105a4
12 changed files with 144 additions and 165 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/controller"
"github.com/goodrain/rainbond/api/middleware"
dbmodel "github.com/goodrain/rainbond/db/model"
)
//V2 v2
@ -130,23 +131,23 @@ func (v2 *V2) serviceRouter() chi.Router {
r.Use(middleware.InitService)
//应用部署(act)
//r.Post("/deploy", controller.GetManager().DeployService)
r.Put("/", middleware.WrapEL(controller.GetManager().UpdateService, middleware.ST, "update-service", middleware.SYNEVENTTYPE))
r.Put("/", middleware.WrapEL(controller.GetManager().UpdateService, dbmodel.TargetTypeService, "update-service", dbmodel.SYNEVENTTYPE))
//应用构建(act)
r.Post("/build", middleware.WrapEL(controller.GetManager().BuildService, middleware.ST, "build-service", middleware.ASYNEVENTTYPE))
r.Post("/build", middleware.WrapEL(controller.GetManager().BuildService, dbmodel.TargetTypeService, "build-service", dbmodel.ASYNEVENTTYPE))
//应用起停
r.Post("/start", middleware.WrapEL(controller.GetManager().StartService, middleware.ST, "start-service", middleware.ASYNEVENTTYPE))
r.Post("/stop", middleware.WrapEL(controller.GetManager().StopService, middleware.ST, "stop-service", middleware.ASYNEVENTTYPE))
r.Post("/restart", middleware.WrapEL(controller.GetManager().RestartService, middleware.ST, "restart-service", middleware.ASYNEVENTTYPE))
r.Post("/start", middleware.WrapEL(controller.GetManager().StartService, dbmodel.TargetTypeService, "start-service", dbmodel.ASYNEVENTTYPE))
r.Post("/stop", middleware.WrapEL(controller.GetManager().StopService, dbmodel.TargetTypeService, "stop-service", dbmodel.ASYNEVENTTYPE))
r.Post("/restart", middleware.WrapEL(controller.GetManager().RestartService, dbmodel.TargetTypeService, "restart-service", dbmodel.ASYNEVENTTYPE))
//应用伸缩
r.Put("/vertical", middleware.WrapEL(controller.GetManager().VerticalService, middleware.ST, "vertical-service", middleware.ASYNEVENTTYPE))
r.Put("/horizontal", middleware.WrapEL(controller.GetManager().HorizontalService, middleware.ST, "horizontal-service", middleware.ASYNEVENTTYPE))
r.Put("/vertical", middleware.WrapEL(controller.GetManager().VerticalService, dbmodel.TargetTypeService, "vertical-service", dbmodel.ASYNEVENTTYPE))
r.Put("/horizontal", middleware.WrapEL(controller.GetManager().HorizontalService, dbmodel.TargetTypeService, "horizontal-service", dbmodel.ASYNEVENTTYPE))
//设置应用语言(act)
r.Post("/language", middleware.WrapEL(controller.GetManager().SetLanguage, middleware.ST, "set-language", middleware.SYNEVENTTYPE))
r.Post("/language", middleware.WrapEL(controller.GetManager().SetLanguage, dbmodel.TargetTypeService, "set-language", dbmodel.SYNEVENTTYPE))
//应用信息获取修改与删除(source)
r.Get("/", controller.GetManager().SingleServiceInfo)
r.Delete("/", middleware.WrapEL(controller.GetManager().SingleServiceInfo, middleware.ST, "delete-service", middleware.SYNEVENTTYPE))
r.Delete("/", middleware.WrapEL(controller.GetManager().SingleServiceInfo, dbmodel.TargetTypeService, "delete-service", dbmodel.SYNEVENTTYPE))
//应用升级(act)
r.Post("/upgrade", middleware.WrapEL(controller.GetManager().UpgradeService, middleware.ST, "upgrade-service", middleware.SYNEVENTTYPE))
r.Post("/upgrade", middleware.WrapEL(controller.GetManager().UpgradeService, dbmodel.TargetTypeService, "upgrade-service", dbmodel.SYNEVENTTYPE))
//应用状态获取(act)
r.Get("/status", controller.GetManager().StatusService)
//构建版本列表
@ -154,9 +155,9 @@ func (v2 *V2) serviceRouter() chi.Router {
//构建版本操作
r.Get("/build-version/{build_version}", controller.GetManager().BuildVersionInfo)
r.Get("/deployversion", controller.GetManager().GetDeployVersion)
r.Delete("/build-version/{build_version}", middleware.WrapEL(controller.GetManager().BuildVersionInfo, middleware.ST, "delete-buildversion", middleware.SYNEVENTTYPE))
r.Delete("/build-version/{build_version}", middleware.WrapEL(controller.GetManager().BuildVersionInfo, dbmodel.TargetTypeService, "delete-buildversion", dbmodel.SYNEVENTTYPE))
//应用分享
r.Post("/share", middleware.WrapEL(controller.GetManager().Share, middleware.ST, "share-service", middleware.ASYNEVENTTYPE))
r.Post("/share", middleware.WrapEL(controller.GetManager().Share, dbmodel.TargetTypeService, "share-service", dbmodel.ASYNEVENTTYPE))
r.Get("/share/{share_id}", controller.GetManager().ShareResult)
//应用日志相关
r.Post("/log", controller.GetManager().Logs)
@ -166,49 +167,49 @@ func (v2 *V2) serviceRouter() chi.Router {
r.Post("/event-log", controller.GetManager().LogByAction)
//应用依赖关系增加与删除(source)
r.Post("/dependency", middleware.WrapEL(controller.GetManager().Dependency, middleware.ST, "add-service-dependency", middleware.SYNEVENTTYPE))
r.Delete("/dependency", middleware.WrapEL(controller.GetManager().Dependency, middleware.ST, "delete-service-dependency", middleware.SYNEVENTTYPE))
r.Post("/dependency", middleware.WrapEL(controller.GetManager().Dependency, dbmodel.TargetTypeService, "add-service-dependency", dbmodel.SYNEVENTTYPE))
r.Delete("/dependency", middleware.WrapEL(controller.GetManager().Dependency, dbmodel.TargetTypeService, "delete-service-dependency", dbmodel.SYNEVENTTYPE))
//环境变量增删改(source)
r.Post("/env", middleware.WrapEL(controller.GetManager().Env, middleware.ST, "add-service-env", middleware.SYNEVENTTYPE))
r.Put("/env", middleware.WrapEL(controller.GetManager().Env, middleware.ST, "update-service-env", middleware.SYNEVENTTYPE))
r.Delete("/env", middleware.WrapEL(controller.GetManager().Env, middleware.ST, "delete-service-env", middleware.SYNEVENTTYPE))
r.Post("/env", middleware.WrapEL(controller.GetManager().Env, dbmodel.TargetTypeService, "add-service-env", dbmodel.SYNEVENTTYPE))
r.Put("/env", middleware.WrapEL(controller.GetManager().Env, dbmodel.TargetTypeService, "update-service-env", dbmodel.SYNEVENTTYPE))
r.Delete("/env", middleware.WrapEL(controller.GetManager().Env, dbmodel.TargetTypeService, "delete-service-env", dbmodel.SYNEVENTTYPE))
//端口变量增删改(source)
r.Post("/ports", middleware.WrapEL(controller.GetManager().Ports, middleware.ST, "add-service-port", middleware.SYNEVENTTYPE))
r.Put("/ports", middleware.WrapEL(controller.GetManager().PutPorts, middleware.ST, "update-service-port-old", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}", middleware.WrapEL(controller.GetManager().Ports, middleware.ST, "update-service-port", middleware.SYNEVENTTYPE))
r.Delete("/ports/{port}", middleware.WrapEL(controller.GetManager().Ports, middleware.ST, "delete-service-port", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}/outer", middleware.WrapEL(controller.GetManager().PortOuterController, middleware.ST, "handle-service-outerport", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}/inner", middleware.WrapEL(controller.GetManager().PortInnerController, middleware.ST, "handle-service-innerport", middleware.SYNEVENTTYPE))
r.Put("/ports/{port}/changelbport", middleware.WrapEL(controller.GetManager().ChangeLBPort, middleware.ST, "change-service-lbport", middleware.SYNEVENTTYPE))
r.Post("/ports", middleware.WrapEL(controller.GetManager().Ports, dbmodel.TargetTypeService, "add-service-port", dbmodel.SYNEVENTTYPE))
r.Put("/ports", middleware.WrapEL(controller.GetManager().PutPorts, dbmodel.TargetTypeService, "update-service-port-old", dbmodel.SYNEVENTTYPE))
r.Put("/ports/{port}", middleware.WrapEL(controller.GetManager().Ports, dbmodel.TargetTypeService, "update-service-port", dbmodel.SYNEVENTTYPE))
r.Delete("/ports/{port}", middleware.WrapEL(controller.GetManager().Ports, dbmodel.TargetTypeService, "delete-service-port", dbmodel.SYNEVENTTYPE))
r.Put("/ports/{port}/outer", middleware.WrapEL(controller.GetManager().PortOuterController, dbmodel.TargetTypeService, "handle-service-outerport", dbmodel.SYNEVENTTYPE))
r.Put("/ports/{port}/inner", middleware.WrapEL(controller.GetManager().PortInnerController, dbmodel.TargetTypeService, "handle-service-innerport", dbmodel.SYNEVENTTYPE))
r.Put("/ports/{port}/changelbport", middleware.WrapEL(controller.GetManager().ChangeLBPort, dbmodel.TargetTypeService, "change-service-lbport", dbmodel.SYNEVENTTYPE))
//应用版本回滚(act)
r.Post("/rollback", middleware.WrapEL(controller.GetManager().RollBack, middleware.ST, "rollback-service", middleware.ASYNEVENTTYPE))
r.Post("/rollback", middleware.WrapEL(controller.GetManager().RollBack, dbmodel.TargetTypeService, "rollback-service", dbmodel.ASYNEVENTTYPE))
//持久化信息API v2.1 支持多种持久化格式
r.Post("/volumes", middleware.WrapEL(controller.AddVolume, middleware.ST, "add-service-volume", middleware.SYNEVENTTYPE))
r.Put("/volumes", middleware.WrapEL(controller.GetManager().UpdVolume, middleware.ST, "update-service-volume", middleware.SYNEVENTTYPE))
r.Post("/volumes", middleware.WrapEL(controller.AddVolume, dbmodel.TargetTypeService, "add-service-volume", dbmodel.SYNEVENTTYPE))
r.Put("/volumes", middleware.WrapEL(controller.GetManager().UpdVolume, dbmodel.TargetTypeService, "update-service-volume", dbmodel.SYNEVENTTYPE))
r.Get("/volumes", controller.GetVolume)
r.Delete("/volumes/{volume_name}", middleware.WrapEL(controller.DeleteVolume, middleware.ST, "delete-service-volume", middleware.SYNEVENTTYPE))
r.Post("/depvolumes", middleware.WrapEL(controller.AddVolumeDependency, middleware.ST, "add-service-depvolume", middleware.SYNEVENTTYPE))
r.Delete("/depvolumes", middleware.WrapEL(controller.DeleteVolumeDependency, middleware.ST, "delete-service-depvolume", middleware.SYNEVENTTYPE))
r.Delete("/volumes/{volume_name}", middleware.WrapEL(controller.DeleteVolume, dbmodel.TargetTypeService, "delete-service-volume", dbmodel.SYNEVENTTYPE))
r.Post("/depvolumes", middleware.WrapEL(controller.AddVolumeDependency, dbmodel.TargetTypeService, "add-service-depvolume", dbmodel.SYNEVENTTYPE))
r.Delete("/depvolumes", middleware.WrapEL(controller.DeleteVolumeDependency, dbmodel.TargetTypeService, "delete-service-depvolume", dbmodel.SYNEVENTTYPE))
r.Get("/depvolumes", controller.GetDepVolume)
//持久化信息API v2
r.Post("/volume-dependency", middleware.WrapEL(controller.GetManager().VolumeDependency, middleware.ST, "add-service-depvolume", middleware.SYNEVENTTYPE))
r.Delete("/volume-dependency", middleware.WrapEL(controller.GetManager().VolumeDependency, middleware.ST, "delete-service-depvolume", middleware.SYNEVENTTYPE))
r.Post("/volume", middleware.WrapEL(controller.GetManager().AddVolume, middleware.ST, "add-service-volume", middleware.SYNEVENTTYPE))
r.Delete("/volume", middleware.WrapEL(controller.GetManager().DeleteVolume, middleware.ST, "delete-service-volume", middleware.SYNEVENTTYPE))
r.Post("/volume-dependency", middleware.WrapEL(controller.GetManager().VolumeDependency, dbmodel.TargetTypeService, "add-service-depvolume", dbmodel.SYNEVENTTYPE))
r.Delete("/volume-dependency", middleware.WrapEL(controller.GetManager().VolumeDependency, dbmodel.TargetTypeService, "delete-service-depvolume", dbmodel.SYNEVENTTYPE))
r.Post("/volume", middleware.WrapEL(controller.GetManager().AddVolume, dbmodel.TargetTypeService, "add-service-volume", dbmodel.SYNEVENTTYPE))
r.Delete("/volume", middleware.WrapEL(controller.GetManager().DeleteVolume, dbmodel.TargetTypeService, "delete-service-volume", dbmodel.SYNEVENTTYPE))
//获取应用实例情况(source)
r.Get("/pods", controller.GetManager().Pods)
//应用探针 增 删 改(surce)
r.Post("/probe", middleware.WrapEL(controller.GetManager().Probe, middleware.ST, "add-service-probe", middleware.SYNEVENTTYPE))
r.Put("/probe", middleware.WrapEL(controller.GetManager().Probe, middleware.ST, "update-service-probe", middleware.SYNEVENTTYPE))
r.Delete("/probe", middleware.WrapEL(controller.GetManager().Probe, middleware.ST, "delete-service-probe", middleware.SYNEVENTTYPE))
r.Post("/probe", middleware.WrapEL(controller.GetManager().Probe, dbmodel.TargetTypeService, "add-service-probe", dbmodel.SYNEVENTTYPE))
r.Put("/probe", middleware.WrapEL(controller.GetManager().Probe, dbmodel.TargetTypeService, "update-service-probe", dbmodel.SYNEVENTTYPE))
r.Delete("/probe", middleware.WrapEL(controller.GetManager().Probe, dbmodel.TargetTypeService, "delete-service-probe", dbmodel.SYNEVENTTYPE))
r.Post("/label", middleware.WrapEL(controller.GetManager().Label, middleware.ST, "add-service-label", middleware.SYNEVENTTYPE))
r.Put("/label", middleware.WrapEL(controller.GetManager().Label, middleware.ST, "update-service-label", middleware.SYNEVENTTYPE))
r.Delete("/label", middleware.WrapEL(controller.GetManager().Label, middleware.ST, "delete-service-label", middleware.SYNEVENTTYPE))
r.Post("/label", middleware.WrapEL(controller.GetManager().Label, dbmodel.TargetTypeService, "add-service-label", dbmodel.SYNEVENTTYPE))
r.Put("/label", middleware.WrapEL(controller.GetManager().Label, dbmodel.TargetTypeService, "update-service-label", dbmodel.SYNEVENTTYPE))
r.Delete("/label", middleware.WrapEL(controller.GetManager().Label, dbmodel.TargetTypeService, "delete-service-label", dbmodel.SYNEVENTTYPE))
//插件
r.Mount("/plugin", v2.serviceRelatePluginRouter())
@ -218,22 +219,22 @@ func (v2 *V2) serviceRouter() chi.Router {
r.Get("/deploy-info", controller.GetServiceDeployInfo)
// third-party service
r.Post("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, middleware.ST, "add-thirdpart-service", middleware.ASYNEVENTTYPE))
r.Put("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, middleware.ST, "update-thirdpart-service", middleware.ASYNEVENTTYPE))
r.Delete("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, middleware.ST, "delete-thirdpart-service", middleware.ASYNEVENTTYPE))
r.Post("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, dbmodel.TargetTypeService, "add-thirdpart-service", dbmodel.ASYNEVENTTYPE))
r.Put("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, dbmodel.TargetTypeService, "update-thirdpart-service", dbmodel.ASYNEVENTTYPE))
r.Delete("/endpoints", middleware.WrapEL(controller.GetManager().Endpoints, dbmodel.TargetTypeService, "delete-thirdpart-service", dbmodel.ASYNEVENTTYPE))
r.Get("/endpoints", controller.GetManager().Endpoints)
// gateway
r.Put("/rule-config", middleware.WrapEL(controller.GetManager().RuleConfig, middleware.ST, "update-service-gateway-rule", middleware.ASYNEVENTTYPE))
r.Put("/rule-config", middleware.WrapEL(controller.GetManager().RuleConfig, dbmodel.TargetTypeService, "update-service-gateway-rule", dbmodel.ASYNEVENTTYPE))
// app restore
r.Post("/app-restore/envs", middleware.WrapEL(controller.GetManager().RestoreEnvs, middleware.ST, "app-restore-envs", middleware.SYNEVENTTYPE))
r.Post("/app-restore/ports", middleware.WrapEL(controller.GetManager().RestorePorts, middleware.ST, "app-restore-ports", middleware.SYNEVENTTYPE))
r.Post("/app-restore/volumes", middleware.WrapEL(controller.GetManager().RestoreVolumes, middleware.ST, "app-restore-volumes", middleware.SYNEVENTTYPE))
r.Post("/app-restore/probe", middleware.WrapEL(controller.GetManager().RestoreProbe, middleware.ST, "app-restore-probe", middleware.SYNEVENTTYPE))
r.Post("/app-restore/deps", middleware.WrapEL(controller.GetManager().RestoreDeps, middleware.ST, "app-restore-deps", middleware.SYNEVENTTYPE))
r.Post("/app-restore/depvols", middleware.WrapEL(controller.GetManager().RestoreDepVols, middleware.ST, "app-restore-depvols", middleware.SYNEVENTTYPE))
r.Post("/app-restore/plugins", middleware.WrapEL(controller.GetManager().RestorePlugins, middleware.ST, "app-restore-plugins", middleware.SYNEVENTTYPE))
r.Post("/app-restore/envs", middleware.WrapEL(controller.GetManager().RestoreEnvs, dbmodel.TargetTypeService, "app-restore-envs", dbmodel.SYNEVENTTYPE))
r.Post("/app-restore/ports", middleware.WrapEL(controller.GetManager().RestorePorts, dbmodel.TargetTypeService, "app-restore-ports", dbmodel.SYNEVENTTYPE))
r.Post("/app-restore/volumes", middleware.WrapEL(controller.GetManager().RestoreVolumes, dbmodel.TargetTypeService, "app-restore-volumes", dbmodel.SYNEVENTTYPE))
r.Post("/app-restore/probe", middleware.WrapEL(controller.GetManager().RestoreProbe, dbmodel.TargetTypeService, "app-restore-probe", dbmodel.SYNEVENTTYPE))
r.Post("/app-restore/deps", middleware.WrapEL(controller.GetManager().RestoreDeps, dbmodel.TargetTypeService, "app-restore-deps", dbmodel.SYNEVENTTYPE))
r.Post("/app-restore/depvols", middleware.WrapEL(controller.GetManager().RestoreDepVols, dbmodel.TargetTypeService, "app-restore-depvols", dbmodel.SYNEVENTTYPE))
r.Post("/app-restore/plugins", middleware.WrapEL(controller.GetManager().RestorePlugins, dbmodel.TargetTypeService, "app-restore-plugins", dbmodel.SYNEVENTTYPE))
r.Get("/pods/{pod_name}/detail", controller.GetManager().PodDetail)

View File

@ -254,17 +254,20 @@ func (e *EventLogStruct) Events(w http.ResponseWriter, r *http.Request) {
if size, err = strconv.Atoi(r.FormValue("size")); err != nil || size <= 0 {
size = 10
}
logrus.Debugf("get event page param[page:%d, page_size:%d]", page, size)
logrus.Debugf("get event page param[target:%s id:%s page:%d, page_size:%d]", target, targetID, page, size)
list, total, err := handler.GetEventHandler().GetEvents(target, targetID, page, size)
if err != nil {
logrus.Errorf("get event log error, %v", err)
httputil.ReturnError(r, w, 500, "get log error")
return
}
// format start and end time
for i := range list {
if list[i].EndTime != "" && len(list[i].EndTime) > 20 {
list[i].EndTime = strings.Replace(list[i].EndTime[0:19]+"+08:00", " ", "T", 1)
}
}
httputil.ReturnList(r, w, total, page, list)
return
}
//EventLog get event log by eventID

View File

@ -97,7 +97,7 @@ func checkEventTimeOut(event *dbmodel.ServiceEvent) (bool, error) {
return true, err
}
if event.OptType == "deploy" || event.OptType == "create" || event.OptType == "build" || event.OptType == "upgrade" {
end := start.Add(2 * time.Minute)
end := start.Add(3 * time.Minute)
if time.Now().After(end) {
event.FinalStatus = "timeout"
err = db.GetManager().ServiceEventDao().UpdateModel(event)

View File

@ -237,112 +237,70 @@ func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.Handle
httputil.ReturnError(r, w, 400, "操作对象未指定")
return
}
// tenantID can not null
tenantID := r.Context().Value(ContextKey("tenant_id")).(string)
logrus.Debugf("path: %s", r.RequestURI)
var ctx context.Context
//eventLog check the latest event
if !canDoEvent(target, targetID) {
if !canDoEvent(optType, synType, target, targetID) {
httputil.ReturnError(r, w, 400, "操作过于频繁,请稍后再试")
return
}
// tenantID can not null
tenantID := r.Context().Value(ContextKey("tenant_id")).(string)
var ctx context.Context
// check resource is enough or not
if err := checkResource(optType, r); err != nil {
httputil.ReturnError(r, w, 400, err.Error())
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
logrus.Warningf("error reading request body: %v", err)
} else {
logrus.Debugf("method: %s; uri: %s; body: %s", r.Method, r.RequestURI, string(body))
}
// set a new body, which will simulate the same data we read
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
event, err := createEvent(target, optType, targetID, tenantID, string(body), "system", synType) // TODO username
if err != nil {
logrus.Error("create event error : ", err)
httputil.ReturnError(r, w, 500, "操作失败")
return
}
ctx = context.WithValue(r.Context(), ContextKey("event"), event)
ctx = context.WithValue(ctx, ContextKey("event_id"), event.EventID)
rw := &resWriter{origWriter: w}
f(rw, r.WithContext(ctx))
if synType == SYNEVENTTYPE || (synType == ASYNEVENTTYPE && rw.statusCode != 200) {
if synType == dbmodel.SYNEVENTTYPE || (synType == dbmodel.ASYNEVENTTYPE && rw.statusCode != 200) {
updateEvent(event.EventID, rw.statusCode)
}
}
}
}
//TIMELAYOUT timelayout
const TIMELAYOUT = "2006-01-02T15:04:05"
// ASYNEVENTTYPE asyn event type
const ASYNEVENTTYPE = 0
// SYNEVENTTYPE syn event type
const SYNEVENTTYPE = 1
// ST service target
const ST = "service"
// TT tenant target
const TT = "tenant"
func canDoEvent(target, targetID string) bool {
events, _, err := db.GetManager().ServiceEventDao().GetEventsByTarget(target, targetID, 0, 10)
func canDoEvent(optType string, synType int, target, targetID string) bool {
if synType == dbmodel.SYNEVENTTYPE {
return true
}
event, err := db.GetManager().ServiceEventDao().GetLastASyncEvent(target, targetID)
if err != nil {
if err.Error() == gorm.ErrRecordNotFound.Error() {
logrus.Debug("record notfound:", err)
return true
}
logrus.Error("get event by targetID error:", err)
return false
}
if len(events) == 0 {
if event == nil || event.FinalStatus != "" {
return true
}
var event *dbmodel.ServiceEvent // the latest asyn event
for i := range events {
if events[i].SynType == SYNEVENTTYPE && events[i].Status != "success" {
// syn event do not finish successfully, can not process on
return false
}
if events[i].SynType == ASYNEVENTTYPE {
event = events[i]
break
}
if !checkTimeout(event) {
return false
}
if event != nil {
if !checkTimeout(event) {
return false
}
}
return true
}
func checkTimeout(event *dbmodel.ServiceEvent) bool {
if event.SynType == ASYNEVENTTYPE {
if event.SynType == dbmodel.ASYNEVENTTYPE {
if event.FinalStatus == "" {
startTime := event.StartTime
start, err := time.ParseInLocation(TIMELAYOUT, startTime, time.Local)
start, err := time.ParseInLocation(time.RFC3339, startTime, time.Local)
if err != nil {
return false
}
@ -372,12 +330,10 @@ func checkResource(optType string, r *http.Request) error {
if optType == "start-service" || optType == "restart-service" || optType == "deploy-service" || optType == "horizontal-service" || optType == "vertical-service" || optType == "upgrade-service" {
if publicCloud := os.Getenv("PUBLIC_CLOUD"); publicCloud != "true" {
tenant := r.Context().Value(ContextKey("tenant")).(*model.Tenants)
if service, ok := r.Context().Value(ContextKey("service")).(*model.TenantServices); ok {
return priChargeSverify(tenant, service.ContainerMemory*service.Replicas)
}
}
}
return nil
}
@ -414,7 +370,7 @@ func createEvent(target, optType, targetID, tenantID, reqBody, userName string,
TargetID: targetID,
RequestBody: reqBody,
UserName: userName,
StartTime: time.Now().Format(TIMELAYOUT),
StartTime: time.Now().Format(time.RFC3339),
SynType: synType,
OptType: optType,
}
@ -432,15 +388,17 @@ func updateEvent(eventID string, statusCode int) {
logrus.Errorf("do not found event by eventID %s", eventID)
return
}
event.FinalStatus = "complete"
event.EndTime = time.Now().Format(time.RFC3339)
if statusCode == 200 {
event.Status = "success"
} else {
event.Status = "failure"
}
err = db.GetManager().ServiceEventDao().UpdateModel(event)
if err != nil {
retry := 3
logrus.Errorf("update event status failure %s", err.Error())
retry := 2
for retry > 0 {
if err = db.GetManager().ServiceEventDao().UpdateModel(event); err != nil {
retry--

View File

@ -375,7 +375,7 @@ type rollingUpgradeTaskBody struct {
func (e *exectorManager) sendAction(tenantID, serviceID, eventID, newVersion, actionType string, configs map[string]string, logger event.Logger) error {
// update build event complete status
logger.Info("Build success", map[string]string{"step": "last", "status": "running"})
logger.Info("Build success", map[string]string{"step": "last", "status": "success"})
switch actionType {
case "upgrade":
//add upgrade event
@ -388,7 +388,7 @@ func (e *exectorManager) sendAction(tenantID, serviceID, eventID, newVersion, ac
Target: "service",
TargetID: serviceID,
UserName: "system",
SynType: 1,
SynType: dbmodel.ASYNEVENTTYPE,
}
if err := db.GetManager().ServiceEventDao().AddModel(event); err != nil {
logrus.Errorf("create upgrade event failure %s, service %s do not auto upgrade", err.Error(), serviceID)
@ -412,7 +412,6 @@ func (e *exectorManager) sendAction(tenantID, serviceID, eventID, newVersion, ac
}); err != nil {
return err
}
logger.Info("Build success", map[string]string{"step": "last", "status": "running"})
return nil
default:
}

View File

@ -313,6 +313,7 @@ type EventDao interface {
GetEventsByTarget(target, targetID string, offset, liimt int) ([]*model.ServiceEvent, int, error)
GetEventsByTenantID(tenantID string, offset, limit int) ([]*model.ServiceEvent, int, error)
GetBySIDAndType(serviceID string, optTypes ...string) (*model.ServiceEvent, error)
GetLastASyncEvent(target, targetID string) (*model.ServiceEvent, error)
}
//VersionInfoDao VersionInfoDao

View File

@ -20,6 +20,18 @@ package model
import "time"
// ASYNEVENTTYPE asyn event type
const ASYNEVENTTYPE = 0
// SYNEVENTTYPE syn event type
const SYNEVENTTYPE = 1
// TargetTypeService service target
const TargetTypeService = "service"
// TargetTypeTenant tenant target
const TargetTypeTenant = "tenant"
//ServiceEvent event struct
type ServiceEvent struct {
Model

View File

@ -19,7 +19,6 @@
package dao
import (
"encoding/json"
"strings"
"time"
@ -45,35 +44,16 @@ func (c *EventDaoImpl) AddModel(mo model.Interface) error {
//UpdateModel UpdateModel
func (c *EventDaoImpl) UpdateModel(mo model.Interface) error {
result := mo.(*model.ServiceEvent)
update := mo.(*model.ServiceEvent)
var oldResult model.ServiceEvent
if ok := c.DB.Where("event_id=?", result.EventID).Find(&oldResult).RecordNotFound(); !ok {
finalUpdateEvent(result, &oldResult)
oldB, _ := json.Marshal(oldResult)
logrus.Infof("update event to %s", string(oldB))
if err := c.DB.Save(&oldResult).Error; err != nil {
if ok := c.DB.Where("event_id=?", update.EventID).Find(&oldResult).RecordNotFound(); !ok {
update.ID = oldResult.ID
if err := c.DB.Save(&update).Error; err != nil {
return err
}
}
return nil
}
func finalUpdateEvent(target *model.ServiceEvent, old *model.ServiceEvent) {
if target.OptType != "" {
old.OptType = target.OptType
}
if target.Status != "" {
old.Status = target.Status
}
if target.Message != "" {
old.Message = target.Message
}
old.FinalStatus = "complete"
if target.FinalStatus != "" {
old.FinalStatus = target.FinalStatus
}
old.EndTime = time.Now().Format(time.RFC3339)
}
//EventDaoImpl EventLogMessageDaoImpl
type EventDaoImpl struct {
@ -131,11 +111,13 @@ func (c *EventDaoImpl) GetEventsByTarget(target, targetID string, offset, limit
var result []*model.ServiceEvent
var total int
db := c.DB
if strings.TrimSpace(target) != "" {
db = db.Where("target=?", strings.TrimSpace(target))
}
if strings.TrimSpace(targetID) != "" {
db = db.Where("target_id=?", strings.TrimSpace(targetID))
if target != "" && targetID != "" {
// Compatible with previous 5.1.7 data, with null target and targetid
if strings.TrimSpace(target) == "service" {
db = db.Where("service_id=? or (target=? and target_id=?) ", strings.TrimSpace(targetID), strings.TrimSpace(target), strings.TrimSpace(targetID))
} else {
db = db.Where("target=? and target_id=?", strings.TrimSpace(target), strings.TrimSpace(targetID))
}
}
if err := db.Find(&result).Count(&total).Error; err != nil {
return nil, 0, err
@ -155,7 +137,6 @@ func (c *EventDaoImpl) GetEventsByTenantID(tenantID string, offset, limit int) (
var result []*model.ServiceEvent
var total int
db := c.DB.Where("tenant_id=?", tenantID)
if err := db.Find(&result).Count(&total).Error; err != nil {
return nil, 0, err
}
@ -165,7 +146,6 @@ func (c *EventDaoImpl) GetEventsByTenantID(tenantID string, offset, limit int) (
}
return nil, 0, err
}
return result, total, nil
}
@ -176,7 +156,15 @@ func (c *EventDaoImpl) GetBySIDAndType(serviceID string, optTypes ...string) (*m
return nil, err
}
return &result, nil
}
//GetLastASyncEvent get last sync event
func (c *EventDaoImpl) GetLastASyncEvent(target, targetID string) (*model.ServiceEvent, error) {
var result model.ServiceEvent
if err := c.DB.Where("target=? and target_id=? and syn_type=0", target, targetID).Last(&result).Error; err != nil {
return nil, err
}
return &result, nil
}
//NotificationEventDaoImpl NotificationEventDaoImpl

View File

@ -23,7 +23,6 @@ import (
"time"
cdb "github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/eventlog/conf"
"github.com/goodrain/rainbond/eventlog/db"
"github.com/goodrain/rainbond/eventlog/util"
@ -283,14 +282,21 @@ func (h *handleMessageStore) handleBarrelEvent() {
eventID := event[1]
status := event[2]
message := event[3]
event := model.ServiceEvent{}
event.EventID = eventID
event.Status = status
event.Message = message
logrus.Infof("updating event %s's status: %s", eventID, status)
if err := cdb.GetManager().ServiceEventDao().UpdateModel(&event); err != nil {
logrus.Errorf("update event status failure %s", err.Error())
event, err := cdb.GetManager().ServiceEventDao().GetEventByEventID(eventID)
if err != nil {
logrus.Errorf("get event by event id %s failure %s", eventID, err.Error())
} else {
event.Status = status
event.FinalStatus = "complete"
event.Message = message
event.EndTime = time.Now().Format(time.RFC3339)
logrus.Infof("updating event %s's status: %s", eventID, status)
if err := cdb.GetManager().ServiceEventDao().UpdateModel(event); err != nil {
logrus.Errorf("update event status failure %s", err.Error())
}
}
}
}
case <-h.ctx.Done():

View File

@ -19,10 +19,11 @@
package controller
import (
httputil "github.com/goodrain/rainbond/util/http"
"net/http"
"strings"
httputil "github.com/goodrain/rainbond/util/http"
"github.com/Sirupsen/logrus"
"github.com/go-chi/chi"
)
@ -44,7 +45,6 @@ func APPDiscover(w http.ResponseWriter, r *http.Request) {
return
}
httputil.ReturnSuccess(r, w, endpoints)
//httputil.ReturnList(r, w, len(endpoints), 1, endpoints...)
}
//APPList 列出已注册应用

View File

@ -21,7 +21,9 @@ package util
import (
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
func TestOpenOrCreateFile(t *testing.T) {
@ -111,3 +113,12 @@ func TestParseVariable(t *testing.T) {
"ZZZ": ",.,.,.,.",
}))
}
func TestTimeFormat(t *testing.T) {
tt := "2019-08-24 11:11:30.165753932 +0800 CST m=+55557.682499470"
timeF, err := time.Parse(time.RFC3339, strings.Replace(tt[0:19]+"+08:00", " ", "T", 1))
if err != nil {
t.Fatal(err)
}
t.Log(timeF.Format(time.RFC3339))
}

View File

@ -141,10 +141,10 @@ func ReturnSuccess(r *http.Request, w http.ResponseWriter, datas interface{}) {
return
}
//ReturnList 返回列表
func ReturnList(r *http.Request, w http.ResponseWriter, listAllNumber, page int, datas ...interface{}) {
//ReturnList return list with page and count
func ReturnList(r *http.Request, w http.ResponseWriter, listAllNumber, page int, list interface{}) {
r = r.WithContext(context.WithValue(r.Context(), render.StatusCtxKey, http.StatusOK))
render.DefaultResponder(w, r, ResponseBody{List: datas, ListAllNumber: listAllNumber, Page: page})
render.DefaultResponder(w, r, ResponseBody{List: list, ListAllNumber: listAllNumber, Page: page})
}
//ReturnError 返回错误信息