Created
February 5, 2015 13:37
-
-
Save runeh/9c57dd997a46f0953c8a to your computer and use it in GitHub Desktop.
Unleash events poller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
}); |
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
Kult! Kjente ikke til "highland" fra før.