Physicsman
Physicsman

Reputation: 181

How to effectively lock a text file while using it in NodeJS?

I have several independent scripts reading and writing from the same text files. I'm trying to lock the text files while a module is reading/writing from them. At the moment I am using the lockfile package but it does not seem to be working. e.g.

//lock file before reading

lockFile.lockSync("./Config/presetString.txt.lock",10000,100,10000,1000,100)

//read file

var preset = fs.readFileSync("./Config/presetString.txt", 'utf8');

//unlock file

lockFile.unlockSync("./Config/presetString.txt.lock",10000,100,10000,1000,100)

However, when lots of modules are running, it sometimes throws an error which brings everything to a halt. The error states that a .lock file already exists. This seems counterintuitive - if a .lock file already exists then the modules should wait until it doesn't exist.With the options above, the modules should retry accessing the lock 1000 times but this does not seem to be working.

Any ideas on how to prevent this?

Here is an example error that is thrown:

Error: EEXIST: file already exists, open './Config/presetString.txt.lock'

Upvotes: 4

Views: 5621

Answers (2)

v.babak
v.babak

Reputation: 906

I've found fs-ext is working perfect and REALLY performs flock!

npm install fs-ext

JavaScript:

Process 1

const fs = require("fs");
const {flockSync} = require('fs-ext');
const fd = fs.openSync('file.txt', 'w');
flockSync(fd, 'ex');

Process 2

const fs = require("fs");
const {flockSync} = require('fs-ext');
const fd = fs.openSync('file.txt', 'r');
flockSync(fd, 'sh'); // PENDING until Process 1 release exclusive lock!

Upvotes: 1

karthickj25
karthickj25

Reputation: 1197

From the documentation

Sync methods return the value/throw the error, others don't. Standard node fs stuff.

You need to check for existing lock and use callback

 // opts is optional, and defaults to {}
lockFile.lock('some-file.lock', opts, function (er) {
   // if the er happens, then it failed to acquire a lock.
   // if there was not an error, then the file was created,
   // and won't be deleted until we unlock it.

  //read file

   var preset = fs.readFileSync("./Config/presetString.txt", 'utf8');

   // do my stuff, free of interruptions
   // then, some time later, do:
lockFile.unlock('some-file.lock', function (er) {
   // er means that an error happened, and is probably bad.
   })
})

Upvotes: 3

Related Questions