Created
November 24, 2014 18:58
-
-
Save geowarin/5a00f8fb29e5cb1140df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@Grapes( | |
@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.7.1') | |
) | |
import groovyx.net.http.RESTClient | |
def etcd = new Etcd() | |
def onSet = { node, previous -> | |
println "set $node" | |
} | |
def onRm = { node, previous -> | |
println "set $node" | |
} | |
etcd.watchForever('/message', onSet, onRm) | |
class Etcd { | |
final def ETCD_URL = 'http://192.168.59.103:4001' | |
def etcd = new RESTClient(ETCD_URL) | |
void post(String dest) { | |
etcd.put( | |
path: '/v2/keys/message', | |
params: [value: 'lol'] | |
) | |
} | |
def watchForever(String key, Closure onSet, Closure onRm) { | |
while (true) { | |
watch(key, onSet, onRm) | |
} | |
} | |
def watch(String key, Closure onSet, Closure onRm) { | |
def result = watch(key) | |
switch (result.action) { | |
case 'set': onSet.call(result.node, result.prevNode); break | |
case 'rm': onRm.call(result.node, result.prevNode); break | |
} | |
} | |
def watch(String key) { | |
etcd.get( | |
path: "/v2/keys$key", | |
params: [wait: 'true'] | |
).data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment