-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from stianeikeland/watcher
Added eventemitter for watching changes
- Loading branch information
Showing
5 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{EventEmitter} = require 'events' | ||
|
||
# A eventemitter for watching changes on a given key for etcd. | ||
# Emits: | ||
# 'change' - on value change | ||
# 'reconnect' - on errors/timeouts | ||
# | ||
# Automatically reconnects and backs off on errors. | ||
# | ||
class Watcher extends EventEmitter | ||
|
||
constructor: (@etcd, @key, @index = null) -> | ||
@retryAttempts = 0 | ||
@_watch() | ||
|
||
_watch: () => | ||
if @index is null | ||
@etcd.watch @key, @_respHandler | ||
else | ||
@etcd.watchIndex @key, @index, @_respHandler | ||
|
||
_respHandler: (err, val) => | ||
if val?.index? | ||
@retryAttempts = 0 | ||
@index = val.index + 1 | ||
@emit 'change', val | ||
@_watch() | ||
|
||
if err isnt null | ||
@emit 'reconnect', { error: err, reconnectcount: @retryAttempts } | ||
@_retry() | ||
|
||
_retry: () => | ||
setTimeout @_watch, 500 * @retryAttempts | ||
@retryAttempts++ | ||
|
||
exports = module.exports = Watcher |
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require 'should' | ||
Watcher = require '../src/watcher.coffee' | ||
|
||
class FakeEtcd | ||
constructor: () -> | ||
@cb = () -> | ||
|
||
watch: (key, cb) -> | ||
key.should.equal 'key' | ||
@cb = cb | ||
|
||
watchIndex: (key, index, cb) -> | ||
key.should.equal 'key' | ||
@cb = cb | ||
|
||
change: (err, val) -> | ||
@cb err, val | ||
|
||
|
||
describe 'Watcher', () -> | ||
it 'should emit change on watch change', (done) -> | ||
etcd = new FakeEtcd | ||
w = new Watcher etcd, 'key' | ||
|
||
w.on 'change', (val) -> | ||
val.should.include { index: 0 } | ||
done() | ||
|
||
etcd.change null, { index: 0 } | ||
|
||
it 'should emit reconnect event on error', (done) -> | ||
etcd = new FakeEtcd | ||
w = new Watcher etcd, 'key' | ||
|
||
w.on 'reconnect', (err) -> | ||
err.should.include { error: "error" } | ||
done() | ||
|
||
etcd.change "error", null | ||
|
||
it 'should reconnect (call watch again) on error', (done) -> | ||
etcd = new FakeEtcd | ||
w = new Watcher etcd, 'key' | ||
|
||
etcd.watch = (key, cb) -> | ||
w.retryAttempts.should.equal 1 | ||
done() | ||
|
||
etcd.change "error", null | ||
|
||
it 'should call watch on next index after getting change', (done) -> | ||
etcd = new FakeEtcd | ||
w = new Watcher etcd, 'key' | ||
|
||
i = 5 | ||
|
||
etcd.watchIndex = (key, index, cb) -> | ||
index.should.equal i + 1 | ||
done() | ||
|
||
etcd.change null, { index: i } | ||
|
||
|