Execution
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
w "cloud.o-forge.io/core/oc-lib/models/resources/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
w2 "cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
|
||||
)
|
||||
|
||||
var models = map[string]func() utils.DBObject{
|
||||
@@ -19,6 +20,7 @@ var models = map[string]func() utils.DBObject{
|
||||
utils.STORAGE_RESOURCE.String(): func() utils.DBObject { return &s.StorageResource{} },
|
||||
utils.PROCESSING_RESOURCE.String(): func() utils.DBObject { return &p.ProcessingResource{} },
|
||||
utils.WORKFLOW.String(): func() utils.DBObject { return &w2.Workflow{} },
|
||||
utils.WORKFLOW_EXECUTION.String(): func() utils.DBObject { return &workflow_execution.WorkflowExecution{} },
|
||||
}
|
||||
|
||||
func Model(model int) utils.DBObject {
|
||||
|
||||
@@ -10,6 +10,7 @@ const (
|
||||
DATACENTER_RESOURCE
|
||||
WORKFLOW_RESOURCE
|
||||
WORKFLOW
|
||||
WORKFLOW_EXECUTION
|
||||
)
|
||||
|
||||
var str = [...]string{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
83
models/workflow_execution/workflow_execution.go
Normal file
83
models/workflow_execution/workflow_execution.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package workflow_execution
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ScheduledType int
|
||||
|
||||
const (
|
||||
SCHEDULED ScheduledType = iota
|
||||
STARTED
|
||||
FAILURE
|
||||
SUCCESS
|
||||
)
|
||||
|
||||
var str = [...]string{
|
||||
"scheduled",
|
||||
"started",
|
||||
"failure",
|
||||
"success",
|
||||
}
|
||||
|
||||
func FromInt(i int) string {
|
||||
return str[i]
|
||||
}
|
||||
|
||||
func (d ScheduledType) String() string {
|
||||
return str[d]
|
||||
}
|
||||
|
||||
// EnumIndex - Creating common behavior - give the type a EnumIndex functio
|
||||
func (d ScheduledType) EnumIndex() int {
|
||||
return int(d)
|
||||
}
|
||||
|
||||
type WorkflowExecution struct {
|
||||
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
|
||||
ExecDate time.Time `json:"execution_date,omitempty" bson:"execution_date,omitempty" validate:"required"`
|
||||
EndDate time.Time `json:"end_date,omitempty" bson:"end_date,omitempty"`
|
||||
State ScheduledType `json:"state,omitempty" bson:"state,omitempty" `
|
||||
WorkflowID string `json:"workflow_id" bson:"workflow_id,omitempty"`
|
||||
}
|
||||
|
||||
func (ao *WorkflowExecution) GetID() string {
|
||||
return ao.UUID
|
||||
}
|
||||
|
||||
func (r *WorkflowExecution) GenerateID() {
|
||||
r.UUID = uuid.New().String()
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) GetName() string {
|
||||
return d.UUID + "_" + d.ExecDate.String()
|
||||
}
|
||||
|
||||
func (d *WorkflowExecution) GetAccessor() utils.Accessor {
|
||||
data := &WorkflowExecutionMongoAccessor{}
|
||||
data.SetLogger(utils.WORKFLOW)
|
||||
return data
|
||||
}
|
||||
|
||||
func (dma *WorkflowExecution) Deserialize(j map[string]interface{}) utils.DBObject {
|
||||
b, err := json.Marshal(j)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, dma)
|
||||
return dma
|
||||
}
|
||||
|
||||
func (dma *WorkflowExecution) Serialize() map[string]interface{} {
|
||||
var m map[string]interface{}
|
||||
b, err := json.Marshal(dma)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
json.Unmarshal(b, dma)
|
||||
return m
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package workflow_execution
|
||||
|
||||
import (
|
||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||
)
|
||||
|
||||
type WorkflowExecutionMongoAccessor struct {
|
||||
utils.AbstractAccessor
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericDeleteOne(id, wfa)
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
|
||||
return wfa.GenericUpdateOne(set, id, wfa)
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data, wfa)
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||
return wfa.GenericStoreOne(data, wfa)
|
||||
}
|
||||
|
||||
func (wfa *WorkflowExecutionMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
|
||||
var workflow WorkflowExecution
|
||||
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
res_mongo.Decode(&workflow)
|
||||
return &workflow, 200, nil
|
||||
}
|
||||
|
||||
func (wfa WorkflowExecutionMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
|
||||
objs := []utils.ShallowDBObject{}
|
||||
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
|
||||
if err != nil {
|
||||
wfa.Logger.Error().Msg("Could not retrieve any from db. Error: " + err.Error())
|
||||
return nil, code, err
|
||||
}
|
||||
for res_mongo.Next(mongo.MngoCtx) {
|
||||
var obj utils.AbstractObject
|
||||
res_mongo.Decode(&obj)
|
||||
objs = append(objs, &obj)
|
||||
}
|
||||
return objs, 200, nil
|
||||
}
|
||||
Reference in New Issue
Block a user