Skip to content

Commit

Permalink
pass err instead exit the client, add onClose Hook
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed Mar 30, 2018
1 parent 0b82db3 commit 738c52c
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,24 @@ function hemeraElasticSearch(hemera, opts, done) {
opts.elasticsearchInstance || new Elasticsearch.Client(opts.elasticsearch)
hemera.decorate('elasticsearch', client)

/**
* Check if cluster is available otherwise exit this client.
*/
client
.ping({
client.ping(
{
requestTimeout: opts.elasticsearch.timeout
})
.then(() => hemera.log.debug('elasticsearch cluster is available'))
.catch(err => {
hemera.log.error(err, 'elasticsearch cluster is down!')
hemera.fatal()
})
},
function(err) {
if (err) {
done(err)
} else {
hemera.log.debug('elasticsearch cluster is available')
done()
}
}
)

hemera.ext('onClose', function closeESClient() {
hemera.log.debug('elasticsearch client is closing ...')
return client.close()
})

hemera.add(
{
Expand Down Expand Up @@ -132,8 +138,6 @@ function hemeraElasticSearch(hemera, opts, done) {
},
req => client.refresh(req.data)
)

done()
}

module.exports = Hp(hemeraElasticSearch, {
Expand Down

0 comments on commit 738c52c

Please sign in to comment.