Skip to content

Commit

Permalink
Now all plugins receive the message on execute, not on construct
Browse files Browse the repository at this point in the history
  • Loading branch information
elboletaire committed Nov 23, 2018
1 parent 05ad689 commit d983eb6
Show file tree
Hide file tree
Showing 20 changed files with 145 additions and 163 deletions.
17 changes: 9 additions & 8 deletions src/worker/ActionCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,28 @@ export default class ActionCreator {
this.event.actions.forEach((action, index) => {
const executer = new ActionExecuter(new Action(action), rabbit, this.event)

const executionPromise = (lastValue, preLog, eventsLenght) => {
if (lastValue === undefined) {
const executionPromise = (contents, preLog, eventsLenght) => {
if (contents === undefined) {
return Promise.reject(new Error('Previous plugin returned undefined'))
}

if (lastValue.id) {
preLog = '[' + lastValue.id + '] > ' + preLog
if (contents.id) {
preLog = '[' + contents.id + '] > ' + preLog
}
debug('Last value received is: ', lastValue)

debug('Last value received is: ', contents)

logger.info(preLog, `Running action ${index + 1} of ${eventsLenght}`)

return executer.execute(contents, lastValue).catch((err) => {
return executer.execute(contents).catch((err) => {
logger.error(preLog, 'Step has failed so ignoring next ones')
return Promise.reject(err)
})
}

promiseChain = promiseChain.then(
(lastValue) => executionPromise(
lastValue,
(contents) => executionPromise(
contents,
this.preLog,
this.event.actions.length
)
Expand Down
54 changes: 26 additions & 28 deletions src/worker/ActionExecuter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,32 @@ describe('ActionExecuter', () => {
}
}

return expect(actionExecuter.execute(msg, {})).resolves
return expect(actionExecuter.execute(msg)).resolves.toBeTruthy()
})

// it('should handle conditional type action', () => {
// const action = new Action({
// name: 'myConditionalAction',
// type: 'conditional',
// options: {
// conditions: {
// field: 'test',
// operation: 'defined'
// }
// },
// event: 'event-name',
// })

// const actionExecuter = new ActionExecuter(action, rabbit, new Event({name: 'test', eventName: 'test', route: 'test', actions: []}))

// const msg = {
// body: {
// test: 'Test',
// test2: 'Test2',
// toString: () => '{test1: "Test", test2: "Test2"}'
// }
// }

// actionExecuter.execute(msg, {}, (err) => {
// expect(err).toBe(null)
// })
// })
it('should handle conditional type action', () => {
const action = new Action({
name: 'myConditionalAction',
type: 'conditional',
options: {
conditions: {
field: 'test',
operation: 'defined'
}
},
event: 'event-name',
})

const actionExecuter = new ActionExecuter(action, rabbit, new Event({name: 'test', eventName: 'test', route: 'test', actions: []}))

const msg = {
body: {
test: 'Test',
test2: 'Test2',
toString: () => '{test1: "Test", test2: "Test2"}'
}
}

return expect(actionExecuter.execute(msg)).resolves.toBeTruthy()
})
})
11 changes: 5 additions & 6 deletions src/worker/ActionExecuter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { ExecutionPluginInterface } from "./ExecutionPluginInterface"

const debug = require('debug')('action-executer')

const loadPlugin = (prevMessage: string, action: Action, preLog: string, rabbit: any) => {
const loadPlugin = (action: Action, preLog: string, rabbit: any) => {
if (plugins[action.type]) {
return new plugins[action.type](prevMessage, action, preLog, rabbit)
return new plugins[action.type](action, preLog, rabbit)
}

return false
Expand All @@ -22,15 +22,14 @@ export default class ActionExecuter {
message: string
public plugin: ExecutionPluginInterface

constructor(action: Action, rabbit: any, event: Event, msg: any) {
constructor(action: Action, rabbit: any, event: Event) {
debug('action executer action received: %j', action)
this.action = action
this.rabbit = rabbit
this.event = event
this.preLog = event.name + ' >'

this.plugin = loadPlugin(
prevMessage,
this.action,
this.preLog,
this.rabbit
Expand All @@ -40,14 +39,14 @@ export default class ActionExecuter {
}

// Instantiate the proper plugin with proper parameters and execute it
execute() {
execute(message: any) {
if (!this.plugin) {
debug(`The plugin cannot be loaded for action: ${this.action}`)
return Promise.reject(new Error(`The plugin cannot be loaded for action: ${JSON.stringify(this.action)}`))
}

// Execute plugin and send result to callback
return this.plugin.execute()
return this.plugin.execute(message)
.then((result) => {
debug('Executed plugin %s with the result %j', this.action.type, result)

Expand Down
3 changes: 1 addition & 2 deletions src/worker/ExecutionPluginInterface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Action from '../model/Action'

export interface ExecutionPluginInterface {
msg: any
action: Action
preLog: string
// these are of type SchemaObjectInstance, but does not work as expected
options: any
execute() : Promise<any>
execute(message: any) : Promise<any>
}
24 changes: 12 additions & 12 deletions src/worker/execution-plugins/conditional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('conditional', () => {
},
event: 'event-name',
})
const conditionalPlugin = new ConditionalPlugin(msg, action, '')
const conditionalPlugin = new ConditionalPlugin(action, '')

describe('checkConditions', () => {
it('should make === operation', () => {
Expand All @@ -37,9 +37,9 @@ describe('conditional', () => {
},
event: 'event-name',
})
const passingConditionalPlugin = new ConditionalPlugin(msg, passingAction, '')
const passingConditionalPlugin = new ConditionalPlugin(passingAction, '')

expect(passingConditionalPlugin.checkConditions()).toBeTruthy()
return expect(passingConditionalPlugin.execute(msg)).resolves.toBeTruthy()
})

it('should make !== operation', () => {
Expand All @@ -57,9 +57,9 @@ describe('conditional', () => {
},
event: 'event-name',
})
const passingConditionalPlugin = new ConditionalPlugin(msg, passingAction, '')
const passingConditionalPlugin = new ConditionalPlugin(passingAction, '')

return expect(passingConditionalPlugin.checkConditions()).toBeFalsy()
return expect(passingConditionalPlugin.execute(msg)).rejects.toEqual({action: 'abort'})
})

it('should return false if the field doesn\'t exist', () => {
Expand All @@ -82,9 +82,9 @@ describe('conditional', () => {
},
event: 'event-name',
})
const passingConditionalPlugin = new ConditionalPlugin(msg, passingAction, '')
const passingConditionalPlugin = new ConditionalPlugin(passingAction, '')

return expect(passingConditionalPlugin.checkConditions()).toBeFalsy()
return expect(passingConditionalPlugin.execute(msg)).rejects.toEqual({action: 'abort'})
})

it('should return false if one of the checks fails', () => {
Expand Down Expand Up @@ -113,14 +113,14 @@ describe('conditional', () => {
event: 'event-name',
})

const nonPassingConditionalPlugin = new ConditionalPlugin(msg, nonPassingAction, '')
const nonPassingConditionalPlugin = new ConditionalPlugin(nonPassingAction, '')

return expect(nonPassingConditionalPlugin.checkConditions()).toBeFalsy()
return expect(nonPassingConditionalPlugin.execute(msg)).rejects.toEqual({action: 'abort'})
})
})

describe('execute', () => {
it('should return a promise', () => {
it('should return a Promise', () => {
const passingAction = new Action({
name: 'testing-checks',
type: 'conditional',
Expand All @@ -135,10 +135,10 @@ describe('conditional', () => {
},
event: 'event-name',
})
const passingConditionalPlugin = new ConditionalPlugin(msg, passingAction, '')
const passingConditionalPlugin = new ConditionalPlugin(passingAction, '')

// It'll reject, as it does not receive proper options
return expect(passingConditionalPlugin.execute()).toBeInstanceOf(Promise)
return expect(passingConditionalPlugin.execute(msg)).toBeInstanceOf(Promise)
})
})
})
13 changes: 5 additions & 8 deletions src/worker/execution-plugins/conditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,14 @@ const flattenObject = (ob) => {
}

export default class ConditionalPlugin implements ExecutionPluginInterface {
msg: any
action: Action
preLog: string
parsedMessage: object
options: any

constructor(msg, action, preLog) {
this.msg = msg
constructor(action, preLog) {
this.action = action
this.preLog = preLog + ' > ' + action.name
this.parsedMessage = flattenObject(
this.msg
)

this.options = new ConditionalPluginOptionsSchema(action.options)
}
Expand Down Expand Up @@ -88,15 +83,17 @@ export default class ConditionalPlugin implements ExecutionPluginInterface {
return retValue
}

execute() {
execute(message: any) {
this.parsedMessage = flattenObject(message)

return new Promise((resolve, reject) => {
if (!this.checkConditions()) {
logger.info(this.preLog, 'Some conditional check has failed')
return reject({action: 'abort'})
}

logger.info(this.preLog, ': All conditional checks has been passed')
return resolve(this.msg)
return resolve(message)
})
}
}
12 changes: 6 additions & 6 deletions src/worker/execution-plugins/http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ describe('execution-plugins :: http', () => {
},
event: 'name-event',
})
const httpPlugin = new HttpPlugin(msg, action, 'test')
const httpPlugin = new HttpPlugin(action, 'test')
const mock = new MockAdapter(axios)

mock.onGet(/topology/).reply(200, {
myTopology: 'test'
})

it('should return a promise', () => {
expect(httpPlugin.execute()).toBeInstanceOf(Promise)
expect(httpPlugin.execute(msg)).toBeInstanceOf(Promise)
})

it('should make the http request', () => {
expect.assertions(2)
return httpPlugin.execute().then((result: any) => {
return httpPlugin.execute(msg).then((result: any) => {
expect(typeof result).toEqual('object')
return expect(result.myTopology).toEqual('test')
})
Expand All @@ -47,7 +47,7 @@ describe('execution-plugins :: http', () => {
it('should embed request response to the previous value', () => {
expect.assertions(3)

return httpPlugin.execute().then((result: any) => {
return httpPlugin.execute(msg).then((result: any) => {
expect(typeof result).toEqual('object')
expect(result.myTopology).toEqual('test')

Expand All @@ -67,9 +67,9 @@ describe('execution-plugins :: http', () => {
},
event: 'name-event',
})
const httpPlugin = new HttpPlugin(msg, action, 'test')
const httpPlugin = new HttpPlugin(action, 'test')

return httpPlugin.execute().then((result: any) => {
return httpPlugin.execute(msg).then((result: any) => {
return expect(result.response.myTopology).toEqual('test')
})
})
Expand Down
7 changes: 4 additions & 3 deletions src/worker/execution-plugins/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ export default class HttpPlugin implements ExecutionPluginInterface {
action: Action
preLog: string

constructor(msg: any, action: any, preLog: string) {
this.msg = msg
constructor(action: any, preLog: string) {
this.action = action
this.options = new HTTPPluginOptionsSchema(action.options)
if (this.options.isErrors()) {
Expand All @@ -32,7 +31,9 @@ export default class HttpPlugin implements ExecutionPluginInterface {
return this.msg
}

execute() {
execute(message: any) {
this.msg = message

const method = this.options.method.toLowerCase()

return axios({
Expand Down
7 changes: 4 additions & 3 deletions src/worker/execution-plugins/log.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ describe('execution-plugins :: log', () => {
options: {},
event: 'event-name',
})
const message = {test: 'value', test2: 'value2'}

const logPlugin = new LogPlugin({test: 'value', test2: 'value2'}, action, 'preLog')
const logPlugin = new LogPlugin(action, 'preLog')

it('should return a promise', () => {
return expect(logPlugin.execute()).toBeInstanceOf(Promise)
return expect(logPlugin.execute(message)).toBeInstanceOf(Promise)
})

it('should log messages', () => {
return logPlugin.execute().then((msg: any) => expect(typeof msg).toEqual('object'))
return logPlugin.execute(message).then((msg: any) => expect(typeof msg).toEqual('object'))
})
})
10 changes: 4 additions & 6 deletions src/worker/execution-plugins/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import logger from '../../services/logger'
import { ExecutionPluginInterface } from '../ExecutionPluginInterface'

export default class LogPlugin implements ExecutionPluginInterface {
msg: any
action: Action
preLog: string
options: any

constructor(msg: any, action: Action, preLog: string) {
this.msg = msg
constructor(action: Action, preLog: string) {
this.action = action
this.preLog = preLog + ' > ' + action.name + ': %j'
}

execute() {
logger.info(this.preLog, this.msg)
execute(message: any) {
logger.info(this.preLog, message)

return Promise.resolve(this.msg)
return Promise.resolve(message)
}
}
Loading

0 comments on commit d983eb6

Please sign in to comment.