Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Commit

Permalink
fix(cache): Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 11, 2015
1 parent e374d8a commit c5c2b59
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 61 deletions.
6 changes: 4 additions & 2 deletions doc/Plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ The hooks structure must be as following:
/**
* registerHooks
* @param object config explorer configuration
* @param string url the base url of your plugin (/p/pluginName)
* @param mixed user the user object, null if no user
* @param Object {notify, cache}
* @see Notify
* @see Cache
*/
function registerHooks(config, url, user) {
function registerHooks(config, user, utils) {
return {
directory: function(tree, path) {
dosomethingwith(config.plugins.pluginName.myConfigValue)
Expand Down
3 changes: 2 additions & 1 deletion doc/examples/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ https:
enabled: true # default option!
key: './certs/key.pem' # change those are dummies
cert: './certs/cert.pem'
plugins: # those are enable by default, use below options to disable them
plugins: # those are enable by default, use above options to disable them
upload: {}
archive: {}
move: {}
cache: 'memory' # redis is available too
redis: {}
# host: 'somesocket.sock'
Expand Down
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict";
'use strict';
var p = require('path')
var http = require('http')
var https = require('https')
Expand All @@ -15,11 +15,12 @@ var https_options = {

require('./server.js')(config)
.then(function(app) {

var server = http.createServer(app)
.listen(config.port, function() {
if(!config.quiet)
console.log('HTTP listening on %s', config.port)
})
.listen(config.port, function() {
if(!config.quiet)
console.log('HTTP listening on %s', config.port)
})

var socket = require('./lib/socket.js')(server, app)

Expand Down Expand Up @@ -52,7 +53,9 @@ require('./server.js')(config)
console.error(err);
})

return interactor.run(plugins_paths, config)
var cache = app.get('cache')

return interactor.run(plugins_paths, config, cache)
.then(function() {
interactor.ipc.on('notify:*', function(data) {
var event = this.event
Expand Down
3 changes: 2 additions & 1 deletion lib/cache/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var Promise = require('bluebird')
var Cache = require('./cache.js')
var util = require('util')
var redis = require('ioredis')

/**
* RedisCache
Expand All @@ -15,7 +16,7 @@ function RedisCache(namespace, client) {

Cache.apply(this, arguments)

if(!(client.constructor.name == 'Redis')) {
if(!client) {
throw new TypeError("Client should be a Redis instance")
}

Expand Down
3 changes: 1 addition & 2 deletions lib/job/interactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ util.inherits(Interactor, EventEmitter)
* @param array plugins strings representing a directory in plugin_path
* @return Promise resolved when job has started
*/
Interactor.prototype.run = function(plugins, config) {
Interactor.prototype.run = function(plugins, config, cache) {
if(this.job) {
throw new ReferenceError("Job is already running with this interactor")
}

var self = this
let cache = require('../cache/index.js')(config)

debug('Forking job with plugins %o', plugins)

Expand Down
2 changes: 1 addition & 1 deletion lib/job/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Notify(namespace, cache) {
throw new TypeError('Notify needs a namespace, none given')
}

this.memory = new cache(namespace)
this.memory = cache(namespace)
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function registerPluginsRoutes(app) {

var config = app.get('config')
var plugins = app.get('plugins')
var cache = app.get('cache')
var allowKeyAccess = config.allowKeyAccess

for(let name in plugins) {
Expand All @@ -72,7 +73,8 @@ function registerPluginsRoutes(app) {
prepareTree: middlewares.prepareTree(app),
HTTPError: HTTPError,
interactor: interactor,
tree: tree
tree: tree,
cache: cache
}, config)

debug('Using router for plugin %s on /p/%s', name, name)
Expand Down
4 changes: 2 additions & 2 deletions middlewares/prepareTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ function prepareTree(app) {

if(opts.cache === true) {
opts.cache = {
time: new cache('tree:time'),
size: new cache('tree:size')
time: cache('tree:time'),
size: cache('tree:size')
}
}

Expand Down
7 changes: 6 additions & 1 deletion middlewares/registerHooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var p = require('path')
var Promise = require('bluebird')
var Notify = require('../lib/job/notify.js')

var debug = require('debug')('explorer:middlewares:registerHooks')

Expand All @@ -9,6 +10,7 @@ function registerHooks(app) {

var plugins = app.get('plugins')
var config = app.get('config')
var cache = app.get('cache')

return function(req, res, next) {
res.locals.hooks = {}
Expand All @@ -24,7 +26,10 @@ function registerHooks(app) {
for(let name in plugins) {
if('hooks' in plugins[name]) {
debug('Registering hooks for %s', name)
hooks[name] = plugins[name].hooks(config, p.join('/p', name), req.user)
hooks[name] = plugins[name].hooks(config, req.user, {
cache: cache,
notify: Notify
})
}
}

Expand Down
7 changes: 2 additions & 5 deletions plugins/move/hooks.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
var Notify = require('../../lib/job/notify.js')
var fs = require('fs')
var p = require('path')
var hamljs = require('hamljs')
var clipboard = fs.readFileSync(p.join(__dirname, './clipboard.haml'))

function registerHooks(config, url, user) {
var cache = require('../../lib/cache')(config)
var memory = new Notify('clipboard', cache)
function registerHooks(config, user, utils) {
var memory = new utils.notify('clipboard', utils.cache)

return memory.get(user.username)
.then(function(paths) {
Expand All @@ -27,7 +25,6 @@ function registerHooks(config, url, user) {

return hamljs.render(clipboard, {locals: {
paths: paths,
url: url,
path: path
}})
}
Expand Down
3 changes: 1 addition & 2 deletions plugins/move/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ function cutPromises(items, dest) {

var Move = function(router, utils, config) {

var cache = require('../../lib/cache')(config)
var memory = new Notify('clipboard', cache)
var memory = new Notify('clipboard', utils.cache)

/**
* @api {post} /p/move/action/copy Copy
Expand Down
4 changes: 2 additions & 2 deletions plugins/upload/hooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function registerHooks(config, url, user) {
function registerHooks(config) {
return {
menu: function() {

if(config.upload.disabled === true)
return ''

return '<li><a href="'+url+'"><i class="icon-upload"></i>Upload</a></li>'
return '<li><a href="/p/upload"><i class="icon-upload"></i>Upload</a></li>'
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
var express = require('express')
var p = require('path')
var util = require('util')
Expand Down Expand Up @@ -30,7 +31,15 @@ module.exports = function(config) {
app.use(bodyParser.json({limit: config.upload.maxSize}))

app.set('config', config)
app.set('cache', require('./lib/cache')(config))

let cache = require('./lib/cache')(config)

app.set('cache', function getCache(namespace) {
if(config.cache == 'redis')
return new cache(namespace, require('./lib/redis.js')(config))
else
return new cache(namespace)
})

app.set('view engine', 'haml')
app.set('view cache', true)
Expand Down Expand Up @@ -101,7 +110,7 @@ module.exports = function(config) {
return res.status(404).render('404.haml')
})

var users = new Users({database: p.resolve(__dirname, config.database)})
let users = new Users({database: p.resolve(__dirname, config.database)})

//load users from file to memory
return users.load()
Expand Down
13 changes: 3 additions & 10 deletions test/api/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var p = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var async = require('async')
var interactor = require('../../lib/job/interactor.js')
var interactor = bootstrap.interactor

var archive_path = p.join(__dirname, '../fixtures/tmp')
var list
Expand All @@ -18,10 +18,7 @@ describe('archive', function() {

before(bootstrap.autoAgent)
before(bootstrap.login)
before(function() {
this.timeout(5000)
return interactor.run([p.resolve(__dirname, '../../plugins/archive')], bootstrap.config)
})
before(bootstrap.runInteractor([p.resolve(__dirname, '../../plugins/archive')]))

it('should post file', function(cb) {
var getNotification = function(notifications) {
Expand Down Expand Up @@ -57,10 +54,6 @@ describe('archive', function() {

after(bootstrap.logout)
after(bootstrap.removeAgent)

after(function(cb) {
interactor.once('exit', cb)
interactor.kill()
})
after(bootstrap.killInteractor())

})
12 changes: 3 additions & 9 deletions test/api/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var p = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var async = require('async')
var interactor = require('../../lib/job/interactor.js')
var interactor = bootstrap.interactor

var fixtures = p.join(__dirname, '../fixtures/tree/move')
var move_path = p.join(__dirname, '../fixtures/tree/move/dest')
Expand Down Expand Up @@ -38,10 +38,7 @@ describe('move', function() {

before(bootstrap.autoAgent)
before(bootstrap.login)
before(function() {
this.timeout(5000)
return interactor.run([p.resolve(__dirname, '../../plugins/move')], bootstrap.config)
})
before(bootstrap.runInteractor([p.resolve(__dirname, '../../plugins/move')]))

before(deleteFiles)
before(createFiles)
Expand Down Expand Up @@ -190,9 +187,6 @@ describe('move', function() {
after(bootstrap.logout)
after(bootstrap.removeAgent)

after(function(cb) {
interactor.once('exit', cb)
interactor.kill()
})
after(bootstrap.killInteractor())

})
14 changes: 3 additions & 11 deletions test/api/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var p = require('path')
var fs = require('fs')
var rimraf = require('rimraf')
var async = require('async')
var interactor = require('../../lib/job/interactor.js')
var interactor = bootstrap.interactor

var upload_path = p.join(__dirname, '../fixtures/upload')
var list
Expand All @@ -18,10 +18,7 @@ describe('upload', function() {

before(bootstrap.autoAgent)
before(bootstrap.login)
before(function() {
this.timeout(5000)
return interactor.run([p.resolve(__dirname, '../../plugins/upload')], bootstrap.config)
})
before(bootstrap.runInteractor([p.resolve(__dirname, '../../plugins/upload')]))

it('should get upload', function(cb) {
this.request.get('/p/upload')
Expand Down Expand Up @@ -95,10 +92,5 @@ describe('upload', function() {

after(bootstrap.logout)
after(bootstrap.removeAgent)

after(function(cb) {
interactor.once('exit', cb)
interactor.kill()
})

after(bootstrap.killInteractor())
})
5 changes: 3 additions & 2 deletions test/job/interactor.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

var cache = require('../../lib/cache')(bootstrap.config)
var interactor = require('../../lib/job/interactor.js')

describe('interactor', function() {
it('should run', function(cb) {
this.timeout(5000)
interactor.run([__dirname + '/../fixtures/testjob'], bootstrap.config)
interactor.run([__dirname + '/../fixtures/testjob'], bootstrap.config, cache)
.then(function(plugins) {
expect(plugins).to.deep.equal(['testjob'])
return cb()
Expand Down Expand Up @@ -75,7 +76,7 @@ describe('interactor', function() {
})

it('should run again', function(cb) {
interactor.run([__dirname + '/../fixtures/testjob'], bootstrap.config)
interactor.run([__dirname + '/../fixtures/testjob'], bootstrap.config, cache)
interactor.ipc.once('job.start', function(plugins) {
return cb()
})
Expand Down
20 changes: 19 additions & 1 deletion test/tools/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

var p = require('path')
var fs = require('fs')
var util = require('util')
Expand All @@ -18,6 +17,8 @@ if(!fs.existsSync(config.database)) {
fs.writeFileSync(config.database, fs.readFileSync(p.join(cwd, '/../doc/examples/data/users')))
}

var cache = require('../../lib/cache')(config)

var options = {
headers: []
}
Expand Down Expand Up @@ -70,5 +71,22 @@ module.exports = {
removeAgent: function(cb) {
this.request = null
return cb()
},
interactor: require('../../lib/job/interactor.js'),
runInteractor: function(plugins) {
var self = this

return function() {
return self.interactor
.run(plugins, self.config, cache)
}
},
killInteractor: function() {
var self = this

return function(cb) {
self.interactor.once('exit', cb)
self.interactor.kill()
}
}
}

0 comments on commit c5c2b59

Please sign in to comment.