javascript - How to effectively lock a text file while using it in NodeJS? - Stack Overflow

I have several independent scripts reading and writing from the same text files. I'm trying to loc

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'

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'
Share Improve this question edited Jan 23, 2019 at 4:44 Physicsman asked Jan 23, 2019 at 4:25 PhysicsmanPhysicsman 1811 gold badge2 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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.
   })
})

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745232216a4617728.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信