Skip to content

Commit b043303

Browse files
authored
Merge pull request #1624 from pelias/type-mapping-discovery-fatal-error
Add option to quit on type mapping discovery errors
2 parents 10d56c5 + ade4de6 commit b043303

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The API recognizes the following properties under the top-level `api` key in you
6464
|`services`|*no*||Service definitions for [point-in-polygon](https://github.com/pelias/pip-service), [libpostal](https://github.com/whosonfirst/go-whosonfirst-libpostal), [placeholder](https://github.com/pelias/placeholder), and [interpolation](https://github.com/pelias/interpolation) services. For a description of when different Pelias services are recommended or required, see our [services documentation](https://github.com/pelias/documentation/blob/master/services.md).|
6565
|`defaultParameters.focus.point.lon` <br> `defaultParameters.focus.point.lat`|no | |default coordinates for focus point
6666
|`targets.auto_discover`|no|true|Should `sources` and `layers` be automatically discovered by querying Elasticsearch at process startup. (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below).
67+
|`targets.auto_discover_required`|no|false|If set to `true`, type mapping discovery failures will result in a fatal error. This means a valid connection to a working Elasticsearch cluster with a Pelias index is _required_ on startup. Setting this to true can help prevent issues such as incorrect configuration in production environments
6768
|`targets.layers_by_source` <br> `targets.source_aliases` <br> `targets.layer_aliases`|no | |custom values for which `sources` and `layers` the API accepts (See more info in the [Custom sources and layers](#custom-sources-and-layers) section below). We recommend using the `targets.auto_discover:true` configuration instead of setting these manually.
6869
|`customBoosts` | no | `{}` | Allows configuring boosts for specific sources and layers, in order to influence result order. See [Configurable Boosts](#custom-boosts) below for details |
6970
|`autocomplete.exclude_address_length` | no | 0 | As a performance optimization, this optional filter excludes results from the 'address' layer for any queries where the character length of the 'subject' portion of the parsed_text is equal to or less than this many characters in length. Addresses are usually the bulk of the records in Elasticsearch, and searching across all of them for very short text inputs can be slow, with little benefit. Consider setting this to 1 or 2 if you have several million addresses in Pelias. |

helper/TypeMapping.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,14 @@ TypeMapping.prototype.loadTargets = function( targetsBlock ){
126126

127127
// load values from either pelias config file or from elasticsearch
128128
TypeMapping.prototype.load = function( done ){
129+
// load targets from config file
130+
this.loadFromConfig();
129131

130-
// load pelias config
131132
const peliasConfigTargets = _.get(
132133
require('pelias-config').generate(require('../schema')),
133134
'api.targets', {}
134135
);
135136

136-
// load targets from config file
137-
this.loadTargets( peliasConfigTargets );
138-
139137
// do not load values from elasticsearch
140138
if( true !== peliasConfigTargets.auto_discover ){
141139
if( 'function' === typeof done ){ done(); }
@@ -146,6 +144,19 @@ TypeMapping.prototype.load = function( done ){
146144
loadFromElasticsearch(this, done);
147145
};
148146

147+
// load values from either pelias config file or from elasticsearch
148+
TypeMapping.prototype.loadFromConfig = function( done ){
149+
150+
// load pelias config
151+
const peliasConfigTargets = _.get(
152+
require('pelias-config').generate(require('../schema')),
153+
'api.targets', {}
154+
);
155+
156+
// load targets from config file
157+
this.loadTargets( peliasConfigTargets );
158+
};
159+
149160
// replace the contents of an object or array
150161
// while maintaining the original pointer reference
151162
function safeReplace(reference, replacement){

helper/type_mapping.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const TypeMapping = require('./TypeMapping');
22

3-
// instantiate a new type mapping
3+
// instantiate a singleton type mapping
4+
// loading normal defaults from pelias-config happens now
5+
// updating that config from Elasticsearch happens later
6+
// before the webserver is started
47
var tm = new TypeMapping();
5-
tm.load();
8+
tm.loadFromConfig();
69

710
// export singleton
811
module.exports = tm;

helper/type_mapping_discovery.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const logger = require('pelias-logger').get('api:type_mapping_discovery');
66
/**
77
* This module allows discovery of the sources and layers used
88
* in an existing elasticsearch index.
9-
*
9+
*
1010
* note: this will override any previously configured type mappings.
1111
*/
1212

@@ -43,15 +43,21 @@ module.exports = (tm, done) => {
4343
if( _.has(res, 'hits.total') ) {
4444
totalHits = _.isPlainObject(res.hits.total) ? res.hits.total.value : res.hits.total;
4545
}
46-
46+
4747
// query error
48-
if( err ){ logger.error( err ); }
49-
48+
if( err ){
49+
logger.error( err );
50+
51+
if (peliasConfig.get('api.targets.auto_discover_required') === true) {
52+
process.exit(1);
53+
}
54+
}
55+
5056
// invalid response
5157
else if ( totalHits < 1 ){
5258
logger.error( 'no hits for aggregation' );
5359
}
54-
60+
5561
// valid response
5662
else {
5763

@@ -78,4 +84,4 @@ module.exports = (tm, done) => {
7884

7985
if ('function' === typeof done) { done(); }
8086
});
81-
};
87+
};

index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
const logger = require('pelias-logger').get('api');
2+
const type_mapping = require('./helper/type_mapping');
3+
24
const app = require('./app'),
35
port = ( process.env.PORT || 3100 ),
46
host = ( process.env.HOST || undefined );
57

6-
const server = app.listen( port, host, () => {
7-
// ask server for the actual address and port its listening on
8-
const listenAddress = server.address();
9-
logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` );
8+
let server;
9+
10+
// load Elasticsearch type mappings before starting web server
11+
type_mapping.load(() => {
12+
server = app.listen( port, host, () => {
13+
// ask server for the actual address and port its listening on
14+
const listenAddress = server.address();
15+
logger.info( `pelias is now running on http://${listenAddress.address}:${listenAddress.port}` );
16+
});
1017
});
1118

1219
function exitHandler() {

0 commit comments

Comments
 (0)