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

Complementing support to /v2/events #12

Merged
merged 5 commits into from
Jan 13, 2017
Merged

Conversation

castanhob
Copy link
Contributor

First of all, thanks for this amazing lib and work!

This pull request adds a required header for the /v2/events endpoint, which needs the Accept: text/event-stream to connect (following the Marathon API Documentation).

Not only that, but since request-promise currently has problems resolving promises with streams (check request/request-promise#90)
and even their documentation states that we must use the request lib to resolve those ones, I made a small change to be able to resolve this promise and return the stream to be managed by the user following his needs.

Copy link
Contributor

@pnedelko pnedelko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Could you please also add some documentation/examples, because I find this feature really interesting.

@@ -62,6 +63,21 @@ function Marathon(url, opts) {
}
}

if (requestStream) {
return new Promise((resolve, reject) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite to normal function, since this lib is not using latest JavaScript standard. We will introduce Babel or whatever in future.

@castanhob
Copy link
Contributor Author

castanhob commented Jan 10, 2017

Sure @pnedelko !

When we attach to the Marathon Event Bus (from the Nodejs perspective) we receive a Readable Stream object.
This object can be used to see what Marathon is doing through the events. It has a big potential for DevOps to control the flux of their deployments, for example.

Here is an example on the user side on how to implement it using the marathon-node lib:

const marathon = require('marathon-node')(MARATHON_URL, opts);

marathon.events.attach()
	.then(function(stream) {
		// Forces the stream to receive a String instead of a Buffer object
		stream.setEncoding('utf-8');

		// Event that receives data from Marathon
		stream.on('data', function(chunk) {
			// Printing the event received from the stream
			console.log(chunk);
		});

		// Last event, it runs when the connection is closed
		stream.on('end', function() {
			// Here you do what you need when it ends...
		});

		// If for some reason we receive an error while connected, we can handle it here
		stream.on('errror', function(err) {
			// Error handling...
		})
	}).catch(function(err) {
		// Any problem while trying to connect to /v2/events
	});

Any further explanation about this feature or documentation that I missed, please let me know!

@pnedelko
Copy link
Contributor

@castanhob could you please add this code example to readme file?

@castanhob
Copy link
Contributor Author

castanhob commented Jan 12, 2017

Done. Could you please check if it fits the README properly?

@pnedelko
Copy link
Contributor

Well done! Thank you!

@pnedelko pnedelko merged commit 1002f4c into elasticio:master Jan 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants