From 2e8246fb2f3d9648b04683a6c5dbdec117e53bc8 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 29 Aug 2024 11:03:26 +0200 Subject: [PATCH] correct workflow problem --- models/workflow/workflow.go | 3 +++ models/workflow/workflow_mongo_accessor.go | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/models/workflow/workflow.go b/models/workflow/workflow.go index 7b32f0f..b5ca48d 100644 --- a/models/workflow/workflow.go +++ b/models/workflow/workflow.go @@ -21,6 +21,9 @@ type AbstractWorkflow struct { } func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) (bool, string) { + if w.Graph == nil || w.Graph.Items == nil { + return false, "" + } if d, ok := w.Graph.Items[link.Source.ID]; ok && d.Datacenter != nil { return true, d.Datacenter.UUID } diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index 2237517..4a46fe2 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -82,7 +82,11 @@ func (wfa *workflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, err wfa.execution(id, &Workflow{ AbstractWorkflow: AbstractWorkflow{ScheduleActive: false}, }, true) - return wfa.GenericDeleteOne(id, wfa) + res, code, err := wfa.GenericDeleteOne(id, wfa) + if res != nil && code == 200 { + wfa.execute(res.(*Workflow), false) + } + return res, code, err } func (wfa *workflowMongoAccessor) book(id string, realData *Workflow, execs []*workflow_execution.WorkflowExecution) error { @@ -157,6 +161,7 @@ func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delet "workflow_id": id, "state": 1, }, utils.WORKFLOW_EXECUTION.String()) + wfa.book(id, realData, []*workflow_execution.WorkflowExecution{}) nats.SetNATSPub(utils.WORKFLOW.String(), tools.REMOVE, realData) } if len(execs) > 0 { @@ -194,7 +199,7 @@ func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (util return nil, code, err } } - wfa.execute(res.(*Workflow)) + wfa.execute(res.(*Workflow), false) return res, code, err } @@ -212,7 +217,7 @@ func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, if code, err := wfa.execution(res.GetID(), res.(*Workflow), false); err != nil { return nil, code, err } - wfa.execute(res.(*Workflow)) + wfa.execute(res.(*Workflow), false) return res, code, err } @@ -220,7 +225,8 @@ func (wfa *workflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, return wfa.GenericStoreOne(data, wfa) } -func (wfa *workflowMongoAccessor) execute(workflow *Workflow) { +func (wfa *workflowMongoAccessor) execute(workflow *Workflow, delete bool) { + accessor := (&workspace.Workspace{}).GetAccessor(nil) filters := &dbs.Filters{ Or: map[string][]dbs.Filter{ @@ -228,6 +234,12 @@ func (wfa *workflowMongoAccessor) execute(workflow *Workflow) { }, } resource, _, err := accessor.Search(filters, "") + if delete { + for _, r := range resource { + accessor.DeleteOne(r.GetID()) + } + return + } if err == nil && len(resource) > 0 { accessor.UpdateOne(&workspace.Workspace{ Active: true, @@ -262,7 +274,7 @@ func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error return nil, code, err } res_mongo.Decode(&workflow) - wfa.execute(&workflow) + wfa.execute(&workflow, false) return &workflow, 200, nil }