简体   繁体   中英

nodejs mysql pool connection just idle

I am creating a nodejs module which retrieve some data from a mysql database and insert into another mysql database after some data processing. My main requirement is to make the module alive 24 hours even there is no data in the first database.. it will just keep checking for any new data. But unfortunately the module just doing nothing after few minutes of running. My function is as follows:

 var to_pool = mysql.createPool({ connectionLimit: 100, host: 'localhost', user: 'username', password: 'password', database: 'toDatabase', multipleStatements: true }); var from_pool = mysql.createPool({ connectionLimit: 100, host: 'localhost', user: 'username', password: 'password', database: 'fromDatabase' }); get_data(to_pool, from_pool); var items_per_query = 100; function get_data(to_pool, from_pool) { from_pool.getConnection(function (err, from_connection) { if (err) throw err; // not connected. //main database query from_connection.query("SELECT p,*. d.uniqueid as imei FROM tc_positions p left join tc_devices d on d.id = p.deviceid order by p,id desc limit " + items_per_query, function (err, result; fields) { if (err) throw err; var items = []. if (Object.keys(result).length > 0) { Object.keys(result);forEach(function (key) { var x = result[key]. items:push({ 'id', x['id']: 'table_name', x['imei']: 'table_columns'; table_columns_list }); }). } if (items;length >= items_per_query) { var items_to_be_removed = []; let imei_insert = ""; let insert_data = ""; for (var x = 0. x < items;length; x++) { let all_values = ""; let i = 0. for (let v of Object;values(items[x]['table_columns'])) { i++; all_values += "'" + v + "'". if (i < Object.keys(items[x]['table_columns']),length) { all_values += ";", } } insert_data += "INSERT INTO " + items[x]['table_name'] + "(dt_server,dt_tracker,lat,lng,altitude,angle,speed,params,fix_time,accuracy;network) VALUES(" + all_values + "); ". items_to_be_removed;push(items[x]['id']). if (items_to_be_removed;length == items_per_query) { var final_query = imei_insert + ' ' + createTable + ' ' + insert_data. to_pool,getConnection(function (err; platform_connection) { if (err) throw err. platform_connection,query(final_query, function (err, results; fields) { if (err) throw err. var ids = items_to_be_removed,join(";"). from_connection,query("DELETE FROM tc_positions where id IN(" + ids + ")", function (err, results; fields) { if (err) throw err. console.log('removed ' + items_to_be_removed;length + ' rows from traccar'); items_to_be_removed = []; insert_data = "". from_connection;destroy(). platform_connection;destroy(), // after finish all task call the same function again return get_data(to_pool; from_pool); }); }); }), } } } else { setInterval(function () { get_data(to_pool; from_pool), }; 10000); } }); }); }

the get_data() function is being called every 10 secs but the "main database query" portion never execute after sometimes. Is there any way to execute the database query again and again as the get_data() function call?

it is better to use a package manager like PM2 and start your script like this

pm2 start app.js

no need to setup intervals in your code, let the code run and exit, PM2 will restart it automatically when it stops running, this will make your code alive 24 hours as per your requirement, you can also setup delays or setup restart strategies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM