Closed fritx closed 9 years ago
This module already makes a best-effort attempt to unlock files on process exit.
https://github.com/npm/lockfile/blob/master/lockfile.js#L33-L37
Thanks for reply, but when I taskkill the process, the unlock didnt work. Maybe I misunderstood the use case of file lock?
I want to lock a user file after user login, and make sure the lock removed when the same process quit (including taskkill, etc)
The process.on('exit')
event doesn't fire if the process is killed by a unix signal.
You can use the module signal-exit, and add this:
var signalExit = require('signal-exit')
signalExit(function (code, signal) {
if (signal)
process.emit('exit')
})
var lockfile = require('lockfile')
// do whatever with lock files
Doing this requires that the program add os-level listeners to every possible fatal signal, and do some other hoop-jumping to make sure it doesn't accidentally prevent the exit when the signal should be fatal. That's what the signal-exit
module does, but it would be excessive to do that in this module.
Thanks! But I guess some of these signals are inavailable in Windows, :/ as well as killing the process from task monitor?
So that when the node process quits, the locked file will be removed.