Skip to content

Update ioredis and ioredismock #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ module.exports = {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');

this.log(`Listing initial revisions for key: \`${keyPrefix}\``, { verbose: true });
this.log(`Fetching initial revisions for key: \`${keyPrefix}\``, { verbose: true });
try {
let initialRevisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
Expand All @@ -163,8 +163,7 @@ module.exports = {
async fetchRevisions(/* context */) {
let redisDeployClient = this.readConfig('redisDeployClient');
let keyPrefix = this.readConfig('keyPrefix');

this.log(`Listing revisions for key: \`${keyPrefix}\``);
this.log(`Fetching revisions for key: \`${keyPrefix}\``, { verbose: true });
try {
let revisions = await redisDeployClient.fetchRevisions(keyPrefix);
return {
Expand Down
36 changes: 17 additions & 19 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = CoreObject.extend({

this._client = new RedisLib(redisOptions);

this._maxRecentUploads = options.maxRecentUploads;
this._allowOverwrite = options.allowOverwrite;
this._maxRecentUploads = options.maxRecentUploads || 10;
this._allowOverwrite = options.allowOverwrite || false;
this._activationSuffix = options.activationSuffix || 'current';
},

Expand Down Expand Up @@ -63,24 +63,22 @@ module.exports = CoreObject.extend({

async fetchRevisions(keyPrefix) {
let revisions = await this._listRevisions(keyPrefix);
let results = await RSVP.hash({
current: this.activeRevision(keyPrefix),
revisionData: this._revisionData(keyPrefix, revisions)
});
let current = await this.activeRevision(keyPrefix);
let revisionData = await this._revisionData(keyPrefix, revisions);
return revisions.map(function(revision, i) {
let hash = {
revision: revision,
active: revision === results.current,
active: revision === current,
};
if (results.revisionData) {
hash.revisionData = results.revisionData[i];
if (revisionData) {
hash.revisionData = revisionData[i];
}
return hash;
});
},

activeRevision(keyPrefix) {
var currentKey = keyPrefix + ':' + this._activationSuffix;
let currentKey = keyPrefix + ':' + this._activationSuffix;
return this._client.get(currentKey);
},

Expand All @@ -90,7 +88,7 @@ module.exports = CoreObject.extend({
}
let dataKeys = revisions.map((rev) => `${keyPrefix}:revision-data:${rev}`);

let data = this._client.mget(dataKeys);
let data = await this._client.mget(dataKeys);
if (!data) {
return;
}
Expand Down Expand Up @@ -161,17 +159,17 @@ module.exports = CoreObject.extend({
let client = this._client;
let listKey = `${keyPrefix}:revisions`;

let results = await RSVP.hash({
revisionsToBeRemoved: client.zrange(listKey, 0, -(maxEntries + 1)),
current: this.activeRevision(keyPrefix)
});
let revisions = results.revisionsToBeRemoved;
let current = results.current;
if (!revisions) {
let revisionCount = await client.zcard(listKey);
let revisionsToBeRemoved;
if (revisionCount > maxEntries) {
revisionsToBeRemoved = await client.zrange(listKey, 0, revisionCount - maxEntries - 1);
}
if (!revisionsToBeRemoved) {
return;
}
let current = await this.activeRevision(keyPrefix);
let promises = [];
revisions.forEach(function(revision) {
revisionsToBeRemoved.forEach(function(revision) {
if (revision !== current) {
promises.push(client.del(`${keyPrefix}:${revision}`));
promises.push(client.del(`${keyPrefix}:revision-data:${revision}`));
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"chalk": "^4.0.0",
"core-object": "^3.1.5",
"ember-cli-deploy-plugin": "^0.2.9",
"ioredis": "^3.2.2",
"ioredis": "^4.17.0",
"rsvp": "^4.8.5"
},
"devDependencies": {
Expand All @@ -29,8 +29,8 @@
"chai-as-promised": "^7.1.1",
"ember-cli": "^3.18.0",
"eslint": "^7.0.0",
"ioredis-mock": "^3.14.0",
"glob": "^7.1.6",
"ioredis-mock": "^4.19.0",
"mocha": "^7.1.2",
"release-it": "*",
"sinon": "^9.0.2"
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/lib/redis-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ describe('redis', function () {
maxRecentUploads: 2
}, IoredisMock);

await redis.upload('key', 1, '1value');
await redis.upload('key', 2, '2value');
await redis.upload('key', 3, '3value');
let values = await redis._client.mget('key:1', 'key:revision-data:1')
assert.equal(values.filter(Boolean).length, 0, 'Expected key:1 and key:revision-data:1 to be deleted.');
let value = await redis._client.zrange('key:revisions', 0, -1);
assert.deepEqual(value, ['2', '3']);
await redis.upload('key', 1, '1value');
await redis.upload('key', 2, '2value');
await redis.upload('key', 3, '3value');
let values = await redis._client.mget('key:1', 'key:revision-data:1')
assert.equal(values.filter(Boolean).length, 0, 'Expected key:1 and key:revision-data:1 to be deleted.');
let value = await redis._client.zrange('key:revisions', 0, -1);
assert.deepEqual(value, ['2', '3']);
});

it('trims the list of recent uploads but leaves the active one', async function () {
Expand Down
Loading