1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var util = require('util');
|
8 | var events = require('events');
|
9 | var os = require('os');
|
10 | var leiNS = require('lei-ns');
|
11 | var createDebug = require('debug');
|
12 | var utils = require('lei-utils');
|
13 | var clone = require('clone');
|
14 | var Redis = require('ioredis');
|
15 | var define = require('./define');
|
16 | module.exports = exports = utils.merge(utils, exports);
|
17 |
|
18 |
|
19 | exports.debug = function (n) {
|
20 | return createDebug('clouds:' + n);
|
21 | };
|
22 |
|
23 | exports.clone = clone;
|
24 |
|
25 | exports.createNamespace = function (data) {
|
26 | var ns = new leiNS.Namespace();
|
27 | Object.keys(data).forEach(function (k) {
|
28 | ns(k, data[k]);
|
29 | });
|
30 | return ns;
|
31 | };
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | exports.uniqueId = function (type) {
|
40 |
|
41 | var name = utils.md5(os.hostname()).substr(0, 8);
|
42 | type = type.toLowerCase().substr(0, 1);
|
43 | var ret = [type, name, process.pid, exports.uniqueId.counter++].join('.');
|
44 |
|
45 | debug('generate uniqueId: %s', ret);
|
46 | return ret;
|
47 | };
|
48 |
|
49 | exports.uniqueId.counter = 0;
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | exports.inheritsEventEmitter = function (fn) {
|
57 | util.inherits(fn, events.EventEmitter);
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | exports.inheritsBase = function (fn) {
|
66 | util.inherits(fn, require('./base'));
|
67 | };
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | exports.createRedisConnection = function (options) {
|
76 | options = options || {};
|
77 | options.host = options.host || define.redisHost;
|
78 | options.port = options.port || define.redisPort;
|
79 | options.db = options.db || define.redisDb;
|
80 | options.password = options.password || options.auth_pass;
|
81 | var client = new Redis(options);
|
82 | debug('create redis connection: %s:%s [%s]', options.host, options.port, options.db);
|
83 | return client;
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | var debug = exports.debug('utils');
|