Node.js - Organising code and closures - SFTP/Inotify

I was hoping I could get some advice on why my nodejs program is behaving in the way it is.

I am using two modules, node-sftp and node-inotify. I have setup node-inotify to watch a directory and call a function when something is written there, the function being an sftp upload.

Now the problem I have is that processing one file at a time is fine but when I drop 4 files in one go there, the function is called four times but only one sftp upload goes through.

Do I need to order my code in a particular way to ensure that the sftp upload occurs x times, is this something to do with closures perhaps?

This is a basic version of my code...

  • "event_handler" is called when something happens on a "watched" directory
  • "check_event" figures out if this type of event is one we want, in this case it's a "write"
  • "ftp_to_server" prepare connection details
  • "do_ftp" basically uses the node-sftp module to perform the sftp upload

    event_handler = function(event){
        var supplier;
        check_event(event, supplier, type, ftp_to_server);
    };
    

=================

    function check_event(event, handler)
    {
        if (event.type === 'xxxxxx') {
            var file_to_process_name = 'abc';
            var file_to_process_dir = 'abc';
            var remote_dir = 'abc';
            handler(file_to_process_name, file_to_process_dir, remote_dir);
        }
    }

    function ftp_to_server(file_to_process_name, file_to_process_dir, remote_dir) {
        var connection_details = conf.ftp.connections
        do_ftp(connection_details, file_to_process_name, file_to_process_dir, remote_dir);    
    }

    function do_ftp(connection_details, file_to_process_name, file_to_process_dir, remote_dir) {

        var credentials = {
            // FTP settings here
        };
        var local_file = file_to_process_dir + file_to_process_name;
        var remote_file = remote_dir + file_to_process_name;

        connection = new sftp(credentials, function(err) {
            if (err){
                throw err;
            }
            connection.writeFile(remote_file, fs.readFileSync(local_file, "utf8"), null, function(err) {
                if (err) {
                    throw err;
                }

                console.info('FTP PUT DONE');

            });
        });
    };

Your "connection = new sftp(credentials, function(err) {" should be var connection = new sftp(credentials, function(err) {

The way you currently have it coded, "connection" is a global and you are writing over it.