Skip to content

Commit

Permalink
Making better logs to understanding the operators working, refs #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Genar Trias Ortiz committed Feb 26, 2018
1 parent 92087a0 commit ef50d97
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/worker/action-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = class ActionCreator {
}
this.rabbit = rabbit
this.event = event
this.preLog = event.name + ':'
this.preLog = event.name + ' >'
}

createHandler() {
Expand Down Expand Up @@ -77,7 +77,7 @@ module.exports = class ActionCreator {

// Iterate over all actions passing the lastResult
this.event.actions.forEach((action, index) => {
const executer = new ActionExecuter(action, rabbit)
const executer = new ActionExecuter(action, rabbit, this.event)

const executionPromise = function (lastValue, preLog, eventsLenght) {
preLog = preLog + ' [' + lastValue.id + ']:'
Expand Down
35 changes: 21 additions & 14 deletions src/worker/action-executer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const ObjectTransformerPlugin = require('./execution-plugins/object-transformer-
const debug = require('debug')('action-executer')

module.exports = class ActionExecuter {
constructor(action, rabbit) {
constructor(action, rabbit, event) {
debug('action executer action received: %j', action)
this.action = action
this.rabbit = rabbit
this.event = event
this.preLog = event.name + ' >'
}

// Instantiate the proper plugin with proper parameters and execute it
Expand All @@ -26,35 +28,40 @@ module.exports = class ActionExecuter {
switch (this.action.type) {
case 'log':
executionPlugin = new LogPlugin(
prevMessage,
this.action
)
prevMessage,
this.action,
this.preLog
)
break
case 'mapper':
case 'obj-transformer':
executionPlugin = new ObjectTransformerPlugin(
prevMessage,
this.action
)
prevMessage,
this.action,
this.preLog
)
break
case 'http':
executionPlugin = new HttpPlugin(
prevMessage,
this.action
)
prevMessage,
this.action,
this.preLog
)
break
case 'conditional':
executionPlugin = new ConditionalPlugin(
prevMessage,
this.action
)
prevMessage,
this.action,
this.preLog
)
break
case 'event2task':
case 'prev2task':
executionPlugin = new Event2TaskPlugin(
prevMessage,
this.action,
this.rabbit
this.rabbit,
this.preLog
)
break
default:
Expand Down
2 changes: 1 addition & 1 deletion src/worker/action-executer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('ActionExecuter', () => {

expect(action.isErrors()).to.be.false

const actionExecuter = new ActionExecuter(action, rabbit)
const actionExecuter = new ActionExecuter(action, rabbit, {name: 'test'})

const msg = {
body: {
Expand Down
4 changes: 2 additions & 2 deletions src/worker/execution-plugins/conditional-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ const PluginOptionsSchema = new SchemaObject({
})

module.exports = class ConditionalPlugin {
constructor(msg, action) {
constructor(msg, action, preLog) {
this.msg = msg
this.action = action
this.preLog = action.name + '[ ' + msg.id + ' ]'
this.preLog = preLog + ' > ' + action.name
this.parsedMessage = flattenObject(
this.msg
)
Expand Down
7 changes: 4 additions & 3 deletions src/worker/execution-plugins/event2task-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const PluginOptionsSchema = new SchemaObject({
})

module.exports = class Event2TaskPlugin {
constructor(msg, action, rabbit) {
constructor(msg, action, rabbit, preLog) {
if (!rabbit) {
throw new Error('You must provide a rabbitmq instance')
}
Expand All @@ -28,6 +28,7 @@ module.exports = class Event2TaskPlugin {
this.msg = msg
this.action = action
this.rabbit = rabbit
this.preLog = preLog + ' > ' + action.name
}

execute(callback) {
Expand All @@ -46,10 +47,10 @@ module.exports = class Event2TaskPlugin {
body: payload,
replyTimeout: 3000
}).then(() => {
logger.info(this.action.name, ': event2task executed')
logger.info(this.preLog, ': event2task executed')
return callback(null, this.msg)
}, (err) => {
logger.info(this.action.name, ': event2task failed')
logger.info(this.preLog, ': event2task failed')
return callback(new Error('Error publishing to the quque', err))
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/worker/execution-plugins/http-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ const PluginOptionsSchema = new SchemaObject({
})

module.exports = class HttpPlugin {
constructor(msg, action) {
constructor(msg, action, preLog) {
this.msg = msg

this.options = new PluginOptionsSchema(action.options)
if (this.options.isErrors()) {
throw new Error('The options provided are not valid '+ JSON.stringify(this.options.getErrors()))
}
this.preLog = preLog + ' > ' + action.name
}

renderUrl() {
Expand Down
5 changes: 3 additions & 2 deletions src/worker/execution-plugins/log-plugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { logger } = require('../../utils/logger')

module.exports = class LogPlugin {
constructor(msg, action) {
constructor(msg, action, preLog) {
this.msg = msg
this.action = action
this.preLog = preLog + ' > ' + action.name
}

execute(callback) {
logger.info(this.action.name, ': ', this.msg)
logger.info(this.preLog, this.msg)
return callback(null, this.msg)
}
}
6 changes: 4 additions & 2 deletions src/worker/execution-plugins/object-transformer-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const PluginOptionsSchema = new SchemaObject({
})

module.exports = class ObjectTransformerPlugin {
constructor(msg, action) {
constructor(msg, action, preLog) {
this.msg = msg
this.action = action
debug('received next msg: %j', this.msg)
Expand All @@ -34,6 +34,8 @@ module.exports = class ObjectTransformerPlugin {
if (this.options.isErrors()) {
throw new Error('The options provided are not valid '+ JSON.stringify(this.options.getErrors()))
}

this.preLog = preLog + ' > ' + action.name
}

execute(callback) {
Expand All @@ -49,7 +51,7 @@ module.exports = class ObjectTransformerPlugin {
}

debug('Result mapped object is %j', transformedObj)
logger.info(this.action.name, ': Object mapping applied')
logger.info(this.preLog, 'Object mapping applied')

return callback(null, transformedObj)
}
Expand Down

0 comments on commit ef50d97

Please sign in to comment.