forked from mozilla/galaxy-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
110 lines (94 loc) · 3.15 KB
/
db.js
File metadata and controls
110 lines (94 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var fs = require('fs');
var path = require('path');
var url = require('url');
var redis = require('redis');
var utils = require('./lib/utils');
var redisURL = url.parse(process.env.REDIS_URL ||
process.env.REDISCLOUD_URL ||
process.env.REDISTOGO_URL ||
'');
redisURL.hostname = redisURL.hostname || 'localhost';
redisURL.port = redisURL.port || 6379;
function redisClient() {
var client = redis.createClient(redisURL.port, redisURL.hostname);
if (redisURL.auth) {
client.auth(redisURL.auth.split(':')[1]);
}
return client;
}
exports.redis = redisClient;
// TODO: Make this use a pool.
var persistentRedisConnection;
function redisView(view, persistent) {
/*
Exposes a view wrapper that establishes a connection to the redis
server, processes the view, then cleans up the connection.
server.post(
'/foo/bar',
redisView(function(client, done, req, res, wrap) {
client.set('hello', 'cvan');
somethingAsync(wrap(function(err) {
if (err) {
throw new Error('This will close the connection');
}
res.send('haldo');
client.set('goodbye', 'cvan');
done();
}));
})
);
Passing a truthy value to the `persistent` argument will use a single
persistent connection rather than creating a new one for each request.
Wrap async functions with `wrap` to close the connection when errors
are thrown within those functions.
Call `done()` at every point that your view can finish executing.
`wrap` will always be the final argument passed to the view. `client`
and `done` will always be the first two.
Note that you should never call `client.end()` if you're using a
persistent connection.
*/
return function() {
var args = Array.prototype.slice.call(arguments, 0);
var client;
// TODO: Handle connection failures and return a 5xx
if (!persistent) {
client = redisClient();
} else {
client = persistentRedisConnection ||
(persistentRedisConnection = redisClient());
}
var killed = false;
function done() {
// Don't clean up the persistent connection, or if we've already
// cleaned up.
if (persistent || killed) {
return;
}
client.quit();
killed = true;
}
function wrap(call) {
return function() {
try {
return call.apply(this, arguments);
} catch(e) {
done();
throw e;
}
};
}
return wrap(view).apply(this, [client, done].concat(args).concat([wrap]));
};
}
exports.redisView = redisView;
function plsNoError(res, done, callback) {
return function(err, result) {
if (err) {
res.json(500, {error: 'db_error'});
done();
return;
}
callback(result);
};
}
exports.plsNoError = plsNoError;