diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index c30906e..8cc7d50 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -5,6 +5,7 @@ import ( "fmt" "slices" "strings" + "time" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/dbs/mongo" @@ -247,14 +248,21 @@ func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (util if code != 200 { return nil, code, err } - fmt.Println("UPDATE", avoid, res.(*Workflow).ScheduleActive) + workflow := res.(*Workflow) + if workflow.ScheduleActive && workflow.Schedule != nil { // if the workflow is scheduled, update the executions + now := time.Now().UTC() + if (workflow.Schedule.End != nil && now.After(*workflow.Schedule.End)) || (workflow.Schedule.End == nil && workflow.Schedule.Start != nil && now.After(*workflow.Schedule.Start)) { // if the start date is passed, then you can book + workflow.ScheduleActive = false + wfa.UpdateOne(workflow, id) + } // if the start date is passed, update the executions + } if !avoid { // if the schedule is not avoided, update the executions - if code, err := wfa.execution(id, res.(*Workflow), false); code != 200 { + if code, err := wfa.execution(id, workflow, false); code != 200 { return nil, code, errors.New("could not update the executions : " + err.Error()) } } - wfa.execute(res.(*Workflow), false, false) // update the workspace for the workflow - wfa.share(res.(*Workflow), false, wfa.Caller) // share the update to the peers + wfa.execute(workflow, false, false) // update the workspace for the workflow + wfa.share(workflow, false, wfa.Caller) // share the update to the peers return res, code, nil } @@ -264,12 +272,20 @@ func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, if err != nil || code != 200 { return nil, code, err } - wfa.share(res.(*Workflow), false, wfa.Caller) // share the creation to the peers + workflow := res.(*Workflow) + if workflow.ScheduleActive && workflow.Schedule != nil { // if the workflow is scheduled, update the executions + now := time.Now().UTC() + if (workflow.Schedule.End != nil && now.After(*workflow.Schedule.End)) || (workflow.Schedule.End == nil && workflow.Schedule.Start != nil && now.After(*workflow.Schedule.Start)) { // if the start date is passed, then you can book + workflow.ScheduleActive = false + wfa.UpdateOne(workflow, workflow.UUID) + } // if the start date is passed, update the executions + } + wfa.share(workflow, false, wfa.Caller) // share the creation to the peers //store the executions - if code, err := wfa.execution(res.GetID(), res.(*Workflow), false); err != nil { + if code, err := wfa.execution(res.GetID(), workflow, false); err != nil { return nil, code, err } - wfa.execute(res.(*Workflow), false, false) // store the workspace for the workflow + wfa.execute(workflow, false, false) // store the workspace for the workflow return res, code, nil } @@ -330,8 +346,14 @@ func (wfa *workflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error return nil, code, err } res_mongo.Decode(&workflow) + if workflow.ScheduleActive && workflow.Schedule != nil { // if the workflow is scheduled, update the executions + now := time.Now().UTC() + if (workflow.Schedule.End != nil && now.After(*workflow.Schedule.End)) || (workflow.Schedule.End == nil && workflow.Schedule.Start != nil && now.After(*workflow.Schedule.Start)) { // if the start date is passed, then you can book + workflow.ScheduleActive = false + wfa.UpdateOne(&workflow, id) + } // if the start date is passed, update the executions + } wfa.execute(&workflow, false, true) // if no workspace is attached to the workflow, create it - return &workflow, 200, nil }