0

I have the below code in node.js . Its a long running background service which

  1. scans a messages table for status = 0
  2. reads date from it
  3. Sends email using Amazon SES
  4. Updates the status to 1 in the messages table

If i run this on the my mac the memory usage is fairly constant below 30M.But when I run this on our CENTOS server the memory consumtion keeps increasing .I am fairly new to node.js . Can some one guide me ? Is this because of recursion ?

   var nodemailer = require('../lib/nodemailer'),
    fs = require("fs"),
    pathlib = require("path");
    var mysql = require("mysql");

    // Create an Amazon SES transport object
    var transport = nodemailer.createTransport("SES", {
        AWSAccessKeyID: "xxxxxxx",
        AWSSecretKey: "yyyyyyyyy",
        ServiceUrl: "https://email.us-east-1.amazonaws.com" // optional
    });

    console.log('SES Configured');

    var connection = mysql.createConnection({
        user: "user",
        password: "xxxxxx",
        database: "db_content"
    }); 

    // Message object

    function loop(){
        console.log("Reading from DB");
        connection.query('SELECT * FROM mm_messages where status = 0 limit 1;', function (error, rows, fields) {

            if( rows.length == 1 ) {        
                var message = {
                    from: 'abc <abc@xxxxx.com>',
                    to: rows[0]["to"],
                    subject: rows[0]["subject"] + rows[0]["id"], //
                    text: 'Hello to myself!',
                    html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'
                };

                console.log('Sending Mail');

                transport.sendMail(message, function(error){
                    if(error){
                        console.log('Error occured');
                        console.log(error.message);
                    //return;
                    }

                    console.log('Message sent successfully!');


                    connection.query('update mm_messages set status = 1 where id='+rows[0]["id"] + ';', function (errorq, rowsq, fieldsq) {
                        message = null;
                        error = null;
                        errorq = null;
                        rowsq = null;
                        rows = null;
                        fieldsq = null;
                        fields = null;
                        setTimeout(loop, 2000);     
                    });

                });
            } else {
                message = null;         
                rows = null;
                fields =null;
                error= null;
                setTimeout(loop, 2000);
            }

        });


    }

    loop();
4

1 回答 1

0

这对我来说仍然看起来很温和。这里有一些你可以改变然后重新测量的东西。

  • 将内联匿名回调函数转换为顶级命名函数。
    • connection.query('SELECT * FROM mm_messages where status = 0 limit 1;', function (error, rows, fields) {
    • connection.query('SELECT * FROM mm_messages where status = 0 limit 1;', messageCallback
  • 使用参数化 SQL 而不是字符串连接来构建 SQL 更新语句
  • 删除所有这些foo = null;陈述。它们是不必要的。它们是单调的。它们使您的代码比需要的更长。它们是由 FUD 驱动的,而不是有实际目的。
于 2013-10-01T14:52:01.150 回答