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.
Add a check in the task runner to wait until tasks running in any dep…
…ended upon packages are complete 🐿 v2.10.0
- Loading branch information
1 parent
de307da
commit dc17d5a
Showing
3 changed files
with
53 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
const Semaphore = require('async-sema'); | ||
const logger = require('./logger'); | ||
const Sema = require('async-sema'); | ||
const waitUntil = require('./wait-until'); | ||
|
||
module.exports = (tasks = [], concurrency = 1) => { | ||
const sema = new Sema(concurrency); | ||
const noRunningDependencies = (running, dependencies) => { | ||
return !dependencies.some((dependency) => running.has(dependency)); | ||
}; | ||
|
||
module.exports = (packages = [], tasks = [], concurrency = 1) => { | ||
const semaphore = new Semaphore(concurrency); | ||
|
||
logger.info(`Executing up to ${concurrency} tasks at a time`); | ||
|
||
const packagesRunning = new Set(); | ||
|
||
return Promise.all( | ||
tasks.map((task) => { | ||
return sema.acquire().then(task).then(() => sema.release()); | ||
tasks.map((task, i) => { | ||
// TODO: number of tasks ~= number of packages | ||
const packageName = packages[i].name; | ||
const packageManifest = packages[i].manifest; | ||
|
||
const allDependencies = Object.keys({ | ||
...packageManifest.dependencies, | ||
...packageManifest.devDependencies, | ||
...packageManifest.peerDependencies, | ||
...packageManifest.optionalDependencies | ||
}); | ||
|
||
return semaphore | ||
.acquire() | ||
.then(() => { | ||
return waitUntil(() => { | ||
return noRunningDependencies(packagesRunning, allDependencies); | ||
}); | ||
}) | ||
.then(() => { | ||
packagesRunning.add(packageName); | ||
return task(); | ||
}) | ||
.then(() => { | ||
packagesRunning.delete(packageName); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const wait = (ms = 100) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
module.exports = (check) => { | ||
return new Promise((resolve) => { | ||
const test = () => { | ||
if (check()) { | ||
resolve(); | ||
} else { | ||
wait().then(test); | ||
} | ||
}; | ||
|
||
test(); | ||
}); | ||
}; |