@autofleet/node-common 1.1.73 → 1.1.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/node-common",
3
- "version": "1.1.73",
3
+ "version": "1.1.75",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "coverage": "jest --coverage --forceExit --runInBand",
package/settings/index.js CHANGED
@@ -8,19 +8,31 @@ const logger = Logger();
8
8
  require('dotenv').config();
9
9
 
10
10
  const fiveMinutes = 60 * 5;
11
- const watingToNetwork = 'watingToNetwork';
11
+ const waitingToNetwork = 'waitingToNetwork';
12
12
 
13
13
  const findUrl = serviceUrl => serviceUrl ||
14
14
  (process.env.SETTING_MS_SERVICE_HOST && process.env.SETTING_MS_SERVICE_HOST.length > 0 ? `http://${process.env.SETTING_MS_SERVICE_HOST}/` : undefined) ||
15
15
  (process.env.NODE_ENV !== 'test' ? 'http://setting-ms.autofleet.io/' : 'http://localhost:9999/');
16
16
 
17
- module.exports = class SettingsManager {
17
+ class SettingsManager {
18
+ static get NEVER_DEFAULT_VALUE() {
19
+ return 'NEVER_DEFAULT_VALUE';
20
+ }
21
+
18
22
  static readNetworkValue(networkValue, defaultValue) {
19
- return typeof networkValue !== 'undefined' ? networkValue : defaultValue;
23
+ const returnValue = typeof networkValue !== 'undefined' ? networkValue : defaultValue;
24
+
25
+ if (returnValue === SettingsManager.NEVER_DEFAULT_VALUE) {
26
+ throw new Error('Cannot find value from network or cache, default value is set to never');
27
+ }
28
+
29
+ return returnValue;
20
30
  }
31
+
21
32
  constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
22
33
  const localServiceUrl = findUrl(serviceUrl);
23
34
 
35
+ this.NEVER_DEFAULT_VALUE = SettingsManager.NEVER_DEFAULT_VALUE;
24
36
  this.ttl = ttl;
25
37
  this.settingsCache = new NodeCache();
26
38
  this.networkEvents = new EventEmitter();
@@ -29,7 +41,7 @@ module.exports = class SettingsManager {
29
41
  });
30
42
  }
31
43
 
32
- async get(key, defaultValue, labels = []) {
44
+ async get(key, defaultValue, labels = [], { timeout = 1000 } = {}) {
33
45
  if (typeof defaultValue === 'undefined') {
34
46
  throw new Error('Can\'t get a key without defaultValue');
35
47
  }
@@ -41,7 +53,7 @@ module.exports = class SettingsManager {
41
53
  const cacheKey = `${key}_${JSON.stringify(labels)}`;
42
54
  const cacheValue = this.settingsCache.get(cacheKey);
43
55
  if (cacheValue !== undefined) {
44
- if (watingToNetwork === cacheValue) {
56
+ if (waitingToNetwork === cacheValue) {
45
57
  return new Promise((resolve) => {
46
58
  this.networkEvents.once(cacheKey, (networkValue) => {
47
59
  resolve(SettingsManager.readNetworkValue(networkValue, defaultValue));
@@ -57,12 +69,12 @@ module.exports = class SettingsManager {
57
69
 
58
70
  let networkValue;
59
71
  try {
60
- this.settingsCache.set(cacheKey, watingToNetwork, this.ttl);
72
+ this.settingsCache.set(cacheKey, waitingToNetwork, this.ttl);
61
73
  const networkReponse = await this.network.get(`/api/v1/settings/get-setting/${key}`, {
62
74
  params: {
63
75
  labels,
64
76
  },
65
- timeout: 1000,
77
+ timeout,
66
78
  });
67
79
  networkValue = networkReponse.data.value;
68
80
  } catch (error) {
@@ -83,4 +95,6 @@ module.exports = class SettingsManager {
83
95
  flush() {
84
96
  return this.settingsCache.flushAll();
85
97
  }
86
- };
98
+ }
99
+
100
+ module.exports = SettingsManager;
@@ -36,7 +36,7 @@ describe('Settings', () => {
36
36
  expect(await settings.get('key1', false)).toBe(false);
37
37
  });
38
38
 
39
- it('Uses env.SETTING_MS_SERVICE_HOST as serivce host if no one defind', async () => {
39
+ it('Uses env.SETTING_MS_SERVICE_HOST as service host if no one defined', async () => {
40
40
  const m = mockSetting('key1', 'value1');
41
41
  process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
42
42
  const settings = new Settings();
@@ -109,11 +109,22 @@ describe('Settings', () => {
109
109
  expect(value).toEqual('dv');
110
110
  });
111
111
 
112
+ it('Throws an error if network error and no default value', async () => {
113
+ const settings = new Settings({
114
+ serviceUrl: `http://${serviceUrl}/`,
115
+ });
116
+
117
+ const f = () => settings.get('key1', settings.NEVER_DEFAULT_VALUE, [{ fleetId: 'uuid' }]);
118
+ expect(f())
119
+ .rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
120
+ });
121
+
112
122
  it('Values can be flushed', async () => {
113
123
  const m1 = mockSetting('key1', 'value1');
114
124
  const settings = new Settings({
115
125
  serviceUrl: `http://${serviceUrl}/`,
116
126
  });
127
+ settings.flush();
117
128
 
118
129
  const value = await settings.get('key1', 'dv');
119
130
  expect(value).toEqual('value1');
package/settings/map.js CHANGED
@@ -62,6 +62,13 @@ module.exports = {
62
62
  defaultValue: 'max',
63
63
  context: 'indefleet',
64
64
  },
65
+ FLEETING_ENABLE: {
66
+ name: 'Enable fleeting (in-de-fleet)',
67
+ description: 'If true it will enable fleeting every 5 min',
68
+ type: 'boolean',
69
+ defaultValue: 'true',
70
+ context: 'indefleet',
71
+ },
65
72
  // FLEETING_DEMAND_CALCULATION_TIME_MARGIN_MINUTES ??
66
73
  // FLEETING_DEMAND_CALCULATION_INTERVAL, FLEETING_DEMAND_CALCULATION_INTERVALS ??
67
74
  ALLOW_STOP_POINT_OUTSIDE_TERRITORY: {