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
Add a check in the task runner to wait for dependents to finish #20
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bda851b
Add a check in the task runner to wait until tasks running in any dep…
i-like-robots a9be892
Refactor tasks to return package instance with callback function
i-like-robots 8941bb1
Refactor logic to fetch all dependencies into package class
i-like-robots 64c637b
Refactor specs to use shared package helper
i-like-robots cda9d8d
Refactor queue logic from a poller (wait until...) to use an event em…
i-like-robots f74bb55
Add a spec for evented queue module
i-like-robots c096ff5
Ensure Node 8 support by switching from eventemitter.off() to eventem…
i-like-robots f94c7e9
Add --preserve-order flag and refactor queue to only enable waiting w…
i-like-robots File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => { | ||
i-like-robots marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) => { | ||
i-like-robots marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be an upper-limit to the number of tasks we can run concurrently? 🤔