Code coverage report for lib/worker.js

Statements: 100% (27 / 27)      Branches: 100% (6 / 6)      Functions: 100% (7 / 7)      Lines: 100% (27 / 27)      Ignored: none     

All files » lib/ » worker.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 671                 1 5                           1   4           4 4   1 6 2 2 2     4 2 2 2     4 4 4 4 3           4 1   4 4 4     1
var fs = require('fs'),
  functions = require('./functions'),
  jazz = require('jazz');
 
/**
 * class Worker
 *
 * @param {Number} workerId: an ID unique to this worker
 */
function Worker(workerId) {
  this.workerId = workerId;
}
 
/**
 * Write a data file consisting of header, segment x numSegments, and footer templates.
 * File is being streamed so it can handle large content.
 * Thanks to Max Ogden's fs stream backpressure example https://gist.github.com/2516455
 *
 * @param {Object} templates: data file templates in the format of { header: '', segment: '', footer: '' }
 * @param {Number} genId: an ID unique to the current data generation, used by all worker processes
 * @param {Number} numSegments: how many segments in a data file
 * @param {String} outFile: the data file name, to be postfixed with worker ID
 * @param {Function} cb: standard cb(err, result) callback
 */
Worker.prototype.write = function (templates, genId, numSegments, outFile, cb) {
 
  var stream = fs.createWriteStream(outFile + this.workerId, { flags: 'w', encoding: 'utf-8' }),
    segmentId = 0,
    segmentTemplate = jazz.compile(templates.segment),
    params = functions,
    status;
 
  params.gen_id = genId;
  params.worker_id = this.workerId;
 
  function write() {
    if (segmentId === numSegments) {
      var footerTemplate = jazz.compile(templates.footer);
      footerTemplate.process(params, function (data) {
        stream.end(data);
      });
    } else {
      if (segmentId === 0) {
        var headerTemplate = jazz.compile(templates.header);
        headerTemplate.process(params, function (data) {
          stream.write(data);
        });
      }
      params.segment_id = ++segmentId;
      segmentTemplate.process(params, function (data) {
        status = stream.write(data);
        if (status) {
          setImmediate(write);
        }
      });
    }
  }
 
  stream.on('error', function (err) {
    console.error('Error: %s', err.message);
  });
  stream.on('close', cb);
  stream.on('open', write);
  stream.on('drain', write);
};
 
module.exports = Worker;