-
Notifications
You must be signed in to change notification settings - Fork 35
/
context.go
477 lines (430 loc) · 12.5 KB
/
context.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package endly
import (
"context"
"fmt"
uuid "github.com/satori/go.uuid"
"github.com/viant/afs/url"
"github.com/viant/endly/internal/debug"
"github.com/viant/endly/model/location"
"github.com/viant/endly/model/msg"
"github.com/viant/scy/cred/secret"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
tudf "github.com/viant/toolbox/data/udf"
"github.com/viant/toolbox/storage"
"os"
"os/exec"
"path"
"strings"
"sync"
"sync/atomic"
"time"
)
// EndlyPanic env key name to skip recover in case of panic, export ENDLY_PANIC=true
const EndlyPanic = "ENDLY_PANIC"
var serviceManagerKey = (*manager)(nil)
var deferFunctionsKey = (*[]func())(nil)
// Context represents a workflow session context/state
type Context struct {
context context.Context
SessionID string
CLIEnabled bool
HasLogger bool
AsyncUnsafeKeys map[interface{}]bool
Secrets *secret.Service
Wait *sync.WaitGroup
Listener msg.Listener
Source *location.Resource
Debugger *debug.Debugger
state data.Map
udfs data.Map
Logging *bool
toolbox.Context
cloned []*Context
closed int32
}
func (c *Context) Background() context.Context {
if c.context != nil {
return c.context
}
c.context = context.Background()
return c.context
}
// Publish publishes event to listeners, it updates current run details like activity workflow name etc ...
func (c *Context) Publish(value interface{}) msg.Event {
event, ok := value.(msg.Event)
if !ok {
event = msg.NewEvent(value)
}
event.SetLoggable(c.IsLoggingEnabled())
if c.Listener != nil {
c.Listener(event)
}
return event
}
// PublishWithStartEvent publishes event to listeners, it updates current run details like activity workflow name etc ...
func (c *Context) PublishWithStartEvent(value interface{}, init msg.Event) msg.Event {
event := msg.NewEventWithInit(value, init)
event.SetLoggable(true)
if c.Listener != nil {
c.Listener(event)
}
return event
}
// SetListener sets context event Listener
func (c *Context) SetListener(listener msg.Listener) {
c.Listener = listener
}
// IsClosed returns true if it is closed.
func (c *Context) IsClosed() bool {
return atomic.LoadInt32(&c.closed) == 1
}
// Clone clones the context.
func (c *Context) Clone() *Context {
if len(c.cloned) == 0 {
c.cloned = make([]*Context, 0)
}
result := &Context{}
result.Wait = &sync.WaitGroup{}
result.Context = c.Context.Clone()
result.state = NewDefaultState(c)
result.state.Apply(c.state)
result.SessionID = c.SessionID
result.Listener = c.Listener
result.CLIEnabled = c.CLIEnabled
result.Secrets = c.Secrets
result.AsyncUnsafeKeys = make(map[interface{}]bool)
for k, v := range c.AsyncUnsafeKeys {
result.AsyncUnsafeKeys[k] = v
}
c.cloned = append(c.cloned, result)
return result
}
func (c *Context) parentURLCandidates() []string {
var result = make([]string, 0)
if c.Source != nil {
baseURL, _ := toolbox.URLSplit(c.Source.URL)
result = append(result, baseURL)
}
currentDirectory, err := os.Getwd()
if err == nil {
result = append(result, toolbox.FileSchema+currentDirectory)
}
return result
}
// IsLoggingEnabled returns tru if logging is enabled
func (c *Context) IsLoggingEnabled() bool {
if c.Logging == nil {
return true
}
return *c.Logging
}
// SetLogging set logging on and off
func (c *Context) SetLogging(flag bool) {
c.Logging = &flag
}
// ExpandResource substitutes any $ expression with the key value from the state map if it is present.
func (c *Context) ExpandResource(resource *location.Resource) (*location.Resource, error) {
if resource == nil {
return nil, msg.ReportError(fmt.Errorf("resource was empty"))
}
if resource.URL == "" {
return nil, msg.ReportError(fmt.Errorf("url was empty"))
}
if !strings.Contains(resource.URL, "://") {
for _, parentCandidate := range c.parentURLCandidates() {
service, err := storage.NewServiceForURL(parentCandidate, "")
if err != nil {
continue
}
var candidateURL = url.Join(parentCandidate, resource.URL)
if exists, err := service.Exists(candidateURL); exists && err == nil {
resource.URL = candidateURL
}
}
}
var result = location.NewResource(c.Expand(resource.URL), location.WithCredentials(c.Expand(resource.Credentials)))
result.CustomKey = resource.CustomKey
return result, nil
}
// Manager returns workflow manager or error
func (c *Context) Manager() (Manager, error) {
if c == nil {
return nil, fmt.Errorf("context was nil")
}
var manager = &manager{}
if !c.GetInto(serviceManagerKey, &manager) {
return nil, msg.ReportError(fmt.Errorf("failed to lookup Manager"))
}
return manager, nil
}
// Service returns a service fo provided id or error.
func (c *Context) Service(name string) (Service, error) {
manager, err := c.Manager()
if err != nil {
return nil, err
}
return manager.Service(name)
}
// Deffer add function to be executed if context closes. If returns currently registered functions.
func (c *Context) Deffer(functions ...func()) []func() {
var result *[]func()
if !c.Contains(deferFunctionsKey) {
var functions = make([]func(), 0)
result = &functions
_ = c.Put(deferFunctionsKey, result)
} else {
c.GetInto(deferFunctionsKey, &result)
}
*result = append(*result, functions...)
_ = c.Put(deferFunctionsKey, &result)
return *result
}
// State returns a context state map.
func (c *Context) State() data.Map {
if c.state == nil {
c.state = NewDefaultState(c)
}
return c.state
}
// SetState sets a new state map
func (c *Context) SetState(state data.Map) {
c.state = state
}
// Expand substitute $ expression if present in the text and state map.
func (c *Context) Expand(text string) string {
state := c.State()
return state.ExpandAsText(text)
}
// PublishAndRestore sets supplied value and returns func restoring original values
func (s *Context) PublishAndRestore(values map[string]interface{}) func() {
var backup = map[string]interface{}{}
for k, v := range values {
if value, has := s.state.GetValue(k); has {
backup[k] = value
}
s.state.SetValue(k, v)
}
return func() {
for k, v := range backup {
s.state.SetValue(k, v)
}
}
}
// NewRequest creates a new request for service and action
func (c *Context) NewRequest(serviceName, action string, rawRequest map[string]interface{}) (result interface{}, err error) {
var service Service
service, err = c.Service(serviceName)
if err != nil {
return nil, err
}
route, err := service.Route(action)
if err != nil {
return nil, err
}
defer func() {
if toolbox.AsBoolean(os.Getenv("ENDLY_PANIC")) {
return
}
if r := recover(); r != nil {
var info = toolbox.AsString(rawRequest)
if JSONSource, err := toolbox.AsJSONText(rawRequest); err == nil {
info = JSONSource
}
err = fmt.Errorf("unable to create %v request: %v, %v", serviceName+":"+action, r, info)
}
}()
request := route.RequestProvider()
if route.OnRawRequest != nil {
if err = route.OnRawRequest(c, rawRequest); err != nil {
return nil, err
}
}
err = toolbox.DefaultConverter.AssignConverted(request, rawRequest)
return request, err
}
// AsRequest converts a source map into request for provided service and action.
func (c *Context) AsRequest(serviceName, action string, source map[string]interface{}) (request interface{}, err error) {
expanded := c.state.Expand(source)
source = toolbox.AsMap(expanded)
if request, err = c.NewRequest(serviceName, action, source); err != nil {
return request, fmt.Errorf("unable to create %v request %v", serviceName+":"+action, err)
}
return request, err
}
// Close closes this context, it executes all deferred function and set closed flag.
func (c *Context) Close() {
atomic.StoreInt32(&c.closed, 1)
for _, context := range c.cloned {
context.Close()
}
for _, function := range c.Deffer() {
function()
}
}
//MakeAsyncSafe makes this contex async safe
func (c *Context) MakeAsyncSafe() *msg.Events {
for k := range c.AsyncUnsafeKeys {
c.Context.Remove(k)
}
result := msg.NewEvents()
c.Listener = result.AsListener()
return result
}
var atomicInt int64
/*
NewDefaultState returns a new default state.
It comes with the following registered keys:
* rand - random int64
* date - current date formatted as yyyy-MM-dd
* time - current time formatted as yyyy-MM-dd hh:mm:ss
* ts - current timestamp formatted as yyyyMMddhhmmSSS
* timestamp.XXX - timestamp in ms where XXX is time diff expression i.e 3DaysAgo, tomorrow, hourAhead
* unix.XXX - timestamp in sec where XXX is time diff expression i.e 3DaysAgo, tomorrow, hourAhead
* tzTime.XXX - RFC3339 formatted time where XXX is time diff expression i.e 3DaysAgo, tomorrow, hourAhead
* tmpDir - temp directory
* uuid.next - generate unique id
* uuid.Get - returns previously generated unique id, or generate new
*.env.XXX where XXX is the SessionID of the env variable to return
* all UFD registry functions
*/
func NewDefaultState(ctx *Context) data.Map {
var result = data.NewMap()
result.Put("ts", time.Now().Unix())
if ctx.udfs == nil {
ctx.udfs = predefinedRegistry()
ctx.udfs.Put("secrets", func(key string) interface{} {
if ctx.Secrets == nil {
return ""
}
genericCred, err := ctx.Secrets.GetCredentials(ctx.Background(), key)
if err == nil {
var result = make(map[string]interface{})
if err = toolbox.DefaultConverter.AssignConverted(&result, genericCred); err == nil {
return data.Map(result)
}
}
return ""
})
}
result.Put(data.UDFKey, ctx.udfs)
return result
}
func predefinedRegistry() data.Map {
var result = data.NewMap()
for k, v := range tudf.Predefined {
result[k] = v
}
for k, v := range PredefinedUdfs {
result.Put(k, v)
}
result.Put("minuteofday", func(key string) interface{} {
now := time.Now()
if strings.ToLower(key) == "utc" {
now = now.UTC()
}
return int(now.Minute()) + int(now.Hour())*60
})
result.Put("weekday", func(key string) interface{} {
now := time.Now()
loc := time.UTC
if key != "" {
if l, err := time.LoadLocation(key); err != nil {
loc = l
}
}
return now.In(loc).Weekday()
})
result.Put("tmpDir", func(key string) interface{} {
tempPath := path.Join(os.TempDir(), key)
exec.Command("mkdir -p " + tempPath)
return tempPath
})
var cachedUUID uuid.UUID
result.Put("uuid", func(key string) interface{} {
if key == "next" {
var err error
cachedUUID, err = uuid.NewV4()
if err != nil {
return ""
}
}
if len(cachedUUID) > 0 {
return cachedUUID.String()
}
return ""
})
result.Put("generator", func(key string) interface{} {
switch key {
case "next":
return atomic.AddInt64(&atomicInt, 1)
case "prev":
return atomic.AddInt64(&atomicInt, -1)
case "reset":
atomic.StoreInt64(&atomicInt, 0)
return atomic.LoadInt64(&atomicInt)
default:
return atomic.LoadInt64(&atomicInt)
}
})
//returns time in ms
result.Put("timestamp", func(key string) interface{} {
timeAt, err := toolbox.TimeAt(key)
if err != nil {
return nil
}
return int(timeAt.Unix()+timeAt.UnixNano()) / 1000000
})
//return time in sec
result.Put("unix", func(key string) interface{} {
timeAt, err := toolbox.TimeAt(key)
if err != nil {
return nil
}
return int(timeAt.Unix()+timeAt.UnixNano()) / 1000000000
})
//return fraction of elapsed today in supplied key locale, i.e ${elapsedToday.UTC}
result.Put("elapsedToday", func(key string) interface{} {
elapsed, err := toolbox.ElapsedToday(key)
if err != nil {
return nil
}
return elapsed
})
//return fraction of elapsed today in supplied key timezone, i.e ${remainingToday.Poland}
result.Put("remainingToday", func(key string) interface{} {
remainingToday, err := toolbox.RemainingToday(key)
if err != nil {
return nil
}
return remainingToday
})
//return fraction of elapsed today in supplied key timezone, i.e ${remainingToday.Poland}
result.Put("remainingToday", func(key string) interface{} {
remainingToday, err := toolbox.RemainingToday(key)
if err != nil {
return nil
}
return remainingToday
})
result.Put("packIntWithDate", func(key string) interface{} {
intValue := toolbox.AsInt(key)
now := time.Now()
dateKey := now.Year()*10000 + int(now.Month())*100 + now.Day()
return dateKey<<32 | intValue
})
//return formatted time with time.RFC3339 yyyy-MM-ddThh:mm:ss.SSS Z i.e ${tzTime.4daysAgoInUTC}
result.Put("tzTime", func(key string) interface{} {
timeAt, err := toolbox.TimeAt(key)
if err != nil {
return nil
}
return timeAt.Format(time.RFC3339)
})
// dateKey := r.At.Year()*10000 + int(r.At.Month())*100 + r.At.Day()
// r.ID = (dateKey << 32) | r.OrderID
result.Put("env", func(key string) interface{} {
return os.Getenv(key)
})
return result
}