-
Notifications
You must be signed in to change notification settings - Fork 200
/
utils.go
250 lines (189 loc) · 6.93 KB
/
utils.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// MIT License
// Copyright (c) [2022] [Bohdan Ivashko (https://github.com/Arriven)]
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package job
import (
"context"
"encoding/base64"
"fmt"
"time"
"github.com/mitchellh/mapstructure"
"github.com/robertkrimen/otto"
"go.uber.org/zap"
"github.com/Arriven/db1000n/src/job/config"
"github.com/Arriven/db1000n/src/utils"
"github.com/Arriven/db1000n/src/utils/metrics"
"github.com/Arriven/db1000n/src/utils/templates"
)
func logJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (
data any, err error, //nolint:unparam // data is here to match Job
) {
var jobConfig struct {
Text string
}
if err := mapstructure.Decode(args, &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
logger.Info(templates.ParseAndExecute(logger, jobConfig.Text, ctx))
return nil, nil
}
func setVarJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (data any, err error) {
var jobConfig struct {
Value string
}
if err := mapstructure.Decode(args, &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
return templates.ParseAndExecute(logger, jobConfig.Value, ctx), nil
}
func checkJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (
data any, err error, //nolint:unparam // data is here to match Job
) {
var jobConfig struct {
Value string
}
if err := mapstructure.Decode(args, &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
if templates.ParseAndExecute(logger, jobConfig.Value, ctx) != "true" {
return nil, fmt.Errorf("validation failed %v", jobConfig.Value)
}
return nil, nil
}
func sleepJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (data any, err error) {
var jobConfig struct {
Value time.Duration
}
if err := utils.Decode(args, &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
time.Sleep(jobConfig.Value)
return nil, nil
}
func discardErrorJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (
data any, err error, //nolint:unparam // data is here to match Job
) {
var jobConfig struct {
BasicJobConfig
Job config.Config
}
if err := ParseConfig(&jobConfig, args, *globalConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
job := Get(jobConfig.Job.Type)
if job == nil {
logger.Debug("unknown job, discarding", zap.String("job", jobConfig.Job.Type))
return nil, nil
}
data, err = job(ctx, jobConfig.Job.Args, globalConfig, a, logger)
if err != nil {
logger.Debug("discarded error", zap.Error(err))
}
return data, nil
}
func timeoutJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (
data any, err error, //nolint:unparam // data is here to match Job
) {
var jobConfig struct {
BasicJobConfig
Timeout time.Duration
Job config.Config
}
if err := ParseConfig(&jobConfig, args, *globalConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
ctx, cancel := context.WithTimeout(ctx, jobConfig.Timeout)
defer cancel()
job := Get(jobConfig.Job.Type)
if job == nil {
return nil, fmt.Errorf("unknown job %q", jobConfig.Job.Type)
}
return job(ctx, jobConfig.Job.Args, globalConfig, a, logger)
}
func loopJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (data any, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var jobConfig struct {
BasicJobConfig
Job config.Config
}
if err := ParseConfig(&jobConfig, args, *globalConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
for jobConfig.Next(ctx) {
job := Get(jobConfig.Job.Type)
if job == nil {
return nil, fmt.Errorf("unknown job %q", jobConfig.Job.Type)
}
data, err := job(ctx, jobConfig.Job.Args, globalConfig, a, logger)
if err != nil {
return nil, fmt.Errorf("error running job: %w", err)
}
ctx = context.WithValue(ctx, templates.ContextKey("data."+jobConfig.Job.Name), data)
}
return nil, nil
}
func jsJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (
data any, err error,
) {
var jobConfig struct {
Script string
Data map[string]any
}
if err := mapstructure.Decode(templates.ParseAndExecuteMapStruct(logger, args, ctx), &jobConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
vm := otto.New()
for key, value := range jobConfig.Data {
if err := vm.Set(key, value); err != nil {
return nil, fmt.Errorf("error setting script data: %w", err)
}
}
return vm.Run(jobConfig.Script)
}
func encryptedJob(ctx context.Context, args config.Args, globalConfig *GlobalConfig, a *metrics.Accumulator, logger *zap.Logger) (data any, err error) {
if globalConfig.SkipEncrypted {
return nil, fmt.Errorf("app is configured to skip encrypted jobs")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var jobConfig struct {
BasicJobConfig
Format string
Data string
}
if err := ParseConfig(&jobConfig, args, *globalConfig); err != nil {
return nil, fmt.Errorf("error parsing job config: %w", err)
}
decoded, err := base64.StdEncoding.DecodeString(jobConfig.Data)
if err != nil {
return nil, err
}
decrypted, err := utils.Decrypt(decoded)
if err != nil {
return nil, err
}
var jobCfg config.Config
if err = utils.Unmarshal(decrypted, &jobCfg, jobConfig.Format); err != nil {
return nil, err
}
job := Get(jobCfg.Type)
if job == nil {
return nil, fmt.Errorf("unknown job %q", jobCfg.Type)
}
return job(ctx, jobCfg.Args, globalConfig, nil, zap.NewNop())
}