Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When a number is passed as concurrent parameter, execute the tasks in… #56

Merged
merged 5 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const pMap = require('p-map');
const Task = require('./lib/task');
const TaskWrapper = require('./lib/task-wrapper');
const renderer = require('./lib/renderer');
Expand Down Expand Up @@ -31,6 +32,14 @@ class Listr {
nonTTYRenderer: 'verbose'
}, opts);
this._tasks = [];

this.concurrency = 1;
if (this._options.concurrent === true) {
this.concurrency = Infinity;
} else if (typeof this._options.concurrent === 'number') {
this.concurrency = this._options.concurrent;
}

this._RendererClass = renderer.getRenderer(this._options.renderer, this._options.nonTTYRenderer);

this.exitOnError = this._options.exitOnError;
Expand Down Expand Up @@ -79,16 +88,10 @@ class Listr {

this._checkAll(context);

let tasks;
if (this._options.concurrent === true) {
tasks = Promise.all(this._tasks.map(task => runTask(task, context, errors)));
} else {
tasks = this._tasks.reduce((promise, task) => promise.then(() => {
this._checkAll(context);

return runTask(task, context, errors);
}), Promise.resolve());
}
const tasks = pMap(this._tasks, task => {
this._checkAll(context);
return runTask(task, context, errors);
}, {concurrency: this.concurrency});

return tasks
.then(() => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"log-symbols": "^1.0.2",
"log-update": "^1.0.2",
"ora": "^0.2.3",
"p-map": "^1.1.1",
"rxjs": "^5.0.0-beta.11",
"stream-to-observable": "^0.1.0",
"strip-ansi": "^3.0.1"
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ Any renderer specific options.

##### concurrent

Type: `boolean`<br>
Type: `boolean` `number`<br>
Default: `false`

Set to `true` if you want tasks to run concurrently.
Set to `true` if you want to run tasks in parallel, set to a number to control the concurrency. By default it runs tasks sequentially.

##### exitOnError

Expand Down
86 changes: 86 additions & 0 deletions test/concurrent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {serial as test} from 'ava';
import delay from 'delay';
import Listr from '../';
import SimpleRenderer from './fixtures/simple-renderer';
import {testOutput} from './fixtures/utils';

const tasks = [
{
title: 'P1',
task: () => delay(100)
},
{
title: 'P2',
task: () => delay(200)
},
{
title: 'P3',
task: () => delay(300)
},
{
title: 'P4',
task: () => delay(400)
}
];

test('run tasks sequentially if concurrency is undefined', async t => {
const list = new Listr(tasks, {
renderer: SimpleRenderer
});

testOutput(t, [
'P1 [started]',
'P1 [completed]',
'P2 [started]',
'P2 [completed]',
'P3 [started]',
'P3 [completed]',
'P4 [started]',
'P4 [completed]',
'done'
]);

await list.run();
});

test('run tasks in parallel if concurrency is true', async t => {
const list = new Listr(tasks, {
renderer: SimpleRenderer,
concurrent: true
});

testOutput(t, [
'P1 [started]',
'P2 [started]',
'P3 [started]',
'P4 [started]',
'P1 [completed]',
'P2 [completed]',
'P3 [completed]',
'P4 [completed]',
'done'
]);

await list.run();
});

test('run tasks in sequential parallel chunks, if concurrency is a number', async t => {
const list = new Listr(tasks, {
renderer: SimpleRenderer,
concurrent: 2
});

testOutput(t, [
'P1 [started]',
'P2 [started]',
'P1 [completed]',
'P3 [started]',
'P2 [completed]',
'P4 [started]',
'P3 [completed]',
'P4 [completed]',
'done'
]);

await list.run();
});