Skip to content

Commit

Permalink
Redis made necessary in cluster mode: Abe has become a stateful app
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Sep 23, 2016
1 parent e983761 commit df7645b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"prettyjson": "^1.1.3",
"prompt": "^1.0.0",
"qs": "^6.0.1",
"redis": "^2.6.2",
"request": "^2.69.0",
"watch": "^0.17.1",
"xss": "^0.2.10"
Expand Down
7 changes: 6 additions & 1 deletion src/cli/config/abe-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ var config = {
partials: 'templates/partials',
custom: 'custom',
siteUrl: false,
sitePort: false
sitePort: false,
redis: {
enable: true,
port: '6379',
host:'127.0.0.1'
}
}

export default config
20 changes: 11 additions & 9 deletions src/cli/models/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
getSelectTemplateKeys
} from '../../cli'

import * as redis from '../services/RedisClient';

let singleton = Symbol()
let singletonEnforcer = Symbol()

Expand All @@ -32,15 +34,15 @@ class Manager {
}

getList() {
if(config.redis.enable){
redis.get().get('list', function(err, reply) {
this._list = JSON.parse(reply);
});
}

return this._list
}

setList(list) {

this._list = list
}

init() {
var p = new Promise((resolve, reject) => {
this._loadTime = new TimeMesure('Loading Manager')
Expand All @@ -60,13 +62,13 @@ class Manager {
}

updateList() {

this._list = FileParser.getAllFilesWithKeys(this._whereKeys)
this._list.sort(FileParser.predicatBy('date'))
if(config.redis.enable){
redis.get().set('list', JSON.stringify(this._list))
}
this._loadTime.duration()

// this._list = FileParser.getAllFiles(useKeys)
// this._list.sort(FileParser.predicatBy('date'))

return this
}

Expand Down
19 changes: 19 additions & 0 deletions src/cli/services/RedisClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import redis from 'redis'

var state = {
db: null,
}

export function connect(port, host) {
state.db = redis.createClient(port, host);
state.db.on('connect', function() {
console.log('connected to Redis');
});
state.db.on('error', function(err) {
console.log('Error connecting to Redis:' + err);
});
}

export function get() {
return state.db
}
6 changes: 6 additions & 0 deletions src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
middleWebsite,
} from './middlewares'

import * as redis from '../cli/services/RedisClient';

var abePort = null

if(process.env.ROOT) config.set({root: process.env.ROOT.replace(/\/$/, '') + '/'})
Expand Down Expand Up @@ -80,6 +82,10 @@ var html = exphbs.create({

var app = express(opts)

if(config.redis.enable){
redis.connect(config.redis.port, config.redis.host)
}

// Instantiate Singleton Manager (which lists all blog files)
Manager.instance.init();
app.set('config', config.getConfigByWebsite());
Expand Down

0 comments on commit df7645b

Please sign in to comment.