Permalink
Browse files

Merge pull request #12 from racker/improve_events_poller

If an error occurs while polling the events feed, log it and propagate i...
  • Loading branch information...
2 parents 844c7cc + 2364fdb commit f59276047ebde7ba7ccec21cbe16f9d027e754bb @Kami Kami committed Mar 27, 2013
Showing with 61 additions and 2 deletions.
  1. +53 −0 examples/event_poller.js
  2. +7 −1 lib/events_feed_poller.js
  3. +1 −1 scripts/lint.sh
@@ -0,0 +1,53 @@
+/**
+ * Copyright 2012 Rackspace
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+/**
+ * En example which uses EventsFeedPoller abstraction to poll the events feed
+ * and print all of the events to the standard output.
+ */
+var Client = require('../lib').Client;
+var EventsFeedPoller = require('../lib').EventsFeedPoller;
+
+var EVENT_KEYS = [
+ 'service.join',
+ 'service.timeout',
+ 'service.remove',
+ 'configuration_value.update',
+ 'configuration_value.remove'
+];
+
+var client = new Client('myusername', 'myapikey', 'us');
+var eventsPoller = new EventsFeedPoller(client.events, {'pollInterval': 5});
+
+function printEmittedEvent(type) {
+ return function(payload) {
+ console.log('Event type: ' + type);
+ console.log('Event payload: ' + JSON.stringify(payload));
+ };
+}
+
+// Add listener for all the available events
+EVENT_KEYS.forEach(function(key) {
+ eventsPoller.on(key, printEmittedEvent(key));
+});
+
+eventsPoller.on('error', function(err) {
+ console.log('Error occured: ' + err.toString());
+});
+
+// Start polling
+eventsPoller.start();
@@ -67,7 +67,13 @@ EventsFeedPoller.prototype._schedulePolling = function() {
this._timeoutId = setTimeout(function() {
self._run(function(err) {
- // Error is not fatal here so we ignore it.
+ // Error is not fatal so the processing is not stopped and we just
+ // propagate it to the user.
+ if (err) {
+ log.debug('Error while polling events feed', {'err': err});
+ self.emit('error', err);
+ }
+
self._schedulePolling();
});
}, interval);
View
@@ -1,3 +1,3 @@
#!/bin/bash
-./node_modules/.bin/jshint $(find ./lib -type f -name "*.js") --config jshint.json
+./node_modules/.bin/jshint $(find ./lib ./examples -type f -name "*.js") --config jshint.json

0 comments on commit f592760

Please sign in to comment.