Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Add a check in the task runner to wait until tasks running in any dep…
Browse files Browse the repository at this point in the history
…ended upon packages are complete

 🐿 v2.10.0
  • Loading branch information
i-like-robots committed Dec 4, 2018
1 parent de307da commit dc17d5a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cli-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = (task) => {
logger.info(`Running ${tasks.length} tasks`);

// 6. execute all tasks
await runParallel(tasks, globals.concurrency);
await runParallel(sortedPackages, tasks, globals.concurrency);

timer.stop();

Expand Down
42 changes: 37 additions & 5 deletions src/run-parallel.js
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();
});
})
);
};
15 changes: 15 additions & 0 deletions src/wait-until.js
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();
});
};

0 comments on commit dc17d5a

Please sign in to comment.