Skip to content

Commit

Permalink
Merge pull request #577 from Heigvd/mikk-yjsMongoFix
Browse files Browse the repository at this point in the history
YJS - MongoDB Ping fix
  • Loading branch information
SandraMonnier authored May 1, 2024
2 parents a52dafc + 02b6d3d commit 3069073
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
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
9 changes: 9 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 @@ -306,6 +306,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 3069073

Please sign in to comment.