Skip to content

Instantly share code, notes, and snippets.

@runeh
Created February 5, 2015 13:37
Show Gist options
  • Save runeh/9c57dd997a46f0953c8a to your computer and use it in GitHub Desktop.
Save runeh/9c57dd997a46f0953c8a to your computer and use it in GitHub Desktop.
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
Copy link

ivarconr commented Feb 5, 2015

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

@ivarconr
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