Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****** Load database wrapper *******/
- function loadDatabase(cb, reconn) {
- global.db = require('mysql').createConnection({
- host : global.config.db_use_socket ? undefined : global.config.db_host,
- port : global.config.db_use_socket ? undefined : global.config.db_port,
- socketPath : global.config.db_use_socket ? global.config.db_socket : undefined,
- user : global.config.db_user,
- password : global.config.db_password,
- database : global.config.db_db,
- });
- global.db.connect(function (err) {
- if (err) {
- if (reconn) {
- e('error reconnecting to main database: ' + err.message+'. Retrying in 2 seconds...');
- setTimeout(loadDatabase.bind(null, function(){}, true), 2000);
- } else {
- crit('error connecting to main database: ' + err.message);
- }
- } else {
- p('Connected to main database');
- /****** Make database method wrapper for squel *******/
- global.db.squery = function () { arguments[0] = arguments[0].toString(); this.query.apply(this, arguments); }
- cb();
- }
- });
- /****** Database reconnect *******/
- global.db.on('error', function (err) {
- if (err.code === 'PROTOCOL_CONNECTION_LOST') {
- e('Lost connection to main database. Reconnecting in 2 seconds...');
- setTimeout(loadDatabase.bind(null, function(){}, true), 2000);
- } else {
- e('Main DB error: ' + err.message);
- }
- });
- }
- loadDatabase(function () {
- ....
Advertisement
Add Comment
Please, Sign In to add comment