Execution
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package oclib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
"github.com/vk496/cron"
|
||||
)
|
||||
|
||||
type WorkflowMongoAccessor struct {
|
||||
@@ -13,12 +17,64 @@ func (wfa *WorkflowMongoAccessor) DeleteOne(id string) (utils.DBObject, int, err
|
||||
return wfa.GenericDeleteOne(id, wfa)
|
||||
}
|
||||
|
||||
func (wfa *WorkflowMongoAccessor) execution(realData *Workflow, delete bool) (int, error) {
|
||||
if delete {
|
||||
mongo.MONGOService.DeleteMultiple(map[string]interface{}{
|
||||
"workflow_id": realData.UUID,
|
||||
}, wfa.GetType())
|
||||
}
|
||||
if realData.Schedule != nil {
|
||||
accessor := workflow_execution.WorkflowExecutionMongoAccessor{}
|
||||
if realData.Schedule.Start.IsZero() {
|
||||
return 422, errors.New("should get a start date on the scheduler.")
|
||||
}
|
||||
if len(realData.Schedule.Cron) > 0 {
|
||||
if realData.Schedule.End.IsZero() {
|
||||
return 422, errors.New("a cron task should got a end date.")
|
||||
}
|
||||
c, err := cron.Parse(realData.Schedule.Cron)
|
||||
if err != nil {
|
||||
return 422, errors.New("Bad cron message: " + err.Error())
|
||||
}
|
||||
for s := c.Next(realData.Schedule.Start); !s.IsZero() && s.Before(realData.Schedule.End); s = c.Next(s) {
|
||||
obj := &workflow_execution.WorkflowExecution{
|
||||
ExecDate: s,
|
||||
EndDate: realData.Schedule.End,
|
||||
State: workflow_execution.SCHEDULED,
|
||||
WorkflowID: realData.UUID,
|
||||
}
|
||||
accessor.StoreOne(obj)
|
||||
}
|
||||
|
||||
} else {
|
||||
obj := &workflow_execution.WorkflowExecution{
|
||||
ExecDate: realData.Schedule.Start,
|
||||
EndDate: realData.Schedule.End,
|
||||
State: workflow_execution.SCHEDULED,
|
||||
WorkflowID: realData.UUID,
|
||||
}
|
||||
accessor.StoreOne(obj)
|
||||
}
|
||||
}
|
||||
return 200, nil
|
||||
}
|
||||
|
||||
func (wfa *WorkflowMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
if code, err := wfa.execution(set.(*Workflow), true); err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
return wfa.GenericUpdateOne(set, id, wfa)
|
||||
}
|
||||
|
||||
func (wfa *WorkflowMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data, wfa)
|
||||
res, code, err := wfa.GenericStoreOne(data, wfa)
|
||||
if err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
if code, err := wfa.execution(res.(*Workflow), false); err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
return res, code, err
|
||||
}
|
||||
|
||||
func (wfa *WorkflowMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
|
@@ -3,13 +3,13 @@ package oclib
|
||||
import "time"
|
||||
|
||||
type WorkflowSchedule struct {
|
||||
Id string `json:"id"`
|
||||
Start time.Time
|
||||
End time.Time
|
||||
Cron string
|
||||
Id string `json:"id"`
|
||||
Start time.Time `json:"start" bson:"start validate:"required""`
|
||||
End time.Time `json:"end,omitempty" bson:"end,omitempty"`
|
||||
Cron string `json:"cron,omitempty" bson:"cron,omitempty"`
|
||||
}
|
||||
|
||||
func (ws *WorkflowSchedule) GetAllDates() (timetable []time.Time){
|
||||
func (ws *WorkflowSchedule) GetAllDates() (timetable []time.Time) {
|
||||
// Return all the execution time generated by the Cron
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user