forked from simplify-framework/simplify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.js
84 lines (76 loc) · 4.46 KB
/
graphql.js
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
'use strict';
const path = require('path')
if (typeof __non_webpack_require__ === 'undefined') global.__non_webpack_require__ = require
class StateExecution {
constructor({ executionPath, executionName, verbosity }) {
this.executionPath = executionPath || path.join(__dirname, "Functions")
this.executionName = executionName
this.verbosity = verbosity
this.STATE_FINISH = [ "DONE", "ERROR" ]
}
verbose(...args) {
if (this.verbosity) {
console.log(...args)
} else {
console.log(`${args[0]}${args.length>1 ? " (***hidden***)" : "."}`)
}
}
isFinished(state) {
return this.STATE_FINISH.indexOf(state) === -1 ? false : true
}
runNextExecution({ event, context }, stateObject, states) {
const _thisFunction = this
return new Promise((resolve, reject) => {
const _modulePath = `${path.join(_thisFunction.executionPath, stateObject.Run)}`
const fModule = __non_webpack_require__(`${_thisFunction.executionName}`)
let _stateFunction = __non_webpack_require__(_modulePath).handler
if (process.env.MONOLITHIC_CODE == "YES" && fModule[stateObject.Run]) {
_stateFunction = fModule[stateObject.Run].handler
}
_thisFunction.verbose(`StateExecution:RUN_CONTEXT name = ${stateObject.Run} args =`, JSON.stringify(event.args))
_stateFunction(event, context, function (err, data) {
if (err && !_thisFunction.isFinished(stateObject.Other)) {
event.dataContext = data
event.errorContext = err
event.retryState = event.retryState || stateObject.Retry
if (event.retryState && --event.retryState > 0) {
_thisFunction.verbose(`StateExecution:RETRY_CONTEXT name = ${stateObject.Run} count = ${event.retryState}`)
_thisFunction.runNextExecution({ event, context }, stateObject, states).then(data => resolve(data)).catch(err => reject(err))
} else {
const nextState = states.find(state => state.Run === stateObject.Other)
if (!nextState) reject({ message: `The execution state is not available: ${stateObject.Other}` })
_thisFunction.verbose(`StateExecution:RESULT name = ${stateObject.Run} error =`, JSON.stringify(event.errorContext))
_thisFunction.runNextExecution({ event, context }, nextState, states).then(data => resolve(data)).catch(err => reject(err))
}
} else if (err && _thisFunction.isFinished(stateObject.Other)) {
event.retryState = event.retryState || stateObject.Retry
if (event.retryState && --event.retryState > 0) {
_thisFunction.verbose(`StateExecution:RETRY_CONTEXT name = ${stateObject.Run} count = ${event.retryState}`)
_thisFunction.runNextExecution({ event, context }, stateObject, states).then(data => resolve(data)).catch(err => reject(err))
} else {
_thisFunction.verbose(`StateExecution:ERROR name = ${stateObject.Run} error =`, JSON.stringify(err))
reject(err)
}
} else if (!err && !_thisFunction.isFinished(stateObject.Next)) {
event.dataContext = data
event.errorContext = err
const nextState = states.find(state => state.Run === stateObject.Next)
if (!nextState) reject({ message: `The execution state is not available: ${stateObject.Next}` })
_thisFunction.verbose(`StateExecution:RESULT name = ${stateObject.Run} data =`, JSON.stringify(event.dataContext))
_thisFunction.runNextExecution({ event, context }, nextState, states).then(data => resolve(data)).catch(err => reject(err))
} else if (!err && _thisFunction.isFinished(stateObject.Next)) {
_thisFunction.verbose(`StateExecution:FINISH name = ${stateObject.Run} data =`, JSON.stringify(data))
resolve(data)
}
})
})
}
execute(states, args, dataType, dataSchema) {
return this.runNextExecution({ event: {
...args, dataType, dataSchema
}, context: args.context }, states[0], states)
}
}
module.exports = {
StateExecution
}