Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the healthcheck optional #26

Merged
merged 4 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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