Skip to content

Commit

Permalink
Fix mongodb using callback instead of promises
Browse files Browse the repository at this point in the history
Issue: BB-632
  • Loading branch information
KillianG committed Nov 21, 2024
1 parent 5993669 commit df36e67
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 36 deletions.
17 changes: 8 additions & 9 deletions extensions/notification/NotificationConfigManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,11 @@ class NotificationConfigManager {
MongoClient.connect(mongoUrl, {
replicaSet: this._mongoConfig.replicaSet,
useNewUrlParser: true,
},
(err, client) => {
if (err) {
this._logger.error('Could not connect to MongoDB', {
method: 'NotificationConfigManager._setupMongoClient',
error: err.message,
});
return cb(err);
}
}).then((client) => {

Check warning on line 99 in extensions/notification/NotificationConfigManager.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected parentheses around single function argument
this._logger.debug('Connected to MongoDB', {
method: 'NotificationConfigManager._setupMongoClient',
});

try {
this._mongoClient = client.db(this._mongoConfig.database, {
ignoreUndefined: true,
Expand All @@ -129,6 +122,12 @@ class NotificationConfigManager {
} catch (error) {
return cb(error);
}
}).catch((err) => {

Check warning on line 125 in extensions/notification/NotificationConfigManager.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected parentheses around single function argument
this._logger.error('Could not connect to MongoDB', {
method: 'NotificationConfigManager._setupMongoClient',
error: err.message,
});
return cb(err);
});
}

Expand Down
17 changes: 8 additions & 9 deletions extensions/utils/LocationStatusStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ class LocationStatusStream {
replicaSet: this._mongoConfig.replicaSet,
useNewUrlParser: true,
useUnifiedTopology: true,
}, (err, client) => {
if (err) {
this._log.error('Could not connect to MongoDB', {
method: 'ServiceStatusManager._setupMongoClient',
error: err.message,
});
return cb(err);
}
}).then((client) => {

Check warning on line 70 in extensions/utils/LocationStatusStream.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected parentheses around single function argument
// connect to metadata DB
this._mongoClient = client.db(this._mongoConfig.database, {
ignoreUndefined: true,
Expand All @@ -96,7 +89,13 @@ class LocationStatusStream {
return cb();
});
return undefined;
});
}).catch((err) => {

Check warning on line 92 in extensions/utils/LocationStatusStream.js

View workflow job for this annotation

GitHub Actions / tests

Unexpected parentheses around single function argument
this._log.error('Could not connect to MongoDB', {
method: 'ServiceStatusManager._setupMongoClient',
error: err.message,
});
return cb(err);
});
}

/**
Expand Down
17 changes: 9 additions & 8 deletions lib/api/BackbeatAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,14 +1339,7 @@ class BackbeatAPI {
replicaSet: mongoConfig.replicaSet,
useNewUrlParser: true,
useUnifiedTopology: true,
}, (err, client) => {
if (err) {
this._logger.error('Could not connect to MongoDB', {
method: 'BackbeatAPI._setupMongoClient',
error: err.message,
});
return cb(err);
}
}).then((client) => {
// connect to metadata DB
this._mongoClient = client.db(mongoConfig.database, {
ignoreUndefined: true,
Expand All @@ -1355,6 +1348,14 @@ class BackbeatAPI {
method: 'BackbeatAPI._setupMongoClient',
});
return cb();
}).catch((err) => {
if (err) {
this._logger.error('Could not connect to MongoDB', {
method: 'BackbeatAPI._setupMongoClient',
error: err.message,
});
return cb(err);
}
});
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/util/LocationStatusManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class LocationStatusManager {
* @return {undefined}
*/
_initCollection(cb) {
this._mongoClient.createCollection(locationStatusCollection, err => {
this._mongoClient.createCollection(locationStatusCollection).then(() => {
this._locationStatusColl = this._mongoClient.collection(locationStatusCollection);
return cb();
}).catch(err => {
// in case the collection already exists, we ignore the error
if (err && err.codeName !== 'NamespaceExists') {
this._logger.error('Could not create mongo collection', {
Expand All @@ -78,8 +81,6 @@ class LocationStatusManager {
});
return cb(err);
}
this._locationStatusColl = this._mongoClient.collection(locationStatusCollection);
return cb();
});
}

Expand Down
11 changes: 4 additions & 7 deletions tests/functional/ingestion/IngestionReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,13 @@ describe('ingestion reader tests with mock', function fD() {
const topic = testConfig.extensions.ingestion.topic;
async.waterfall([
next => {
MongoClient.connect(mongoUrl, {}, (err, client) => {
if (err) {
next(err);
}
MongoClient.connect(mongoUrl, {}).then((client) => {
this.client = client;
this.db = this.client.db('metadata', {
ignoreUndefined: true,
});
next();
});
}).catch(next);
},
next => kafkaAdminClient.createTopic({
topic,
Expand Down Expand Up @@ -153,10 +150,10 @@ describe('ingestion reader tests with mock', function fD() {
consumer.subscribe([testConfig.extensions.ingestion.topic]);
setTimeout(next, 2000);
},
next => this.db.createCollection('PENSIEVE', err => {
next => this.db.createCollection('PENSIEVE').catch((err) => {
assert.ifError(err);
return next();
}),
}).then(next),
next => {
this.m = this.db.collection('PENSIEVE');
this.m.insertOne(dummyPensieveCredentials, {});
Expand Down

0 comments on commit df36e67

Please sign in to comment.