Skip to content

Instantly share code, notes, and snippets.

@yunghoy
Last active September 5, 2016 12:47
Show Gist options
  • Save yunghoy/7f3ef46b29b71ae81ac599ca2895fb10 to your computer and use it in GitHub Desktop.
Save yunghoy/7f3ef46b29b71ae81ac599ca2895fb10 to your computer and use it in GitHub Desktop.
Consul KV TTL
// Code for checking the behavior of Consul
var Promise = require('bluebird');
var Consul = require('consul');
var consul = Consul({host: "localhost", promisify: true});
// Session Create
consul.session.create({
name: "test-java-script",
ttl: "600s",
behavior: "delete"
}, setKey);
// Set Key with TTL
function setKey(err, result) {
console.log(result);
consul.kv.set({
key: "test-java-script-key",
value: "aaaaaaa",
acquire: result.ID
}, getKey);
}
// Get Key
function getKey(err, result) {
console.log(result);
consul.kv.get("test-java-script-key", renew);
}
// Renew TTL if the key has Session (TTL)
function renew(err, result) {
console.log(result);
if(result && result.Session) {
consul.session.renew(result.Session, log);
}
}
function log(err, result) {
// consul.session.destroy(result.ID, log); // Destroy Test.
console.log(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment