Unitech / Unitech/pm2

the types and behavior for sendDataToProcessId is confused

Open
#4,888 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

What's going wrong?

The type for sendDataToProcessId is function sendDataToProcessId(proc_id: number, packet: object, cb: ErrResultCallback): void. However, from the doc and the test code for sendDataToProcessId, we can use sendDataToProcessId as below:

pm2.sendDataToProcessId({
  id: proc1.pm2_env.pm_id,
  topic : 'process:msg',
  data : {
    some : 'data',
    hello : true
  }
}, function(err, res) {
});

So the typescript definition should add overloads support for sendDataToProcessId function.
And the behavior of these two function is also defferent, pm2.sendDataToProcessId(packet, cb) will send message to current process, pm2.sendDataToProcessId(id, packet, cb) won't. For example:

// we start this program use pm2 --no-daemon -i 2
function a() {
  pm2.connect(() => {
    pm2.list((err, list) => {
      for (const proc of list) {
        // change to any here, otherwise, it will result in typescript error
        (pm2 as any).sendDataToProcessId(
          {
            type: 'type',
            topic: 'topic',
            data: {
              hello: 'hello',
            },
            id: proc.pm_id,
          },
          function (error: unknown) {
            console.log(error);
            console.log('message sent');
          }
        );
      }
      pm2.disconnect();
    });
  });
}

process.on('message', function (packet) {
  if (packet.type === 'type') {
    console.log('emit message', packet.data, packet.type);
  }
});

// when run a() in process 1.
process 1: emit message
process 2: emit message

// when we start program in this one
function b() {
  pm2.connect(() => {
    pm2.list((err, list) => {
      for (const proc of list) {
         pm2.sendDataToProcessId(
            id: proc.pm_id,
          {
            type: 'type',
            topic: 'topic',
            data: {
              hello: 'hello',
            }
          },
          function (error: unknown) {
            console.log(error);
            console.log('message sent');
          }
        );
      }
      pm2.disconnect();
    });
  });
}

process.on('message', function (packet) {
  if (packet.type === 'type') {
    console.log('emit message', packet.data, packet.type); // it will only trigger twice.
  }
});

// when run a() in process 1.
process 1: // no console
process 2: emit message

How could we reproduce this issue?

As the example show above

Supporting information

pm2d version         : 4.5.0
node version         : 12.19.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the sendDataToProcessId entry point and its TypeScript declaration, then compare both call forms with the documented API and test/programmatic/send_data_process.mocha.js. Reproduce the differing process-target behavior described in the issue. Done means the supported signatures and runtime behavior are consistent and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.