Skip to content

Commit ce7e40e

Browse files
killagudead-horse
authored andcommitted
fix: should listen redis ready event before app start (eggjs#29)
1 parent 2d8f019 commit ce7e40e

File tree

10 files changed

+78
-25
lines changed

10 files changed

+78
-25
lines changed

.autod.conf.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ module.exports = {
1515
'eslint',
1616
'eslint-config-egg',
1717
'supertest',
18+
'typescript',
19+
],
20+
dep: [
21+
'@types/ioredis'
1822
],
1923
exclude: [
2024
'./test/fixtures',
2125
],
22-
}
26+
};

lib/redis.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const assert = require('assert');
4+
const awaitFirst = require('await-first');
45

56
module.exports = app => {
67
app.addSingleton('redis', createClient);
@@ -51,15 +52,8 @@ function createClient(config, app) {
5152

5253
app.beforeStart(async () => {
5354
const index = count++;
54-
if (app.config.redis.supportTimeCommand) {
55-
const serverTimes = await client.time();
56-
// [ '1543378095', '393297' ]
57-
const dateTime = new Date(Number(String(serverTimes[0]) + String(serverTimes[1]).substring(0, 3)));
58-
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, redis server currentTime: ${dateTime}`);
59-
} else {
60-
await client.get('egg-redis-hello');
61-
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, client ready`);
62-
}
55+
await awaitFirst(client, [ 'ready', 'error' ]);
56+
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, client ready`);
6357
});
6458

6559
return client;

package.json

+12-11
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@
2121
"database"
2222
],
2323
"dependencies": {
24-
"ioredis": "^3.2.2",
25-
"@types/ioredis": "^4.0.1"
24+
"@types/ioredis": "^4.0.10",
25+
"await-first": "^1.0.0",
26+
"ioredis": "^4.9.0"
2627
},
2728
"devDependencies": {
2829
"@types/node": "^10.9.4",
29-
"typescript": "^3.0.3",
30-
"autod": "^3.0.1",
31-
"egg": "^2.3.0",
32-
"egg-bin": "^4.4.0",
33-
"egg-ci": "^1.8.0",
34-
"egg-mock": "^3.14.0",
35-
"eslint": "^4.18.1",
36-
"eslint-config-egg": "^7.0.0",
37-
"supertest": "^3.0.0",
30+
"autod": "^3.1.0",
31+
"egg": "^2.21.1",
32+
"egg-bin": "^4.13.0",
33+
"egg-ci": "^1.11.0",
34+
"egg-mock": "^3.22.2",
35+
"eslint": "^5.16.0",
36+
"eslint-config-egg": "^7.3.1",
37+
"supertest": "^4.0.2",
38+
"typescript": "^3.4.5",
3839
"utility": "^1.9.0"
3940
},
4041
"engines": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
return class HomeController extends app.Controller {
5+
* index() {
6+
const { ctx, app } = this;
7+
yield app.redis.set('foo', 'bar');
8+
ctx.body = yield app.redis.get('foo');
9+
}
10+
};
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = function(app) {
4+
app.get('/', 'home.index');
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
exports.redis = {
4+
client: {
5+
host: '127.0.0.1',
6+
port: 6379,
7+
password: '',
8+
db: '0',
9+
enableOfflineQueue: false,
10+
},
11+
agent: false,
12+
};
13+
14+
exports.keys = 'keys';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "redisapp"
3+
}

test/fixtures/apps/ts-multi/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"lib": [
88
"es2017"
99
],
10-
"skipLibCheck": false
10+
"skipLibCheck": false,
11+
"noImplicitAny": false
1112
},
1213
"include": [
1314
"../../../../**/*"
1415
]
15-
}
16+
}

test/fixtures/apps/ts/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"lib": [
88
"es2017"
99
],
10-
"skipLibCheck": false
10+
"skipLibCheck": false,
11+
"noImplicitAny": false
1112
},
1213
"include": [
1314
"../../../../**/*"
1415
]
15-
}
16+
}

test/redis.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,23 @@ describe('test/redis.test.js', () => {
128128
.expect('bar');
129129
});
130130
});
131+
132+
describe('await ready event', () => {
133+
let app;
134+
before(async () => {
135+
app = mm.app({
136+
baseDir: 'apps/redisapp-disable-offline-queue',
137+
});
138+
await app.ready();
139+
});
140+
after(() => app.close());
141+
afterEach(mm.restore);
142+
143+
it('should query', () => {
144+
return request(app.callback())
145+
.get('/')
146+
.expect(200)
147+
.expect('bar');
148+
});
149+
});
131150
});

0 commit comments

Comments
 (0)