/
runner_job.go
137 lines (126 loc) · 4.55 KB
/
runner_job.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package models
import (
"fmt"
"github.com/dotenx/dotenx/ao-api/config"
)
type Job struct {
ExecutionId int `json:"executionId"`
TaskId int `json:"taskId"`
Timeout int `json:"timeout"`
Name string `json:"name"`
Type string `json:"type"`
AwsLambda string `json:"aws_lambda"`
Image string `json:"image"`
AccountId string `json:"account_id"`
Body map[string]interface{} `json:"body"`
MetaData TaskDefinition `json:"task_meta_data"`
ResultEndpoint string `json:"result_endpoint"`
WorkSpace string `json:"workspace"`
}
// creates a new job dto for runner based on given task for certain execution
func NewJob(task TaskDetails, executionId int, accountId string) *Job {
image := AvaliableTasks[task.Type].Image
return &Job{
ExecutionId: executionId,
TaskId: task.Id,
Type: task.Type,
AwsLambda: task.AwsLambda,
Timeout: task.Timeout,
Image: image,
Body: nil,
Name: task.Name,
AccountId: accountId,
MetaData: AvaliableTasks[task.Type],
ResultEndpoint: fmt.Sprintf("%s/execution/id/%d/task/%d/result", config.Configs.Endpoints.AoApi, executionId, task.Id),
}
}
// add integration fields to job body and task meta data fields
func (job *Job) SetIntegration(integration Integration) {
for key, value := range integration.Secrets {
k := "INTEGRATION_" + key
// for inputNumber, _ := range job.Body {
job.Body[k] = value
// }
job.MetaData.Fields = append(job.MetaData.Fields, TaskField{Key: k, Type: "text"})
}
}
// AddDynamicValuesToMetaData adds dynamic value keys to task meta data
func (job *Job) AddDynamicValuesToMetaData() {
dynamicValueKeys := make([]string, 0)
// for inputNumber, _ := range job.Body {
for key, _ := range job.Body {
if !job.isInFields(key) {
dynamicValueKeys = append(dynamicValueKeys, key)
}
}
// break
// }
if len(dynamicValueKeys) > 0 {
job.MetaData.Fields = append(job.MetaData.Fields, TaskField{Key: "DYNMAIC_VARIABLE_KEYS", Type: "array"})
job.MetaData.Fields = append(job.MetaData.Fields, TaskField{Key: "DYNMAIC_VARIABLES", Type: "json"})
// for inputNumber, _ := range job.Body {
dynamicValues := make(map[string]interface{})
job.Body["DYNMAIC_VARIABLE_KEYS"] = dynamicValueKeys
for _, key := range dynamicValueKeys {
dynamicValues[key] = job.Body[key]
}
job.Body["DYNMAIC_VARIABLES"] = dynamicValues
// }
}
}
func (job *Job) isInFields(key string) bool {
for _, field := range job.MetaData.Fields {
if field.Key == key {
return true
}
}
return false
}
// TODO change this and use AddDynamicValuesToMetaData logic to handle it
func (job *Job) SetRunCodeFields() {
variables := ""
// for inputNumber, _ := range job.Body {
for key, _ := range job.Body {
if key != "code" && key != "dependency" {
if variables != "" {
variables += ","
}
variables += key
// TODO check here if field is already exist in task meta data
job.MetaData.Fields = append(job.MetaData.Fields, TaskField{Key: key, Type: "text"})
}
}
// TODO check here if field is already exist in task meta data
job.MetaData.Fields = append(job.MetaData.Fields, TaskField{Key: "VARIABLES", Type: "text"})
job.Body["VARIABLES"] = variables
// }
}
func (job *Job) PrepRunMiniTasks() {
// for taskName, task := range manifest.Tasks {
// if task.Type == "Custom task" {
// task.Type = "Run node code"
// }
// manifest.Tasks[taskName] = task
// }
// return manifest, nil
job.Type = "Run node code"
job.MetaData = AvaliableTasks["Run node code"]
// for inputNumber, _ := range job.Body {
// logrus.Info(job.Body)
// logrus.Info(job.Body["tasks"])
// importStore := miniTasks.NewImportStore()
// parsed := job.Body[inputNumber]["tasks"].(map[string]interface{})
// code, err := miniTasks.ConvertToCode(parsed["steps"].([]interface{}), &importStore)
// if err != nil {
// fmt.Println(err)
// }
// fmt.Println(`********************************************************************************`)
// fmt.Println(code)
// fmt.Println(`********************************************************************************`)
// job.Body[inputNumber]["code"] = fmt.Sprintf("module.exports = () => {\n%s\n}", code)
// job.Body[inputNumber]["VARIABLES"] = "outputs"
// job.Body[inputNumber]["outputs"] = make([]string, 0)
// job.Body[inputNumber]["dependency"] = "{}"
delete(job.Body, "tasks")
// }
}