Skip to content

Using multiple progressbars with node cluster #22

@lrecknagel

Description

@lrecknagel

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions