Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unleash events poller
const request = require('request-promise');
const highland = require('highland');
const promiseToStream = highland;
const arrayToStream = highland;
const pollerToStream = highland;
function seenRecently(timeGetter, mostRecentTime=0) {
return (e) => {
let then = timeGetter(e).getTime();
mostRecentTime = Math.max(mostRecentTime, then);
return mostRecentTime <= then;
}
}
function unleashPoller(url) {
return (push, next) => {
push(null, request(url))
next();
}
}
function unleashUpdatesStream(url, pollInterval=60000) {
return pollerToStream(unleashPoller(url))
.ratelimit(1, pollInterval)
.flatMap(promiseToStream)
.map(JSON.parse)
.flatMap(e => arrayToStream(e.events.reverse()))
.filter(e => e.type == 'feature-updated')
.filter(seenRecently(e => new Date(e.createdAt)));
// fixme: filter out events that have a timestamp more than n seconds ago
}
const url = "http://unleash.herokuapp.com/events";
const stream = unleashUpdatesStream(url);
stream.each(e => {
console.log(e.data.name, "became", e.data.enabled ? "enabled" : "disabled");
// todo, statsd tracking here.
});
@ivarconr

This comment has been minimized.

Copy link

ivarconr commented Feb 5, 2015

Kult! Kjente ikke til "highland" fra før.

@ivarconr

This comment has been minimized.

Copy link

ivarconr commented Feb 5, 2015

Until we have the websocket-API in place we could use this script to post updates to hipchat also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.