This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Financial-Times/matth/depends-on
Add a check in the task runner to wait for dependents to finish
- Loading branch information
Showing
21 changed files
with
248 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const EventEmitter = require('events'); | ||
|
||
class EventedQueue extends EventEmitter { | ||
constructor() { | ||
super(); | ||
this.queue = new Set(); | ||
} | ||
|
||
add(item) { | ||
this.queue.add(item); | ||
this.emit('add', item); | ||
return this; | ||
} | ||
|
||
delete(item) { | ||
this.queue.delete(item); | ||
this.emit('delete', item); | ||
return this; | ||
} | ||
|
||
waitFor(items = []) { | ||
return new Promise((resolve) => { | ||
const callback = () => { | ||
const itemsRunning = items.some((item) => this.queue.has(item)); | ||
|
||
if (!itemsRunning) { | ||
this.removeListener('delete', callback); | ||
resolve(); | ||
} | ||
}; | ||
|
||
this.on('delete', callback); | ||
|
||
callback(null); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = EventedQueue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,30 @@ | ||
const Semaphore = require('async-sema'); | ||
const logger = require('./logger'); | ||
const Sema = require('async-sema'); | ||
const EventedQueue = require('./evented-queue'); | ||
|
||
module.exports = (tasks = [], concurrency = 1) => { | ||
const sema = new Sema(concurrency); | ||
module.exports = (tasks = [], concurrency = 1, preserveOrder = false) => { | ||
const semaphore = new Semaphore(concurrency); | ||
const queue = new EventedQueue(); | ||
|
||
logger.info(`Executing up to ${concurrency} tasks at a time`); | ||
|
||
return Promise.all( | ||
tasks.map((task) => { | ||
return sema.acquire().then(task).then(() => sema.release()); | ||
tasks.map(({ pkg, apply }) => { | ||
queue.add(pkg.name); | ||
|
||
return semaphore | ||
.acquire() | ||
.then(() => { | ||
// wait for any dependencies still in the queue to finish | ||
return preserveOrder ? queue.waitFor(pkg.allDependencies) : null; | ||
}) | ||
.then(() => { | ||
return apply(); | ||
}) | ||
.then(() => { | ||
queue.delete(pkg.name); | ||
return semaphore.release(); | ||
}); | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const Package = require('../../src/package'); | ||
|
||
module.exports = (name, options = {}) => { | ||
const manifest = { name, ...options }; | ||
const instance = new Package(manifest, `/Path/to/${name}`); | ||
|
||
instance.writeManifest = jest.fn(); | ||
|
||
return instance; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const Subject = require('../../src/evented-queue'); | ||
|
||
describe('src/evented-queue', () => { | ||
let instance; | ||
|
||
beforeEach(() => { | ||
instance = new Subject(); | ||
}); | ||
|
||
describe('#add', () => { | ||
it('adds the given item to the queue', () => { | ||
instance.add('foo'); | ||
expect(instance.queue.size).toEqual(1); | ||
}); | ||
|
||
it('emits an event when items are added to the queue', (done) => { | ||
instance.on('add', () => { | ||
done(); | ||
}); | ||
|
||
instance.add('foo'); | ||
}); | ||
}); | ||
|
||
describe('#delete', () => { | ||
it('removes the given item to the queue', () => { | ||
instance.add('foo'); | ||
expect(instance.queue.size).toEqual(1); | ||
|
||
instance.delete('foo'); | ||
expect(instance.queue.size).toEqual(0); | ||
}); | ||
|
||
it('emits an event when items are removed from the queue', (done) => { | ||
instance.add('foo'); | ||
|
||
instance.on('delete', () => { | ||
done(); | ||
}); | ||
|
||
instance.delete('foo'); | ||
}); | ||
}); | ||
|
||
describe('#waitFor', () => { | ||
it('resolves when the queue no longer contains any of the given items', (done) => { | ||
instance | ||
.add('foo') | ||
.add('bar') | ||
.add('baz'); | ||
|
||
instance.waitFor(['foo', 'bar', 'baz']).then(() => { | ||
expect(instance.queue.size).toEqual(0); | ||
done(); | ||
}); | ||
|
||
instance | ||
.delete('foo') | ||
.delete('bar') | ||
.delete('baz'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.