Skip to content

ECONNRESET Error using ftp after few mintues #280

@sudhir-pandey24

Description

@sudhir-pandey24

I am using FTP Connection and download all files from folder.

I have written this script , its downloading few files but after few min getting this error.

Below my script:

     //create a connection to ftp server
     var downloadList = [];
     var allFileNames = [];

     const ftpConnection = async (callback) => {
 try{
	ftp_client.on('ready', function() { 
		ftp_client.list('/Gracenote',function(err, list) {
			list.map(function(entry){
				if (entry.name !== '.' && entry.name !== '..' && typeof entry.name !== 'undefined') {
					const pathArray = "data/" + entry.name;
					if (!fs.existsSync(pathArray)) {
						downloadList.push(entry.name);
					}
				}
			});	
			callback(downloadList);
			ftp_client.end();
		});
	});
	ftp_client.end();
      } catch (error) {
	       console.log(error);
      }
     }

       ftpConnection(function(a) {
           allFileNames = a;

         // Defining the queue
           const queue = asyncProccess.queue((task, completed) => {
                 console.log("Currently Busy Processing Task " + task);
                 // Simulating a Complex task
                  setTimeout(()=>{
	                    // The number of tasks to be processed
	                   const remaining = queue.length();
	                   completed(null, {task, remaining});
	                  downloadXMLFile(task);
                    }, 3000);
                   }, 1); // The concurrency value is 1

            // Adding the each task to the queue
           allFileNames.forEach((task)=>{
	     queue.push(task, (error, {task, remaining})=>{
		if(error){
		 console.log(`An error occurred while processing task ${task}`);
		}else {
		 console.log(`Finished processing task ${task}. ${remaining} tasks remaining`);
	       }
	   }); 
	
         });
	// Executes the callback when the queue is done processing all the tasks
         queue.drain(() => {
	          console.log('Successfully processed all items');
            });

       });

       ftp_client.connect(ftpConfig);

       const downloadXMLFile = async function (fileName) {
    try{
	const path = "data/" + fileName;
	if (!fs.existsSync(path)) {
		let d = new FTPClient();
		d.on('ready', function() {
			if (fileName !== '.' && fileName !== '..' && typeof fileName !== 'undefined') {
				d.get(`Gracenote/${fileName}`, function(err, stream) {
				if (err) console.dir(err);
				if(!err){
						console.log(`copied file Gracenote/${fileName}`);
						stream.once('close', function() { d.end(); });
						stream.pipe(fs.createWriteStream('data/'+ fileName,{flags: 'w'}));
				}
				});
			}
		});
		d.end();
		d.connect(ftpConfig);
	}	
        }catch (err) {
	          return err.message;
        }
        };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions