ascii-progress icon indicating copy to clipboard operation
ascii-progress copied to clipboard

Using multiple progressbars with node cluster

Open lrecknagel opened this issue 8 years ago • 0 comments
trafficstars

I really like this project and with multiple bars without use of cluster it works perfect.

But if I try to use it in clustermode i got weird. I think its related to the ProgressBar object references but not sure.

Has anyone an idea how this can be done?

I prepared a litte example to demonstrate the scenario:

runC() => does the progressbars for cluster mode which works not :/ runN() => does the progressbars without cluster mode which works

const cluster = require('cluster'),
  ProgressBar = require('ascii-progress');

const bars = {};

async function job(myNum) {
  const timer = setInterval( () => {
      bars[myNum].tick();
      if (bars[myNum].completed) {
        clearInterval(timer);
      }
    }, 150);
}

function createBars(data) {
  for (let i = 0; i < data.length; i++) {
    bars[data[i]] = new ProgressBar({
      schema: `${ data[i] } [:bar.gradient(green,magenta)] :percent`,
      total: data[i]
    });
  }
}

async function runC() {
  const data = [10,20,30];
  createBars(data);

  if (cluster.isMaster) {
    for (let i = 0; i < 3; i++) {
      cluster.fork({special_data: data.pop()});
    }
  } else {
    job(process.env.special_data);
  }
}

async function runN() {
  const data = [10,20,30];
  createBars(data);

  for (let i = 0; i < 3; i++) {
    job(data.pop());
  }
}

// runC();
// runN();

lrecknagel avatar Sep 01 '17 14:09 lrecknagel