Skip to content

Commit 203d2b5

Browse files
committed
system store - load from core - two steps publish - add load from core test
Signed-off-by: Amit Prinz Setter <[email protected]>
1 parent 807b37a commit 203d2b5

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/server/system_services/system_store.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ class SystemStore extends EventEmitter {
341341
*/
342342
static get_instance(options = {}) {
343343
const { standalone } = options;
344+
//load from core if enabled and this is an endpoint
345+
const is_endpoint = process.env.HOSTNAME && process.env.HOSTNAME.indexOf("endpoint") !== -1;
346+
this.source = options.source ||
347+
((config.SYSTEM_STORE_SOURCE.toUpperCase() === 'CORE' && is_endpoint) ? SOURCE.CORE : SOURCE.DB);
344348
SystemStore._instance = SystemStore._instance || new SystemStore({ standalone });
345349
return SystemStore._instance;
346350
}
@@ -357,13 +361,19 @@ class SystemStore extends EventEmitter {
357361
this.START_REFRESH_THRESHOLD = 10 * 60 * 1000;
358362
this.FORCE_REFRESH_THRESHOLD = 60 * 60 * 1000;
359363
this.SYSTEM_STORE_LOAD_CONCURRENCY = config.SYSTEM_STORE_LOAD_CONCURRENCY || 5;
360-
//load from core is enabled and this is an endpoint
361-
const is_endpoint = process.env.HOSTNAME && process.env.HOSTNAME.indexOf("endpoint") !== -1;
362-
this.source = (config.SYSTEM_STORE_SOURCE.toUpperCase() === 'CORE' && is_endpoint) ? SOURCE.CORE : SOURCE.DB;
364+
this.source = options.source || SOURCE.DB;
363365
dbg.log0("system store source is", this.source);
364366
this._load_serial = new semaphore.Semaphore(1, { warning_timeout: this.START_REFRESH_THRESHOLD });
365367
for (const col of COLLECTIONS) {
366-
db_client.instance().define_collection(col);
368+
try {
369+
db_client.instance().define_collection(col);
370+
} catch (e) {
371+
if (e.message?.indexOf("already defined") > -1) {
372+
dbg.warn("Ignoring already defined error");
373+
} else {
374+
throw e;
375+
}
376+
}
367377
}
368378
js_utils.deep_freeze(COLLECTIONS);
369379
js_utils.deep_freeze(COLLECTIONS_BY_NAME);
@@ -430,7 +440,7 @@ class SystemStore extends EventEmitter {
430440
// because it might not see the latest changes if we don't reload right after make_changes.
431441
return this._load_serial.surround(async () => {
432442
try {
433-
dbg.log3('SystemStore: loading ... this.last_update_time =', this.last_update_time, ", since =", since);
443+
dbg.log3('SystemStore: loading ... this.last_update_time =', this.last_update_time, ", since =", since, "load_from_core_step =", load_from_core_step);
434444

435445
// If we get a load request with an timestamp older then our last update time
436446
// we ensure we load everyting from that timestamp by updating our last_update_time.

src/test/integration_tests/db/test_system_store.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const mocha = require('mocha');
55
const assert = require('assert');
66
const coretest = require('../../utils/coretest/coretest');
77
const db_client = require('../../../util/db_client');
8+
const { SystemStore } = require('../../../server/system_services/system_store');
89

910
// setup coretest first to prepare the env
1011
coretest.setup();
@@ -138,4 +139,17 @@ mocha.describe('system_store', function() {
138139
assert.strictEqual(data2.systems[0].name, 'new_name');
139140
});
140141

142+
mocha.it("Load from core", async function() {
143+
144+
const system_store_from_core = new SystemStore({
145+
source: 'CORE'
146+
});
147+
148+
const from_db = await system_store.load();
149+
const from_core = await system_store_from_core.load(undefined, 'ENDPOINT');
150+
151+
assert.deepStrictEqual(from_db, from_core);
152+
153+
});
154+
141155
});

0 commit comments

Comments
 (0)