Execution

This commit is contained in:
mr
2024-07-23 16:14:46 +02:00
parent 806f5d0f20
commit 00f25b48c0
9 changed files with 222 additions and 7 deletions

View 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
}

View File

@@ -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
}