Skip to content

Commit

Permalink
Merge branch 'master' into returnUnpublishedSubjectOnPopulateSamples
Browse files Browse the repository at this point in the history
  • Loading branch information
annyhe committed Aug 14, 2017
2 parents 99b8f92 + 90b7f6a commit b43b7db
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 45 deletions.
80 changes: 60 additions & 20 deletions cache/sampleStoreInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const samsto = require('./sampleStore');
const log = require('winston');
const samstoPersist = require('./sampleStorePersist');
const constants = samsto.constants;
const infoLoggingEnabled =
featureToggles.isFeatureEnabled('enableSampleStoreInfoLogging');

/**
* Deletes the previousStatuskey (that stores the previous value of the
Expand Down Expand Up @@ -88,8 +90,13 @@ function eradicate() {
}));
return deletePreviousStatus()
.then(() => Promise.all(promises))
.then(() => log.info('Sample Store eradicated from cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Sample Store eradicated from cache :D');
}

return true;
});
} // eradicate

/**
Expand All @@ -106,8 +113,11 @@ function populateAspects() {
},
})
.then((allAspects) => {
const msg = `Starting to load ${allAspects.length} aspects to cache :|`;
log.info(msg);
if (infoLoggingEnabled) {
const msg = `Starting to load ${allAspects.length} aspects to cache :|`;
log.info(msg);
}

aspects = allAspects;
const getWritersPromises = [];

Expand Down Expand Up @@ -135,8 +145,13 @@ function populateAspects() {

cmds.push(['sadd', constants.indexKey.aspect, aspectIdx]);
return redisClient.batch(cmds).execAsync()
.then(() => log.info('Done loading aspects to cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Done loading aspects to cache :D');
}

return true;
});
})
.catch(console.error); // eslint-disable-line no-console
} // populateAspects
Expand All @@ -149,8 +164,11 @@ function populateAspects() {
function populateSubjects() {
return Subject.findAll()
.then((subjects) => {
const msg = `Starting to load ${subjects.length} subjects to cache :|`;
log.info(msg);
if (infoLoggingEnabled) {
const msg = `Starting to load ${subjects.length} subjects to cache :|`;
log.info(msg);
}

const cmds = [];
subjects.forEach((s) => {
const key = samsto.toKey(constants.objectType.subject, s.absolutePath);
Expand All @@ -163,8 +181,13 @@ function populateSubjects() {
});

return redisClient.batch(cmds).execAsync()
.then(() => log.info('Done loading subjects to cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Done loading subjects to cache :D');
}

return true;
});
})
.catch(console.error); // eslint-disable-line no-console
} // populateSubjects
Expand All @@ -177,8 +200,11 @@ function populateSubjects() {
function populateSamples() {
return Sample.findAll()
.then((samples) => {
const msg = `Starting to load ${samples.length} samples to cache :|`;
log.info(msg);
if (infoLoggingEnabled) {
const msg = `Starting to load ${samples.length} samples to cache :|`;
log.info(msg);
}

const sampleIdx = new Set();
const subjectSets = {};
const sampleHashes = {};
Expand Down Expand Up @@ -227,8 +253,13 @@ function populateSamples() {

// Return once all batches have completed.
return Promise.all(batchPromises)
.then(() => log.info('Done loading samples to cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Done loading samples to cache :D');
}

return true;
});
})
.catch(console.error); // eslint-disable-line no-console
} // populateSamples
Expand All @@ -240,8 +271,11 @@ function populateSamples() {
* false if the feature is not enabled.
*/
function populate() {
const msg = 'Populating redis sample store from db started :|';
log.info(msg);
if (infoLoggingEnabled) {
const msg = 'Populating redis sample store from db started :|';
log.info(msg);
}

let resp;
const promises = [populateSubjects(), populateAspects()];
return Promise.all(promises)
Expand Down Expand Up @@ -286,14 +320,20 @@ function storeSampleToCacheOrDb() {
* "enableRedisSampleStore" flag has been changed from true to false
*/
if (currentStatus) {
log.info('"enableRedisSampleStore" flag was switched to true,' +
' so populating the cache from db');
if (infoLoggingEnabled) {
log.info('"enableRedisSampleStore" flag was switched to true, so ' +
'populating the cache from db');
}

return populate();
}

log.info('"enableRedisSampleStore" flag was switched to false' +
' so persisting to db from cache. The cache will be eradicated ' +
if (infoLoggingEnabled) {
log.info('"enableRedisSampleStore" flag was switched to false so ' +
'so persisting to db from cache. The cache will be eradicated ' +
'after the samples are persisted to db');
}

return samstoPersist.storeSampleToDb() // populate the sample table
.then(() => eradicate()); // eradicate the cache
}
Expand Down
31 changes: 24 additions & 7 deletions cache/sampleStorePersist.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const redisClient = require('./redisCache').client.sampleStore;
const samsto = require('./sampleStore');
const constants = samsto.constants;
const log = require('winston');
const infoLoggingEnabled =
featureToggles.isFeatureEnabled('enableSampleStoreInfoLogging');

/**
* Truncate the sample table in the DB and persist all the samples from redis
Expand All @@ -26,18 +28,27 @@ const log = require('winston');
* @returns {Promise} - which resolves to number of samples persisted to db
*/
function storeSampleToDb() {
log.info('Persist to db started :|. This will start by truncating the ' +
'sample table followed by persisting the sample to db');
if (infoLoggingEnabled) {
log.info('Persist to db started :|. This will start by truncating the ' +
'sample table followed by persisting the sample to db');
}

return Sample.destroy({ truncate: true, force: true })
.then(() => {
log.info('truncated the sample table :|');
if (infoLoggingEnabled) {
log.info('truncated the sample table :|');
}

return redisClient.smembersAsync(constants.indexKey.sample);
})
.then((keys) => keys.map((key) => ['hgetall', key]))
.then((cmds) => redisClient.batch(cmds).execAsync())
.then((res) => {
log.info('Preparing list of samples to persist...');
log.info(`Checking ${res.length} samples...`);
if (infoLoggingEnabled) {
log.info('Preparing list of samples to persist...');
log.info(`Checking ${res.length} samples...`);
}

const samplesToCreate = res.map((sample) => {
sample.relatedLinks = JSON.parse(sample.relatedLinks);
return sample;
Expand All @@ -51,11 +62,17 @@ function storeSampleToDb() {

return true;
});
log.info(`Bulk creating ${samplesToCreate.length} samples...`);
if (infoLoggingEnabled) {
log.info(`Bulk creating ${samplesToCreate.length} samples...`);
}

return Sample.bulkCreate(samplesToCreate);
})
.then((retval) => {
log.info('persisted redis sample store to db :D');
if (infoLoggingEnabled) {
log.info('persisted redis sample store to db :D');
}

return retval.length;
});
} // storeSampleToDb
Expand Down
4 changes: 4 additions & 0 deletions config/toggles.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const longTermToggles = {
// Enable Rooms functionality
enableRooms: environmentVariableTrue(pe, 'ENABLE_ROOMS'),

// Enable sample store info logging
enableSampleStoreInfoLogging: environmentVariableTrue(pe,
'ENABLE_SAMPLE_STORE_INFO_LOGGING'),

// Enable worker activity logging
enableWorkerActivityLogs:
environmentVariableTrue(pe, 'ENABLE_WORKER_ACTIVITY_LOGS'),
Expand Down
10 changes: 1 addition & 9 deletions tests/roomFlag/roomFlagDisabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* tests/api/v1/roomFlag/roomFlagDisabled.js
*/
'use strict';

const supertest = require('supertest');
const api = supertest(require('../../index').app);
const constants = require('../../api/v1/constants');
Expand All @@ -21,13 +20,6 @@ describe('Rooms path is found', () => {
it('GET is found', (done) => {
api.get(path)
.expect(constants.httpStatus.NOT_FOUND)
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});
});

10 changes: 1 addition & 9 deletions tests/roomFlag/roomFlagEnabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* tests/api/v1/roomFlag/roomFlagEnabled.js
*/
'use strict';

const supertest = require('supertest');
const api = supertest(require('../../index').app);
const constants = require('../../api/v1/constants');
Expand All @@ -21,13 +20,6 @@ describe('Rooms path is found', () => {
it('GET is found', (done) => {
api.get(path)
.expect(constants.httpStatus.OK)
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});
});

0 comments on commit b43b7db

Please sign in to comment.