Skip to content

Commit

Permalink
Merge branch 'master' into redis-config-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
iamigo committed Aug 14, 2017
2 parents 045d497 + a1fec51 commit 599cfb9
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 155 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({ where: { isPublished: true } })
.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
2 changes: 1 addition & 1 deletion tests/enforceToken/collector/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe(`api: POST ${path}`, () => {
const deRegisterPath = `/v1/collectors/${res.body.id}/deregister`;
return Collector.destroy({
where: {},
truncate: true
truncate: true,
})
.then(() => {
api.post(deRegisterPath)
Expand Down
18 changes: 3 additions & 15 deletions tests/enforceToken/createToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('api: createToken', () => {
.send(u.fakeUserCredentials)
.end((err, res) => {
if (err) {
done(err);
return done(err);
}

defaultToken = res.body.token;
Expand All @@ -40,13 +40,7 @@ describe('api: createToken', () => {
.send({ name: 'newToken' })
.expect(constants.httpStatus.FORBIDDEN)
.expect(/No authorization token was found/)
.end((err) => {
if (err) {
return done(err);
}

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

it('error if wrong token found provided', (done) => {
Expand All @@ -55,13 +49,7 @@ describe('api: createToken', () => {
.send({ name: 'newToken' })
.expect(constants.httpStatus.FORBIDDEN)
.expect(/Invalid Token/)
.end((err) => {
if (err) {
return done(err);
}

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

it('sucessful authentication, create token for user', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/enforceToken/revoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('enforceToken: revoke:', () => {
.send(u.fakeUserCredentials)
.end((err, res) => {
if (err) {
done(err);
return done(err);
}

defaultToken = res.body.token;
Expand Down
17 changes: 2 additions & 15 deletions tests/enforceToken/sampleUpsertBulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* tests/tokenReq/sampleUpsertBulk.js
*/
'use strict';

const expect = require('chai').expect;
const supertest = require('supertest');
const api = supertest(require('../../index').app);
Expand Down Expand Up @@ -81,13 +80,7 @@ describe('api: POST ' + path, () => {
])
.expect(constants.httpStatus.FORBIDDEN)
.expect(/ForbiddenError/)
.end((err /* , res */) => {
if (err) {
return done(err);
}

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

it('all succeed', (done) => {
Expand All @@ -103,12 +96,6 @@ describe('api: POST ' + path, () => {
},
])
.expect(constants.httpStatus.OK)
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});
});
21 changes: 4 additions & 17 deletions tests/enforceToken/subjectGetHierarchy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* tests/tokenReq/subjectGetHierarchy.js
*/
'use strict';

const supertest = require('supertest');
const api = supertest(require('../../index').app);
const constants = require('../../api/v1/constants');
Expand Down Expand Up @@ -46,7 +45,7 @@ describe(`api: GET ${path}`, () => {
token = returnedToken;
done();
})
.catch((err) => done(err));
.catch(done);
});

before((done) => {
Expand Down Expand Up @@ -82,7 +81,7 @@ describe(`api: GET ${path}`, () => {
password: 'abcd',
}))
.then(() => done())
.catch((err) => done(err));
.catch(done);
});

after(u.forceDelete);
Expand All @@ -93,13 +92,7 @@ describe(`api: GET ${path}`, () => {
api.get(path.replace('{key}', ipar))
.expect(constants.httpStatus.FORBIDDEN)
.expect(/ForbiddenError/)
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});
it('should be an empty object at the parent level (with token)', (done) => {
api.get(path.replace('{key}', ipar))
Expand All @@ -109,13 +102,7 @@ describe(`api: GET ${path}`, () => {
expect(res.body.samples).to.be.an('array');
expect(res.body.samples).to.be.empty;
})
.end((err /* , res */) => {
if (err) {
return done(err);
}

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

0 comments on commit 599cfb9

Please sign in to comment.