Skip to content

Commit

Permalink
feat: Health check methods (#308) Thanks to @willhausman!
Browse files Browse the repository at this point in the history
* started adding testConnections

* functions for testing and releasing all current active drivers

* call core functions from server

* redundant async

* null checks

* resolved missing mock
  • Loading branch information
willhausman authored and paveltiunov committed Dec 24, 2019
1 parent da10e39 commit 49ca36b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/cubejs-server-core/core/OrchestratorApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class OrchestratorApi {
constructor(driverFactory, logger, options) {
options = options || {};
this.orchestrator = new QueryOrchestrator(options.redisPrefix || 'STANDALONE', driverFactory, logger, options);
this.driverFactory = driverFactory;
const { externalDriverFactory } = options;
this.externalDriverFactory = externalDriverFactory;
this.logger = logger;
}

Expand Down Expand Up @@ -57,6 +60,36 @@ class OrchestratorApi {
throw { error: err.toString() };
}
}

testConnection() {
return Promise.all([
this.testDriverConnection(this.driverFactory),
this.testDriverConnection(this.externalDriverFactory)
]);
}

async testDriverConnection(driverFn) {
if (driverFn) {
const driver = await driverFn();
await driver.testConnection();
}
}

release() {
return Promise.all([
this.releaseDriver(this.driverFactory),
this.releaseDriver(this.externalDriverFactory)
]);
}

async releaseDriver(driverFn) {
if (driverFn) {
const driver = await driverFn();
if (driver.release) {
await driver.release();
}
}
}
}

module.exports = OrchestratorApi;
19 changes: 19 additions & 0 deletions packages/cubejs-server-core/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,25 @@ class CubejsServerCore {
}
return DriverDependencies[dbType];
}

testConnections() {
const tests = [];
Object.keys(this.dataSourceIdToOrchestratorApi).forEach(dataSourceId => {
const orchestratorApi = this.dataSourceIdToOrchestratorApi[dataSourceId];
tests.push(orchestratorApi.testConnection());
});
return Promise.all(tests);
}

async releaseConnections() {
const releases = [];
Object.keys(this.dataSourceIdToOrchestratorApi).forEach(dataSourceId => {
const orchestratorApi = this.dataSourceIdToOrchestratorApi[dataSourceId];
releases.push(orchestratorApi.release());
});
await Promise.all(releases);
this.dataSourceIdToOrchestratorApi = {};
}
}

module.exports = CubejsServerCore;
5 changes: 5 additions & 0 deletions packages/cubejs-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ class CubejsServer {
}
}

testConnections() {
return this.core.testConnections();
}

async close() {
try {
if (this.socketServer) {
Expand All @@ -97,6 +101,7 @@ class CubejsServer {
await this.redirector.close();
this.redirector = null;
}
await this.core.releaseConnections();
} catch (e) {
if (this.core.event) {
await this.core.event("Dev Server Fatal Error", {
Expand Down
6 changes: 6 additions & 0 deletions packages/cubejs-server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ jest.mock("@cubejs-backend/server-core", () => {
const staticCreate = jest.fn();
const initApp = jest.fn(() => Promise.resolve())
const event = jest.fn(() => Promise.resolve())
const releaseConnections = jest.fn(() => Promise.resolve())
class CubejsServerCore {
static create() {
staticCreate.call(null, arguments);
Expand All @@ -14,11 +15,16 @@ jest.mock("@cubejs-backend/server-core", () => {
event() {
return event();
}

releaseConnections() {
return releaseConnections();
}
}
CubejsServerCore.mock = {
staticCreate,
initApp,
event,
releaseConnections,
};
return CubejsServerCore;
});
Expand Down

0 comments on commit 49ca36b

Please sign in to comment.