github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/workflow.go (about)

     1  package funcs
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/mdaxf/iac/com"
     8  	"github.com/mdaxf/iac/workflow"
     9  )
    10  
    11  type WorkFlowFunc struct{}
    12  
    13  func (w *WorkFlowFunc) Execute_Explode(f *Funcs) {
    14  	// function execution start time
    15  	startTime := time.Now()
    16  	defer func() {
    17  		// calculate elapsed time
    18  		elapsed := time.Since(startTime)
    19  		// log performance with duration
    20  		f.iLog.PerformanceWithDuration("engine.funcs.WorkFlow.Execute", elapsed)
    21  	}()
    22  
    23  	defer func() {
    24  		// recover from any panics
    25  		if err := recover(); err != nil {
    26  			// log the error
    27  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
    28  			// cancel execution and set error message
    29  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
    30  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err)
    31  		}
    32  	}()
    33  
    34  	namelist, valuelist, _ := f.SetInputs()
    35  
    36  	EntityName := ""
    37  	EntityType := ""
    38  	WorkFlowName := ""
    39  	Description := ""
    40  	Data := make(map[string]interface{})
    41  
    42  	for i, name := range namelist {
    43  		Data[name] = valuelist[i]
    44  
    45  		if name == "EntityName" {
    46  			EntityName = valuelist[i]
    47  		} else if name == "EntityType" {
    48  			EntityType = valuelist[i]
    49  		} else if name == "WorkFlowName" {
    50  			WorkFlowName = valuelist[i]
    51  		} else if name == "Description" {
    52  			Description = valuelist[i]
    53  		}
    54  	}
    55  
    56  	if WorkFlowName == "" || EntityName == "" {
    57  		err := fmt.Errorf("There is value for the WorkFlowName and EntityName")
    58  		f.iLog.Error(fmt.Sprintf("failed to create the notification: %v", err))
    59  		f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
    60  		return
    61  	}
    62  	f.iLog.Debug(fmt.Sprintf("%s, %s", Description, EntityType))
    63  
    64  	wfe := workflow.NewExplosion(WorkFlowName, EntityName, EntityType, f.SystemSession["UserName"].(string), "")
    65  	wfentityid, err := wfe.Explode(Description, Data)
    66  
    67  	if err != nil {
    68  		f.iLog.Error(fmt.Sprintf("failed to create the notification: %v", err))
    69  		f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
    70  		return
    71  	}
    72  	outputs := make(map[string]interface{})
    73  	outputs["WorkFloeEntityID"] = wfentityid
    74  	f.SetOutputs(outputs)
    75  
    76  }
    77  
    78  func (w *WorkFlowFunc) Execute_StartTask(f *Funcs) {
    79  	// function execution start time
    80  	startTime := time.Now()
    81  	defer func() {
    82  		// calculate elapsed time
    83  		elapsed := time.Since(startTime)
    84  		// log performance with duration
    85  		f.iLog.PerformanceWithDuration("engine.funcs.WorkFlow.Execute", elapsed)
    86  	}()
    87  
    88  	defer func() {
    89  		// recover from any panics
    90  		if err := recover(); err != nil {
    91  			// log the error
    92  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
    93  			// cancel execution and set error message
    94  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
    95  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err)
    96  		}
    97  	}()
    98  
    99  	namelist, valuelist, _ := f.SetInputs()
   100  
   101  	TaskID := 0
   102  
   103  	for i, name := range namelist {
   104  		if name == "TaskID" {
   105  			TaskID = com.ConverttoIntwithDefault(valuelist[i], 0)
   106  		}
   107  	}
   108  
   109  	if TaskID == 0 {
   110  		err := fmt.Errorf("TaskID cannot be 0 or no value")
   111  		f.iLog.Error(fmt.Sprintf("failed to get the input : %v", err))
   112  		f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
   113  		return
   114  	}
   115  	user := f.SystemSession["UserName"].(string)
   116  
   117  	wft := workflow.NewWorkFlowTaskType(int64(TaskID), user)
   118  	err := wft.StartTask()
   119  
   120  	if err != nil {
   121  
   122  		f.iLog.Error(fmt.Sprintf("failed to start the tasks for the user %s with error: %v", user, err))
   123  		f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
   124  		return
   125  	}
   126  }
   127  
   128  func (w *WorkFlowFunc) Execute_CompleteTask(f *Funcs) {
   129  	// function execution start time
   130  	startTime := time.Now()
   131  	defer func() {
   132  		// calculate elapsed time
   133  		elapsed := time.Since(startTime)
   134  		// log performance with duration
   135  		f.iLog.PerformanceWithDuration("engine.funcs.WorkFlow.Execute", elapsed)
   136  	}()
   137  
   138  	defer func() {
   139  		// recover from any panics
   140  		if err := recover(); err != nil {
   141  			// log the error
   142  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
   143  			// cancel execution and set error message
   144  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
   145  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err)
   146  		}
   147  	}()
   148  
   149  	namelist, valuelist, _ := f.SetInputs()
   150  
   151  	TaskID := 0
   152  
   153  	for i, name := range namelist {
   154  		if name == "TaskID" {
   155  			TaskID = com.ConverttoIntwithDefault(valuelist[i], 0)
   156  		}
   157  	}
   158  
   159  	if TaskID == 0 {
   160  		err := fmt.Errorf("TaskID cannot be 0 or no value")
   161  		f.iLog.Error(fmt.Sprintf("failed to get the input : %v", err))
   162  		f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
   163  		return
   164  	}
   165  	user := f.SystemSession["UserName"].(string)
   166  
   167  	wft := workflow.NewWorkFlowTaskType(int64(TaskID), user)
   168  	err := wft.CompleteTask()
   169  
   170  	if err != nil {
   171  
   172  		f.iLog.Error(fmt.Sprintf("failed to complete the tasks for the user %s with error: %v", user, err))
   173  		f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.WorkFlow.Execute with error: %s", err))
   174  		return
   175  	}
   176  }