Comparing version 1.1.1 to 1.2.0
## File API | ||
#### async File.append(path) | ||
#### async File.append(path, data) | ||
@@ -9,2 +9,8 @@ ```js | ||
#### async File.appendJSON(path, data ={}) | ||
```js | ||
await File.append('myfile.json', {name:'Alex'}); | ||
``` | ||
#### async File.create(path, [data='']) | ||
@@ -11,0 +17,0 @@ |
{ | ||
"name": "fslockjs", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Easy to use file system queue with locking and events. Provide Asynchronous utilities for Directories and File", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,3 +34,4 @@ const fs = require('fs'); | ||
} catch (err) { | ||
console.error(err); | ||
reject(err); | ||
throw err; | ||
} | ||
@@ -37,0 +38,0 @@ })).then(() => { |
const File = {}; | ||
File.append = require('./methods/append').bind(File); | ||
File.appendJSON = require('./methods/appendJSON').bind(File); | ||
File.create = require('./methods/create').bind(File); | ||
@@ -4,0 +5,0 @@ File.download = require('./methods/download').bind(File); |
@@ -40,4 +40,5 @@ const fs = require('fs'); | ||
} catch (e) { | ||
console.error('CREATE', p, 'error'); | ||
console.error(e); | ||
rej(e); | ||
throw e; | ||
} | ||
@@ -44,0 +45,0 @@ } else write(res); |
const Job = require('../../Job/Job'); | ||
module.exports = function add(command, path, params){ | ||
const job = new Job({command, path, params}); | ||
this.queue.push(job); | ||
job.state = 'queued'; | ||
return job; | ||
module.exports = function add(command, path, params){ | ||
const job = new Job({command, path, params}); | ||
this.queue.push(job); | ||
job.state = 'queued'; | ||
return job; | ||
} |
const File = require('../../../File/File'); | ||
const Directory = require('../../../Directory/Directory'); | ||
const utils = {File, Directory} | ||
module.exports = async function execCommand(command, path, params){ | ||
switch (command) { | ||
case "Directory.create": | ||
return Directory.create(path); | ||
case "Directory.exists": | ||
return Directory.exists(path); | ||
case "File.create": | ||
return File.create(path, params); | ||
case "File.read": | ||
return File.read(path); | ||
default: | ||
throw new Error(`execCommand - Unsupported command ${command}`); | ||
let result; | ||
try{ | ||
const [type,fn] = command.split('.'); | ||
result = await utils[type][fn](path, params); | ||
} catch(err){ | ||
result = err; | ||
} | ||
return result; | ||
} |
const {map} = require('lodash'); | ||
const next = async (self)=>{ | ||
if(self.options.autoexec && !self.autoExecStarted){ | ||
return; | ||
} | ||
await self.processNext() | ||
} | ||
const processQueue = async (self)=>{ | ||
if(self.queue.length>0){ | ||
await next(self); | ||
await processQueue(self); | ||
} | ||
} | ||
module.exports = async function processAll() { | ||
@@ -6,9 +20,4 @@ this.state = 'processingAll'; | ||
if (this.queue.length === 0) return; | ||
await Promise.all(map(this.queue, async () => { | ||
if(this.options.autoexec && !this.autoExecStarted){ | ||
return; | ||
} | ||
await self.processNext() | ||
})); | ||
await processQueue(self); | ||
this.state = 'idle'; | ||
}; |
@@ -8,46 +8,48 @@ const execCommand = require('./ops/execCommand'); | ||
module.exports = async function processNext(index=0, tries=0) { | ||
if(this.state!=='processingAll') this.state = 'processing'; | ||
if(!this.queue.length){ | ||
return false; | ||
} | ||
const job = (index===0) ? this.queue.shift() : this.queue.splice(index,1); | ||
const {command} = job; | ||
const self = this; | ||
return new Promise(async (resolve, reject) => { | ||
self.state = 'processing'; | ||
if(!self.queue.length){ | ||
return false; | ||
} | ||
const job = (index===0) ? self.queue.shift() : self.queue.splice(index,1)[0]; | ||
const {command} = job; | ||
const {path, params} = job; | ||
const {path, params} = job; | ||
// If there is a lock, we just try to process the next one | ||
if(this.locks[path]){ | ||
// We can't deal with it right now. let's replace the item | ||
this.queue.splice(index, 0, job); | ||
// If there is a lock, we just try to process the next one | ||
if(self.locks[path]===1){ | ||
// We can't deal with it right now. let's replace the item | ||
self.queue.splice(index, 0, job); | ||
if(this.queue.length>index+2){ | ||
return this.processNext(1); | ||
}else{ | ||
// It's locked. We have to wait. Let's retry in a few | ||
return await (new Promise(((resolve, reject) => { | ||
setTimeout(()=>{ | ||
return resolve(this.processNext(0, tries+=1)); | ||
}, 80) | ||
}))); | ||
if(self.queue.length>index+2){ | ||
return self.processNext(1); | ||
}else{ | ||
// It's locked. We have to wait. Let's retry in a few | ||
return await (new Promise(((resolve, reject) => { | ||
setTimeout(()=>{ | ||
return resolve(self.processNext(0, tries+=1)); | ||
}, 50) | ||
}))); | ||
} | ||
} | ||
} | ||
self.locks[path] = 1; | ||
this.locks[path] = 1; | ||
job.state = 'processing'; | ||
job.emit('processing'); | ||
job.state = 'processing'; | ||
job.emit('processing'); | ||
job.results = await execCommand(command,path, params); | ||
job.results = await execCommand(command,path, params); | ||
job.state = 'executed'; | ||
job.emit('executed'); | ||
job.state = 'executed'; | ||
job.emit('executed'); | ||
// FIXME : Actually, it works without this, but I saw cases where .exists was returning false | ||
// Keeping this except requested otherwise | ||
setTimeout(()=>{ | ||
delete this.locks[path]; | ||
}, 20); | ||
if(this.state!=='processingAll') this.state = 'idle'; | ||
return true; | ||
// FIXME : Actually, it works without this, but I saw cases where .exists was returning false | ||
// Keeping this except requested otherwise | ||
setTimeout(()=>{ | ||
delete self.locks[path] | ||
self.state = 'idle'; | ||
resolve(true); | ||
}, 20); | ||
}) | ||
}; |
@@ -8,7 +8,6 @@ const EventEmitter = require('events'); | ||
} | ||
this.command = props.command; | ||
this.path = props.path; | ||
this.params = props.params || null; | ||
this.state = null; | ||
this.state = 'idle'; | ||
this.results = null; | ||
@@ -15,0 +14,0 @@ } |
@@ -16,4 +16,4 @@ const {expect} = require('chai'); | ||
}); | ||
it('should create a command and return a job element', function () { | ||
const addedJob = queue.add('Directory.exists', usersPath); | ||
it('should create a command and return a job element', async function () { | ||
const addedJob = await queue.add('Directory.exists', usersPath); | ||
expect(addedJob.constructor.name).to.equal(Job.name); | ||
@@ -30,3 +30,3 @@ expect(addedJob.state).to.equal('queued'); | ||
it('should process a command and mutate job element', async function () { | ||
const job2 = queue.add('Directory.create', usersPath); | ||
const job2 = await queue.add('Directory.create', usersPath); | ||
@@ -41,3 +41,3 @@ await queue.processNext(); | ||
const job3 = queue.add('Directory.exists', usersPath); | ||
const job3 = await queue.add('Directory.exists', usersPath); | ||
await queue.processNext(); | ||
@@ -52,4 +52,4 @@ expect(job3.state).to.equal('executed'); | ||
const createJob = queue.add('File.create', `${usersPath}/${doc._id}.json`, Object.assign({}, {_id, email})); | ||
const readJob = queue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
const createJob = await queue.add('File.create', `${usersPath}/${doc._id}.json`, Object.assign({}, {_id, email})); | ||
const readJob = await queue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
@@ -65,6 +65,6 @@ // We process | ||
const updateJob = queue.add('File.create', `${usersPath}/${doc._id}.json`, Object.assign({}, storeDoc, {age})) | ||
const updateJob = await queue.add('File.create', `${usersPath}/${doc._id}.json`, Object.assign({}, storeDoc, {age})) | ||
await queue.processNext(); | ||
const verifyJob = queue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
const verifyJob = await queue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
await queue.processNext(); | ||
@@ -74,15 +74,17 @@ | ||
}); | ||
it('should have job emitting a event when ready', function (done) { | ||
it('should have job emitting a event when ready', async function () { | ||
const doc = {"_id": "5d6d4123117055fa0b17bb16", "email": "alex@valjean.fr", "age": 27} | ||
queue.add('File.create', `${usersPath}/${doc._id}.json`, doc) | ||
const readJob = queue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
console.log(readJob); | ||
readJob.on('executed', () => { | ||
expect(readJob.state).to.deep.equal('executed'); | ||
expect(readJob.results).to.deep.equal(doc); | ||
done(); | ||
await queue.add('File.create', `${usersPath}/${doc._id}.json`, doc) | ||
const readJob = await queue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
return new Promise(async (res, rej)=>{ | ||
readJob.on('executed', () => { | ||
expect(readJob.state).to.deep.equal('executed'); | ||
expect(readJob.results).to.deep.equal(doc); | ||
res(true); | ||
}) | ||
await queue.processAll(); | ||
}) | ||
queue.processAll(); | ||
@@ -94,4 +96,4 @@ }); | ||
autoQueue.add('File.create', `${usersPath}/${doc._id}.json`, doc) | ||
const readJob = autoQueue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
await autoQueue.add('File.create', `${usersPath}/${doc._id}.json`, doc) | ||
const readJob = await autoQueue.add('File.read', `${usersPath}/${doc._id}.json`); | ||
@@ -98,0 +100,0 @@ await readJob.execution(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33544
41
825