Skip to content

Commit

Permalink
Add ping method to MongoDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
TehAwol committed May 1, 2024
1 parent a52dafc commit 93e7c98
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- dev
- mikk-yjsMongoFix
tags:
- "v*"
pull_request:
Expand Down
13 changes: 4 additions & 9 deletions colab-api/src/main/node/colab-yjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
getQueryParams,
onSocketError,
} from './utils/utils.js';
import { MongoClient } from 'mongodb';

dotenv.config();

Expand All @@ -34,13 +33,10 @@ const port = process.env.PORT || 4321;
// Payara params
const payaraHost = process.env.AUTHHOST || 'http://127.0.0.1:3004/';
// Mongo params
const mongoHost = process.env.DBHOST || 'mongodb://localhost:27019';
const mongoDBName = 'colablexical';
const mongoHost = process.env.DBHOST || 'mongodb://localhost:27019/colablexical';
const mongoCollection = 'documents';

const mongoClient = new MongoClient(mongoHost);

const mongoDriver = new MongodbPersistence(mongoHost + '/' + mongoDBName, {
const mongoDriver = new MongodbPersistence(mongoHost, {
collectionName: mongoCollection,
flushSize: 100,
multipleCollections: false,
Expand Down Expand Up @@ -91,9 +87,8 @@ app.get('/healthz', async (request: Request, response: Response) => {
});
if (payaraResponse.status >= 400) throw new Error('Payara-Server Error');

await mongoClient.connect();
const db = mongoClient.db(mongoDBName);
await db.command({ ping: 1 });
const mongoResponse = await mongoDriver.ping();
if (mongoResponse.ok !== 1) throw new Error('MongoDB Error');

response.status(200).send('Server is healthy');
} catch (err) {
Expand Down
7 changes: 7 additions & 0 deletions colab-api/src/main/node/colab-yjs/src/mongo/mongo-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ export class MongoAdapter {
return curs.toArray();
}

/**
* Ping connection to MongoDB instance.
*/
async ping() {
return await this.db.command({ ping: 1 });
}

/**
* Close connection to MongoDB instance.
*/
Expand Down
10 changes: 10 additions & 0 deletions colab-api/src/main/node/colab-yjs/src/mongo/y-mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class MongodbPersistence {
collection: collectionName,
multipleCollections,
});
this.mongoAdapter = db;
this.flushSize = flushSize ?? U.PREFERRED_TRIM_SIZE;
this.multipleCollections = multipleCollections;

Expand Down Expand Up @@ -306,6 +307,15 @@ export class MongodbPersistence {
});
}

/**
* Ping the current database connection
*/
ping() {
return this._transact('global', async db => {
return await db.ping();
});
}

/**
* Closes open database connection
* @returns {Promise<void>}
Expand Down

0 comments on commit 93e7c98

Please sign in to comment.