mirror of
https://github.com/harish2704/node-job-manager.git
synced 2026-09-10 05:01:07 +00:00
setConcurency method: it is used to dynamically change concurreny. + test cases
This commit is contained in:
+15
-3
@@ -30,12 +30,24 @@ function JobManager(opts){
|
|||||||
|
|
||||||
JobManager.prototype.updateState = function(){
|
JobManager.prototype.updateState = function(){
|
||||||
var workers = this.workers;
|
var workers = this.workers;
|
||||||
var tmpPoolLen = workers.length && (workers.length * Math.ceil( this.concurrency/workers.length ) ) , i;
|
var tmpPoolBlock = workers.length && Math.ceil( this.concurrency/workers.length ) , i;
|
||||||
var tmpPool = [];
|
var tmpPool = [];
|
||||||
for( i=0; i < tmpPoolLen; i++){
|
for( i=0; i < tmpPoolBlock; i++){
|
||||||
tmpPool[i] = workers[ i % workers.length ];
|
tmpPool = tmpPool.concat( this.workers );
|
||||||
}
|
}
|
||||||
this.tmpPool = tmpPool;
|
this.tmpPool = tmpPool;
|
||||||
|
this.tmpPoolLen = tmpPoolBlock * this.workers.length ;
|
||||||
|
};
|
||||||
|
|
||||||
|
JobManager.prototype.setConcurrency = function( newVal ){
|
||||||
|
if( newVal > this.tmpPoolLen ){
|
||||||
|
var tmpPool = this.tmpPool;
|
||||||
|
var additionalPoolBlocks = Math.ceil( ( newVal - this.tmpPoolLen )/this.workers.length ), i;
|
||||||
|
for( i=0; i < additionalPoolBlocks; i++){
|
||||||
|
tmpPool = tmpPool.concat( this.workers );
|
||||||
|
}
|
||||||
|
this.tmpPoolLen += additionalPoolBlocks * this.workers.length ;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -72,5 +72,20 @@ describe('JobManager', function(){
|
|||||||
jm.runningTasks.should.be.equal( 0 );
|
jm.runningTasks.should.be.equal( 0 );
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should dynamically change concurrency', function( done ){
|
||||||
|
jm.concurrency = NO_WORKERS-1;
|
||||||
|
jm.updateState();
|
||||||
|
jm.tmpPoolLen.should.be.equal( NO_WORKERS );
|
||||||
|
jm.setConcurrency( (2*NO_WORKERS) +1 );
|
||||||
|
jm.tmpPoolLen.should.be.equal( 3*NO_WORKERS );
|
||||||
|
jm.setConcurrency( (3*NO_WORKERS) +1 );
|
||||||
|
jm.tmpPoolLen.should.be.equal( 4*NO_WORKERS );
|
||||||
|
jm.setConcurrency( (4*NO_WORKERS) +1 );
|
||||||
|
jm.tmpPoolLen.should.be.equal( 5*NO_WORKERS );
|
||||||
|
jm.setConcurrency( (5*NO_WORKERS) +1 );
|
||||||
|
jm.tmpPoolLen.should.be.equal( 6*NO_WORKERS );
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user