Skip to content

Commit f004033

Browse files
author
Caolan McMahon
committed
Merge pull request caolan#474 from maxkueng/pause-and-resume
queue.pause() and queue.resume()
2 parents 9d09fd1 + 4722e78 commit f004033

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,9 @@ methods:
10881088
and further tasks will be queued.
10891089
* `empty` - a callback that is called when the last item from the `queue` is given to a `worker`.
10901090
* `drain` - a callback that is called when the last item from the `queue` has returned from the `worker`.
1091+
* `paused` - a boolean for determining whether the queue is in a paused state
1092+
* `pause()` - a function that pauses the processing of tasks until `resume()` is called.
1093+
* `resume()` - a function that resumes the processing of queued tasks when the queue is paused.
10911094

10921095
__Example__
10931096

lib/async.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,14 +725,15 @@
725725
saturated: null,
726726
empty: null,
727727
drain: null,
728+
paused: false,
728729
push: function (data, callback) {
729730
_insert(q, data, false, callback);
730731
},
731732
unshift: function (data, callback) {
732733
_insert(q, data, true, callback);
733734
},
734735
process: function () {
735-
if (workers < q.concurrency && q.tasks.length) {
736+
if (!q.paused && workers < q.concurrency && q.tasks.length) {
736737
var task = q.tasks.shift();
737738
if (q.empty && q.tasks.length === 0) {
738739
q.empty();
@@ -760,6 +761,16 @@
760761
},
761762
idle: function() {
762763
return q.tasks.length + workers === 0;
764+
},
765+
pause: function () {
766+
if (q.paused === true) { return; }
767+
q.paused = true;
768+
q.process();
769+
},
770+
resume: function () {
771+
if (q.paused === false) { return; }
772+
q.paused = false;
773+
q.process();
763774
}
764775
};
765776
return q;

test/test-async.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,57 @@ exports['queue idle'] = function(test) {
23002300
}
23012301
}
23022302

2303+
exports['queue pause'] = function(test) {
2304+
var call_order = [],
2305+
task_timeout = 100,
2306+
pause_timeout = 300,
2307+
resume_timeout = 500,
2308+
tasks = [ 1, 2, 3, 4, 5, 6 ],
2309+
2310+
elapsed = (function () {
2311+
var start = +Date.now();
2312+
return function () { return Math.floor((+Date.now() - start) / 100) * 100; };
2313+
})();
2314+
2315+
var q = async.queue(function (task, callback) {
2316+
call_order.push('process ' + task);
2317+
call_order.push('timeout ' + elapsed());
2318+
callback();
2319+
});
2320+
2321+
function pushTask () {
2322+
var task = tasks.shift();
2323+
if (!task) { return; }
2324+
setTimeout(function () {
2325+
q.push(task);
2326+
pushTask();
2327+
}, task_timeout);
2328+
}
2329+
pushTask();
2330+
2331+
setTimeout(function () {
2332+
q.pause();
2333+
test.equal(q.paused, true);
2334+
}, pause_timeout);
2335+
2336+
setTimeout(function () {
2337+
q.resume();
2338+
test.equal(q.paused, false);
2339+
}, resume_timeout);
2340+
2341+
setTimeout(function () {
2342+
test.same(call_order, [
2343+
'process 1', 'timeout 100',
2344+
'process 2', 'timeout 200',
2345+
'process 3', 'timeout 500',
2346+
'process 4', 'timeout 500',
2347+
'process 5', 'timeout 500',
2348+
'process 6', 'timeout 600'
2349+
]);
2350+
test.done();
2351+
}, 800);
2352+
}
2353+
23032354
exports['cargo'] = function (test) {
23042355
var call_order = [],
23052356
delays = [160, 160, 80];

0 commit comments

Comments
 (0)