forked from baudehlo/node-fs-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
56 lines (47 loc) · 1.24 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use strict";
var fs = require('./');
/**
* Function that locks the current file
* for `timeout` miliseconds
*
* The `counter` represents an index of how many
* times the function has been called so far
* (it acts as an id)
*
* The callback `cb` is optional
*/
function getCurrentFileSize(counter, timeout, cb) {
var fd = fs.openSync(__filename, 'r');
console.log("Trying to aquire lock for the %s time", counter);
fs.flock(fd, 'exnb', function(err) {
if (err) {
return console.log("Couldn't lock file", counter);
}
console.log('Aquired lock', counter);
// unlock after `timeout`
setTimeout(function() {
fs.flock(fd, 'un', function(err) {
if (err) {
return console.log("Couldn't unlock file", counter);
}
if (cb) { cb(); }
});
}, timeout);
});
}
getCurrentFileSize(1, 300, function() {
// this will succeed because we're calling the function
// after unlock
getCurrentFileSize(3, 2000);
});
// this will fail because #1 locks the file first
getCurrentFileSize(2, 1000);
// The output should be:
/*
Trying to aquire lock for the 1 time
Trying to aquire lock for the 2 time
Aquired lock 1
Couldn't lock file 2
Trying to aquire lock for the 3 time
Aquired lock 3
*/