# Code coverage report for csv2json/libs/core/workerMgr.js

# All files / csv2json/libs/core workerMgr.js

0% Statements 0/79
0% Branches 0/18
0% Functions 0/18
0% Lines 0/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152

     module.exports = workerMgr;
    var eom = "\x03";
    var eom1 = "\x0e";
    var eom2 = "\x0f";
    var CSVError = require('./CSVError');
    function workerMgr() {
      var spawn = require("child_process").spawn;
      var exports = {
        initWorker: initWorker,
        sendWorker: sendWorker,
        setParams: setParams,
        drain: function(){},
        isRunning: isRunning,
        destroyWorker: destroyWorker
      };
      var workers = [];
      var running = 0;
      var waiting = null;
      function initWorker(num, params) {
        workers = [];
        running = 0;
        waiting = null;
        for (var i = 0; i < num; i++) {
          workers.push(new Worker(params));
        }

      }
      function isRunning() {
        return running > 0;
      }
      function destroyWorker() {
        workers.forEach(function(w) {
          w.destroy();
        });
      }

      function sendWorker(data, startIdx, transformCb, cbResult) {
        if (workers.length > 0) {
          var worker = workers.shift();
          running++;
          worker.parse(data, startIdx, function(result) {
            // var arr=JSON.parse(result);
            // arr.forEach(function(item){
            //   console.log('idx',item.index)
            // })
            workers.push(worker);
            cbResult(result, startIdx);
            running--;
            if (waiting === null && running === 0) {
              exports.drain();
            } else if (waiting) {
              sendWorker.apply(this, waiting);
              waiting = null;
            }
          });
          process.nextTick(transformCb);
        } else {
          waiting = [data, startIdx, transformCb, cbResult];
        }
      }

      function setParams(params) {
        workers.forEach(function(w) {
          w.setParams(params);
        });
      }
      return exports;
    }

    function Worker(params) {
      var spawn = require("child_process").spawn;
      this.cp = spawn(process.execPath, [__dirname + "/worker.js"], {
        env: {
          child:true
        },
        stdio:['pipe', 'pipe', 2, 'ipc']
        // stdio:[0,1,2,'ipc']
      });
      this.setParams(params);
      this.cp.on("message", this.onChildMsg.bind(this));
      this.buffer = "";
      var self = this;
      this.cp.stdout.on("data", function(d) {
        var str = d.toString("utf8");
        var all = self.buffer + str;
        var cmdArr = all.split(eom);
        while (cmdArr.length > 1) {
          self.onChildMsg(cmdArr.shift());
        }
        self.buffer = cmdArr[0];
      });
    }

    Worker.prototype.setParams = function(params) {
      var msg = "0" + JSON.stringify(params);
      this.sendMsg(msg);
    };

    /**
     * msg is like:
     * <cmd><data>
     * cmd is from 0-9
     */
    Worker.prototype.onChildMsg = function(msg) {
      if (msg) {
        var cmd = msg[0];
        var data = msg.substr(1);
        switch (cmd) {
          case "0": //total line number of current chunk
            if (this.cbLine) {
              var sp = data.split("|");
              var len = parseInt(sp[0]);
              var partial = sp[1];
              this.cbLine(len, partial);
            }
            break;
          case "1": // json array of current chunk
            if (this.cbResult) {
              var rows = data.split(eom1);
              rows.pop();
              var res = [];
              rows.forEach(function(row) {
                var sp = row.split(eom2);
                res.push({
                  index: sp[0],
                  row: sp[1],
                  err: sp[2] ? CSVError.fromArray(JSON.parse(sp[2])) : null,
                  json: sp[3]
                });
              });
              this.cbResult(res);
            }
            break;
        }
      }
    };

    Worker.prototype.parse = function(data, startIdx, cbResult) {
      this.cbResult = cbResult;
      var msg = "1" + startIdx + "|" + data;
      this.sendMsg(msg);
    };

    Worker.prototype.destroy = function() {
      this.cp.kill();
    };

    Worker.prototype.sendMsg = function(msg) {
      this.cp.stdin.write(msg + eom, "utf8");
      // this.cp.send(msg)
    };