Guest User

Untitled

a guest
Jul 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. var events = require('events'),
  2. sys = require('sys');
  3.  
  4. // constructor for job queue
  5. var JobQueue = function(opts) {
  6. var that = this;
  7.  
  8. this.opts = opts || {}
  9. this.jobs = [];
  10. this.queue = [];
  11. this.active = [];
  12.  
  13. this.limit = 10;
  14.  
  15. process.on('SIGINT', function() {
  16. that.stop();
  17. });
  18.  
  19. this.start();
  20. }
  21.  
  22. // start the timer to spawn tasks.
  23. JobQueue.prototype.start = function() {
  24. var that = this;
  25. this._interval = this._interval || setInterval(function() { that.spawn(); }, 1000, [this]);
  26. }
  27.  
  28. // remove the timer from the queue to check for more items.
  29. JobQueue.prototype.stop = function() {
  30. if (this._interval !== undefined) {
  31. clearInterval(this._interval);
  32. delete this._interval;
  33. }
  34. }
  35.  
  36. // test to see if more jobs can be started
  37. // could be overridden if needed.
  38. JobQueue.prototype.spawnMore = function() {
  39. var count = 0;
  40.  
  41. this.active.forEach(function(el) { count++ });
  42.  
  43. if (count >= this.limit) {
  44. return false;
  45. }
  46. else {
  47. return true;
  48. }
  49. }
  50.  
  51. // callback that spawns the individual jobs
  52. JobQueue.prototype.spawn = function() {
  53. if (this.spawnMore()) {
  54. var next_task = this.queue.pop();
  55.  
  56. if (next_task !== undefined) {
  57. this.active.push(next_task);
  58. this.startJob(this.jobs[next_task]);
  59. }
  60. }
  61.  
  62. }
  63.  
  64. // start a job's processing.
  65. JobQueue.prototype.startJob = function(job) {
  66.  
  67. var that = this;
  68. // set a status somewhere
  69. job.emit('start', job);
  70.  
  71. // set up a listener on the job to remove it from this queue.
  72. job.on('finish', function() { that.remove(job); });
  73.  
  74. // if there are no work listener, just emit finish.
  75. if (job.listeners('work').length === 0) {
  76. job.emit('finish');
  77. }
  78. }
  79.  
  80. // add a job to the queue
  81. JobQueue.prototype.add = function(job) {
  82. // reset the length property on the jobs array.
  83. this.jobs = this.jobs.some(function() { return true; }) ? this.jobs : [];
  84.  
  85. // add the job to the end of the queue
  86. job._index = this.jobs.push(job) - 1;
  87.  
  88. // add it to the queue array
  89. this.queue.push(job._index);
  90.  
  91. return job;
  92. }
  93.  
  94. // remove a job from the queue
  95. JobQueue.prototype.remove = function(job) {
  96. var idx;
  97. idx = this.active.indexOf(job._index);
  98. if (idx !== -1) {
  99. delete this.active[idx];
  100. }
  101.  
  102. idx = this.queue.indexOf(job._index);
  103. if (idx !== -1) {
  104. delete this.queue[idx];
  105. }
  106.  
  107.  
  108. delete this.jobs[job._index];
  109. }
  110.  
  111. // constructor for the jobs
  112. var Job = function(opts) {
  113. events.EventEmitter.call(this);
  114.  
  115. var opts = opts || {}
  116.  
  117. if (opts.start !== undefined) {
  118. this.on('start', opts.start);
  119. }
  120.  
  121. if (opts.work !== undefined) {
  122. this.on('work', opts.work);
  123. }
  124.  
  125. if (opts.finish !== undefined) {
  126. this.on('finish', opts.finish);
  127. }
  128. }
  129.  
  130. sys.inherits(Job, events.EventEmitter);
  131.  
  132.  
  133. // create a new job queue
  134. var jq = new JobQueue();
  135.  
  136. // Create a new job object
  137. var job = new Job();
  138.  
  139. job.on('start', function() {
  140. var that = this;
  141. console.log("Start job");
  142. // start some long running task
  143. setTimeout( function() {
  144. that.emit('work');
  145. }, 1000);
  146. });
  147.  
  148. job.on('work', function() {
  149. console.log("Job work unit");
  150. var that = this;
  151.  
  152. setTimeout( function() {
  153. that.emit('finish');
  154. }, 1000);
  155. });
  156.  
  157. job.on('finish', function() {
  158. console.log('Job finishes');
  159. });
  160.  
  161.  
  162. // add a job to the queue.
  163. jq.add(job); // will start it
Add Comment
Please, Sign In to add comment