alangilbi
alangilbi

Reputation: 331

EBUSY: resource busy or locked, rename

I have this problem going on with my app, so i have a stage folder where we receive files at, there is a fs.watch on the directory which will monitor files and move the file to another directory once it sees it . Just to mimic the process, I have the app running and copy/paste a file to the stage directory. But i keep getting the error , not sure how to overcome this

EBUSY: resource busy or locked, rename 'C:\Users\a\Desktop\Node js Data Loader\stage_load\data\UI.txt' -> 'C:\Users\a\Desktop\Node js Data Loader\stage_load\stage\UI.txt'

Here a snippet from my code

fs.watch(DATA_PATH,(eventype,filename) =>
    {

        if(filename.length > 0)
        {
            const data_name = path.join(DATA_PATH, filename);
            const stg_name = path.join(STAGE_PATH, filename);
            fs.rename(data_name,stg_name ,(err) =>{
                if(err)
                {
                    console.log('File failed to move to Stage');
                    throw err;
                }

            })
            main();
        }

    })

Seems like the code needs to wait for the full file to be written, not sure

Upvotes: 4

Views: 3637

Answers (1)

Gautier Logeot
Gautier Logeot

Reputation: 83

sorry about the dig up, but I have encounter the same problem today and I have finally found a "solution".

And that post is the second result in google so...

I could solve my problem by writing in my log file asynchronously.

My code was running like this

  1. format the access path to folder
  2. check size of program.log if too big rename with "000x" in the end
  3. fs.appendFile(path,data,callback)

When I tried with fs.appendFileSync() I had automatically the EBUSY. but with the async method that work like a charm.

I think that are some library that allow to do it automatically but I wanted my own code that I could modify and bend like I want

Upvotes: 0

Related Questions