-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better support for host argument, and handling of 'no es' scenarios
- Loading branch information
Spencer Alger
committed
Jan 14, 2015
1 parent
02526a1
commit 3a08c7e
Showing
6 changed files
with
57 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,49 @@ | ||
var argv = require('./argv'); | ||
var es = require('elasticsearch'); | ||
var _ = require('lodash'); | ||
var Promise = require('bluebird'); | ||
var through2 = require('through2'); | ||
var parse = require('url').parse; | ||
var Client = require('elasticsearch').Client; | ||
var NoConnections = require('elasticsearch').errors.NoConnections; | ||
var RequestTimeout = require('elasticsearch').errors.RequestTimeout; | ||
|
||
var clientConfig = {}; | ||
var host = String(argv.host); | ||
var proto = _.contains(host, '//') ? '' : '//'; | ||
var parsed = parse(proto + host, false, true); | ||
|
||
if (argv.trace) { | ||
clientConfig.log = 'trace'; | ||
} | ||
|
||
clientConfig.hosts = [ | ||
{ | ||
host: argv.host.split(":")[0], | ||
port: argv.host.split(":")[1] ? argv.host.split(":")[1] : "9200", | ||
var ms = 1000; | ||
var client = module.exports = new Client({ | ||
log: { | ||
type: 'stream', | ||
level: argv.trace ? 'trace' : 'warning', | ||
stream: through2(function (chunk, enc, cb) { | ||
client.usable.then(function () { | ||
process.stdout.write(chunk, enc); | ||
cb(); | ||
}); | ||
}) | ||
}, | ||
host: { | ||
host: parsed.hostname, | ||
port: parsed.port, | ||
auth: argv.auth | ||
} | ||
]; | ||
}); | ||
|
||
client.usable = client.ping({ | ||
requestTimeout: ms | ||
}) | ||
.catch(function (err) { | ||
var notAlive = err instanceof NoConnections; | ||
var timeout = err instanceof RequestTimeout; | ||
|
||
if (notAlive || timeout) { | ||
console.error('Unable to connect to elasticsearch at %s within %d seconds', host, ms / 1000); | ||
} else { | ||
console.log('unknown ping error', err); | ||
} | ||
|
||
module.exports = new es.Client(clientConfig); | ||
client.close(); | ||
// prevent the promise from ever resolving or rejecting | ||
return new Promise(_.noop); | ||
}); |
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
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