Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support of object storage on error #97

Open
wants to merge 4 commits into
base: elasticio-1456-add-maester-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/amqp.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ class Amqp {
return this.prepareMessageAndSendToExchange(data, properties, routingKey);
}

async sendError(err, properties, originalMessageContent, throttle) {
async sendError(err, properties, originalMessage, throttle) {
const settings = this.settings;
const headers = properties.headers;
const messageContent = _.get(originalMessage , 'content');
const messageObjectId = _.get(originalMessage, 'properties.headers.objectId');

const encryptedError = encryptor.encryptMessageContent({
name: err.name,
Expand All @@ -215,9 +217,10 @@ class Amqp {
const payload = {
error: encryptedError
};

if (originalMessageContent && originalMessageContent !== '') {
payload.errorInput = originalMessageContent.toString();
if (messageObjectId) {
payload.objectId = messageObjectId;
} else if (messageContent && messageContent !== '') {
payload.errorInput = messageContent.toString();
}
const errorPayload = JSON.stringify(payload);

Expand Down Expand Up @@ -245,7 +248,7 @@ class Amqp {
return this.sendError(
new Error('Rebound limit exceeded'),
properties,
originalMessage.content
originalMessage
);
} else {
properties.expiration = getExpiration(reboundIteration);
Expand Down
8 changes: 5 additions & 3 deletions lib/sailor.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class Sailor {
}, 'processMessage emit error');
headers.end = new Date().getTime();
const props = createAmqpProperties(headers);
return self.amqpConnection.sendError(err, props, message.content, self.throttles.error);
return self.amqpConnection.sendError(err, props, message, self.throttles.error);
}

VitaliyKovalchuk marked this conversation as resolved.
Show resolved Hide resolved
async function onRebound(err) {
Expand Down Expand Up @@ -450,9 +450,11 @@ class Sailor {
}

function onModuleNotFound(err) {
const headers = _.clone(outgoingMessageHeaders);
console.error(err.stack);
outgoingMessageHeaders.end = new Date().getTime();
self.amqpConnection.sendError(err, outgoingMessageHeaders, message.content);
headers.end = new Date().getTime();
const props = createAmqpProperties(headers);
self.amqpConnection.sendError(err, props, message);
self.amqpConnection.reject(message);
}

Expand Down
18 changes: 18 additions & 0 deletions mocha_spec/integration_component/actions/error_action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

exports.process = processAction;

function processAction(msg, cfg) {

//eslint-disable-next-line no-invalid-this
this.emit('error', {
statusCode: 200,
body: 'Ok',
headers: {
'content-type': 'text/plain'
}
});
//eslint-disable-next-line no-invalid-this
this.emit('end');

}
4 changes: 4 additions & 0 deletions mocha_spec/integration_component/component.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
"http_reply_action": {
"main": "./actions/http_reply_action.js",
"title": "Http Reply Action"
},
"error_action": {
"main": "./actions/error_action.js",
"title": "Error Action"
}
}
}
31 changes: 31 additions & 0 deletions mocha_spec/run.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,37 @@ describe('Integration Test', () => {
return new Promise((resolve) => { done = resolve; });
});

it('should send objectId on error instead of original message', async () => {

process.env.ELASTICIO_OBJECT_STORAGE_ENABLED = true;
env.ELASTICIO_FUNCTION = 'error_action';

helpers.mockApiTaskStepResponse();

const objectStorageGet = nock(process.env.ELASTICIO_OBJECT_STORAGE_URI)
.get(`/objects/${objectId}`)
.matchHeader('authorization', /Bearer/)
.reply(200, await helpers.encryptForObjectStorage(inputMessage), {
'content-type': 'application/octet-stream'
});

let done;
amqpHelper.on('data', ({ emittedMessage }) => {
expect(emittedMessage.objectId).to.equal(objectId);
expect(objectStorageGet.isDone()).to.be.true;
done();
});

run = requireRun();

amqpHelper.publishMessage('', {
parentMessageId,
traceId
}, { objectId });

return new Promise((resolve) => { done = resolve; });
});

it('should get object storage message if error repeat succeed', async () => {
process.env.ELASTICIO_OBJECT_STORAGE_ENABLED = true;

Expand Down
50 changes: 48 additions & 2 deletions spec/amqp.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('AMQP', () => {
}
};

amqp.sendError(new Error('Test error'), props, message.content);
amqp.sendError(new Error('Test error'), props, message);

expect(amqp.publishChannel.publish).toHaveBeenCalled();
expect(amqp.publishChannel.publish.callCount).toEqual(1);
Expand Down Expand Up @@ -371,7 +371,7 @@ describe('AMQP', () => {
};

async function test() {
await amqp.sendError(new Error('Test error'), props, message.content);
await amqp.sendError(new Error('Test error'), props, message);
}

test().then(() => {
Expand Down Expand Up @@ -497,6 +497,52 @@ describe('AMQP', () => {
expect(publishParameters[3]).toEqual(props);
});

it('Should send objectId instead of message when process error', () => {
VitaliyKovalchuk marked this conversation as resolved.
Show resolved Hide resolved

const amqp = new Amqp(settings);
const objectId = 'aef703e0-7ebf-456f-a7b9-20b647ab43cd';
amqp.publishChannel = jasmine.createSpyObj('publishChannel', ['publish']);


const props = {
contentType: 'application/json',
contentEncoding: 'utf8',
mandatory: true,
headers: {
taskId: 'task1234567890',
stepId: 'step_456'
}
};

const msgWithObjectId = _.cloneDeep(message);
msgWithObjectId.properties.headers.objectId = objectId;

amqp.sendError(new Error('Test error'), props, msgWithObjectId);

expect(amqp.publishChannel.publish).toHaveBeenCalled();
expect(amqp.publishChannel.publish.callCount).toEqual(1);

const publishParameters = amqp.publishChannel.publish.calls[0].args;
expect(publishParameters).toEqual([
settings.PUBLISH_MESSAGES_TO,
settings.ERROR_ROUTING_KEY,
jasmine.any(Object),
props
]);

const payload = JSON.parse(publishParameters[2].toString());
payload.error = encryptor.decryptMessageContent(payload.error);

expect(payload).toEqual({
error: {
name: 'Error',
message: 'Test error',
stack: jasmine.any(String)
},
objectId
});
});

it('Should send message to rebounds when rebound happened', () => {

const amqp = new Amqp(settings);
Expand Down
4 changes: 2 additions & 2 deletions spec/sailor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ describe('Sailor', () => {
expect(fakeAMQPConnection.sendError).toHaveBeenCalled();
expect(fakeAMQPConnection.sendError.calls[0].args[0].message).toEqual('Some component error');
expect(fakeAMQPConnection.sendError.calls[0].args[0].stack).not.toBeUndefined();
expect(fakeAMQPConnection.sendError.calls[0].args[2]).toEqual(message.content);
expect(fakeAMQPConnection.sendError.calls[0].args[2]).toEqual(message);

expect(fakeAMQPConnection.reject).toHaveBeenCalled();
expect(fakeAMQPConnection.reject.callCount).toEqual(1);
Expand Down Expand Up @@ -654,7 +654,7 @@ describe('Sailor', () => {
/* eslint-enable */
);
expect(fakeAMQPConnection.sendError.calls[0].args[0].stack).not.toBeUndefined();
expect(fakeAMQPConnection.sendError.calls[0].args[2]).toEqual(message.content);
expect(fakeAMQPConnection.sendError.calls[0].args[2]).toEqual(message);

expect(fakeAMQPConnection.reject).toHaveBeenCalled();
expect(fakeAMQPConnection.reject.callCount).toEqual(1);
Expand Down