-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
69 lines (56 loc) · 1.84 KB
/
db.js
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
const mysql = require('mysql');
// const cfg = require('./config');
// 只需要保持一个连接
// var conn = mysql.createConnection(cfg.mysqlConnStr);
var conn = mysql.createConnection({
host: '127.0.0.1',
password: '',
database: 'node-blog-crawer-mamicode'
});
//way1
// If the Node process ends, close the Mongoose connection (效果不怎么好可以删除 看mysql版本的api)
// process.on( 'SIGINT', function() {
// conn.end( function ( err ) {
// console.log( 'MySQL connection disconnected through app termination' );
// process.exit( 0 ) ;
// } );
// } );
//
//报错(内存泄露 导致了这个错误 用way2 的方式 就ok)
//spider3 > (node:30211) [DEP0096] DeprecationWarning: timers.unenroll() is deprecated. Please use clearTimeout instead.
//推荐使用这个 pm2 内存占用低 cpu使用也较低 这个是内存泄漏最好的补救
//way2
// If the Node process ends, close the Mongoose connection (没有效果可以删除 看mysql版本的api)
process.on('SIGINT', () => {
const cleanUp = () => {
conn.end(console.error);
process.exit( 0 ) ;
};
server.close(() => {
// Stop after 10 secs
setTimeout(() => {
cleanUp();
process.exit();
}, 10000);
});
// Force close server after 15 secs
setTimeout((e) => {
console.log('Forcing server close !!!', e);
cleanUp();
process.exit(1);
}, 15000);
});
process.on( 'SIGINT', function(){
console.log( 'gracefully shutting down :)' );
conn.end();
process.exit();
});
module.exports = new Promise((resolve, reject) => {
conn.connect((err) => {
if (err) {
console.error('mysql 连接出现错误:', err);
reject(err);
}
resolve(conn);
});
});