diff --git a/models/workflow/workflow_mongo_accessor.go b/models/workflow/workflow_mongo_accessor.go index 865d579..08645a9 100644 --- a/models/workflow/workflow_mongo_accessor.go +++ b/models/workflow/workflow_mongo_accessor.go @@ -242,20 +242,21 @@ func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (util if code != 200 { return nil, 409, err } + d := set.(*Workflow) + if d.ScheduleActive && d.Schedule != nil { // if the workflow is scheduled, update the executions + now := time.Now().UTC() + if (d.Schedule.End != nil && now.After(*d.Schedule.End)) || (d.Schedule.End == nil && d.Schedule.Start != nil && now.After(*d.Schedule.Start)) { // if the start date is passed, then you can book + d.ScheduleActive = false + } // if the start date is passed, update the executions + } // avoid the update if the schedule is the same avoid := set.(*Workflow).Schedule == nil || (res.(*Workflow).Schedule != nil && res.(*Workflow).ScheduleActive == set.(*Workflow).ScheduleActive && res.(*Workflow).Schedule.Start == set.(*Workflow).Schedule.Start && res.(*Workflow).Schedule.End == set.(*Workflow).Schedule.End && res.(*Workflow).Schedule.Cron == set.(*Workflow).Schedule.Cron) - res, code, err = wfa.GenericUpdateOne(set, id, wfa, &Workflow{}) + res, code, err = wfa.GenericUpdateOne(d, id, wfa, &Workflow{}) if code != 200 { return nil, code, err } 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.GenericRawUpdateOne(workflow, id, wfa) - } // 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, workflow, false); code != 200 { return nil, code, errors.New("could not update the executions : " + err.Error()) @@ -268,18 +269,19 @@ func (wfa *workflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (util // StoreOne stores a workflow in the database func (wfa *workflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) { - res, code, err := wfa.GenericStoreOne(data, wfa) + d := data.(*Workflow) + if d.ScheduleActive && d.Schedule != nil { // if the workflow is scheduled, update the executions + now := time.Now().UTC() + if (d.Schedule.End != nil && now.After(*d.Schedule.End)) || (d.Schedule.End == nil && d.Schedule.Start != nil && now.After(*d.Schedule.Start)) { // if the start date is passed, then you can book + d.ScheduleActive = false + } // if the start date is passed, update the executions + } + res, code, err := wfa.GenericStoreOne(d, wfa) if err != nil || code != 200 { return nil, code, err } 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.GenericRawUpdateOne(workflow, workflow.UUID, wfa) - } // 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(), workflow, false); err != nil {