Skip to content

Commit

Permalink
Merge pull request #26 from fastify/optional-ping
Browse files Browse the repository at this point in the history
Make the healthcheck optional
  • Loading branch information
delvedor authored Nov 15, 2019
2 parents c5851ba + 44d5173 commit ea4382b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ fastify.listen(3000, err => {
})
```

By default, `fastify-elasticsearch` will try to ping the cluster as soon as you start Fastify, but in some cases pinging may not be supported due to the user permissions. If you want, you can disable the initial ping with the `healthcheck` option:
```js
fastify.register(require('fastify-elasticsearch'), {
node: 'http://localhost:9200',
healthcheck: false
})
```

If you need to connect to different clusters, you can also pass a `namespace` option:
```js
const fastify = require('fastify')()
Expand Down
2 changes: 1 addition & 1 deletion es-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ exec docker run \
--rm \
-e "discovery.type=single-node" \
-p 9200:9200 \
docker.elastic.co/elasticsearch/elasticsearch:7.1.0
docker.elastic.co/elasticsearch/elasticsearch:7.4.0
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ const fp = require('fastify-plugin')
const { Client } = require('@elastic/elasticsearch')

async function fastifyElasticsearch (fastify, options) {
const { namespace } = options
const { namespace, healthcheck } = options
delete options.namespace
delete options.healthcheck

const client = options.client || new Client(options)

await client.ping()
if (healthcheck !== false) {
await client.ping()
}

if (namespace) {
if (!fastify.elastic) {
Expand Down
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ test('with unreachable cluster', async t => {
}
})

test('with unreachable cluster and healthcheck disabled', async t => {
const fastify = Fastify()
fastify.register(fastifyElasticsearch, {
node: 'http://localhost:9201',
healthcheck: false
})

try {
await fastify.ready()
t.strictEqual(fastify.elastic.name, 'elasticsearch-js')
} catch (err) {
t.fail('should not error')
}
await fastify.close()
})

test('namespaced', async t => {
const fastify = Fastify()
fastify.register(fastifyElasticsearch, {
Expand Down

0 comments on commit ea4382b

Please sign in to comment.