Skip to content

Commit bf3ecac

Browse files
committed
Add functionality to copy revision on activation. This enables nginx to do the lookup as redis support is very limited.
change copyToKey to activeContentSuffix add to readme update readme remove copyOnActivate boolean update tests
1 parent ddfdaa8 commit bf3ecac

File tree

5 files changed

+95
-16
lines changed

5 files changed

+95
-16
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ The unique revision number for the version of the file being uploaded to Redis.
122122

123123
*Default:* `context.commandLineArgs.revisionKey || context.revisionData.revisionKey`
124124

125+
### activeContentSuffix
126+
127+
The suffix to be used for the Redis key under which the activated revision content will be stored in Redis. By default this option will be `"current-content"`. This makes the default activated revision in Redis looks like: `project.name() + ':index:current-content'`
128+
129+
This makes it possible to serve content completely from within NGINX using the [redis](https://www.nginx.com/resources/wiki/modules/redis/) module without doing a primary key lookup.
130+
131+
```
132+
server {
133+
location / {
134+
set $redis_key project-name:index:current-content;
135+
redis_pass name:6379;
136+
default_type text/html;
137+
}
138+
}
139+
```
140+
141+
*Default:* `current-content`
142+
125143
### allowOverwrite
126144

127145
A flag to specify whether the revision should be overwritten if it already exists in Redis.

index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
return context.project.name() + ':index';
3737
},
3838
activationSuffix: 'current',
39+
activeContentSuffix: 'current-content',
3940
didDeployMessage: function(context){
4041
var revisionKey = context.revisionData && context.revisionData.revisionKey;
4142
var activatedRevisionKey = context.revisionData && context.revisionData.activatedRevisionKey;
@@ -62,7 +63,8 @@ module.exports = {
6263
if (!this.pluginConfig.url) {
6364
['host', 'port'].forEach(this.applyDefaultConfigProperty.bind(this));
6465
}
65-
['filePattern', 'distDir', 'keyPrefix', 'activationSuffix', 'revisionKey', 'didDeployMessage', 'redisDeployClient', 'maxRecentUploads'].forEach(this.applyDefaultConfigProperty.bind(this));
66+
67+
['filePattern', 'distDir', 'keyPrefix', 'activationSuffix', 'activeContentSuffix', 'revisionKey', 'didDeployMessage', 'redisDeployClient', 'maxRecentUploads'].forEach(this.applyDefaultConfigProperty.bind(this));
6668

6769
this.log('config ok', { verbose: true });
6870
},
@@ -100,13 +102,14 @@ module.exports = {
100102
},
101103

102104
activate: function(/* context */) {
103-
var redisDeployClient = this.readConfig('redisDeployClient');
104-
var revisionKey = this.readConfig('revisionKey');
105-
var keyPrefix = this.readConfig('keyPrefix');
106-
var activationSuffix = this.readConfig('activationSuffix');
105+
var redisDeployClient = this.readConfig('redisDeployClient');
106+
var revisionKey = this.readConfig('revisionKey');
107+
var keyPrefix = this.readConfig('keyPrefix');
108+
var activationSuffix = this.readConfig('activationSuffix');
109+
var activeContentSuffix = this.readConfig('activeContentSuffix');
107110

108111
this.log('Activating revision `' + revisionKey + '`', { verbose: true });
109-
return Promise.resolve(redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix))
112+
return Promise.resolve(redisDeployClient.activate(keyPrefix, revisionKey, activationSuffix, activeContentSuffix))
110113
.then(this.log.bind(this, '✔ Activated revision `' + revisionKey + '`', {}))
111114
.then(function(){
112115
return {

lib/redis.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,11 @@ module.exports = CoreObject.extend({
5252
});
5353
},
5454

55-
activate: function(keyPrefix, revisionKey, activationSuffix) {
56-
var currentKey = keyPrefix + ':' + activationSuffix;
57-
55+
activate: function(keyPrefix, revisionKey, activationSuffix, activeContentSuffix) {
5856
return Promise.resolve()
5957
.then(this._listRevisions.bind(this, keyPrefix))
6058
.then(this._validateRevisionKey.bind(this, revisionKey))
61-
.then(this._activateRevisionKey.bind(this, currentKey, revisionKey));
59+
.then(this._activateRevision.bind(this, keyPrefix, revisionKey, activationSuffix, activeContentSuffix));
6260
},
6361

6462
fetchRevisions: function(keyPrefix) {
@@ -90,9 +88,36 @@ module.exports = CoreObject.extend({
9088
return revisions.indexOf(revisionKey) > -1 ? Promise.resolve() : Promise.reject('`' + revisionKey + '` is not a valid revision key');
9189
},
9290

93-
_activateRevisionKey: function(currentKey, revisionKey) {
91+
_activateRevisionKey: function(keyPrefix, revisionKey, activationSuffix) {
92+
var currentKey = keyPrefix + ':' + activationSuffix;
93+
94+
return this._client.set(currentKey, revisionKey);
95+
},
96+
97+
_activateRevision: function(keyPrefix, revisionKey, activationSuffix, activeContentSuffix) {
98+
if (activeContentSuffix) {
99+
return this._copyRevisionAndActivateRevisionKey(keyPrefix, revisionKey, activationSuffix, activeContentSuffix);
100+
}
101+
102+
return this._activateRevisionKey(keyPrefix, revisionKey, activationSuffix);
103+
},
104+
105+
_copyRevisionAndActivateRevisionKey: function(keyPrefix, revisionKey, activationSuffix, activeContentSuffix) {
94106
var client = this._client;
95-
return client.set(currentKey, revisionKey);
107+
var _this = this;
108+
var activeContentKey = keyPrefix + ':' + activeContentSuffix;
109+
var revisionContentKey = keyPrefix + ':' + revisionKey;
110+
111+
return new Promise(function(resolve, reject) {
112+
client.get(revisionContentKey).then(
113+
function(value) {
114+
client.set(activeContentKey, value).then(function() {
115+
_this._activateRevisionKey(keyPrefix, revisionKey, activationSuffix).then(resolve, reject);
116+
});
117+
},
118+
reject
119+
);
120+
});
96121
},
97122

98123
_uploadIfKeyDoesNotExist: function(redisKey, value) {

tests/unit/index-nodetest.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ describe('redis plugin', function() {
271271

272272
return previous;
273273
}, []);
274-
assert.equal(messages.length, 10);
274+
assert.equal(messages.length, 11);
275275
});
276276
it('adds default config to the config object', function() {
277277
plugin.configure(context);
@@ -310,7 +310,7 @@ describe('redis plugin', function() {
310310

311311
return previous;
312312
}, []);
313-
assert.equal(messages.length, 9);
313+
assert.equal(messages.length, 10);
314314
});
315315
it('does not add default config to the config object', function() {
316316
plugin.configure(context);
@@ -350,7 +350,7 @@ describe('redis plugin', function() {
350350

351351
return previous;
352352
}, []);
353-
assert.equal(messages.length, 9)
353+
assert.equal(messages.length, 10)
354354
});
355355
it('does not add default config to the config object', function() {
356356
plugin.configure(context);
@@ -390,7 +390,7 @@ describe('redis plugin', function() {
390390

391391
return previous;
392392
}, []);
393-
assert.equal(messages.length, 8);
393+
assert.equal(messages.length, 9);
394394
});
395395

396396
it('does not add default config to the config object', function() {

tests/unit/lib/redis-nodetest.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,39 @@ describe('redis', function() {
245245
assert.equal(redisValue, 'c');
246246
});
247247
});
248+
249+
it('copies revision to the activeContentSuffix', function() {
250+
var redisKey, redisValue;
251+
252+
var redisClient = new FakeRedis(FakeClient.extend({
253+
_db: {
254+
"key-prefix:a": "first revision content",
255+
"key-prefix:b": "second revision content",
256+
"key-prefix:c": "third revision content"
257+
},
258+
259+
get: function(key) {
260+
return Promise.resolve(this._db[key]);
261+
},
262+
set: function(key, value) {
263+
this._db[key] = value;
264+
return Promise.resolve(value);
265+
},
266+
}));
267+
268+
var redis = new Redis({}, redisClient);
269+
270+
redis._client.recentRevisions = ['a', 'b', 'c'];
271+
272+
var activate = redis.activate('key-prefix', 'c', 'current-id', 'current-content').then(function() {
273+
return redis._client.get('key-prefix:current-content');
274+
});
275+
276+
return assert.isFulfilled(activate)
277+
.then(function(result) {
278+
assert.equal(result, "third revision content");
279+
});
280+
});
248281
});
249282

250283
describe('#fetchRevisions', function() {

0 commit comments

Comments
 (0)