进程操作

暴露 RPC 方法:进程操作

暴露 RPC 方法允许您与正在运行的进程进行实时交互。

这对于以下情况很有用

  • 更改行为(例如,将日志切换到调试模式)
  • 检索数据结构
  • 触发操作

快速入门

首先安装 tx2 模块

$ npm install tx2

然后创建一个名为 rpc.js 的应用程序

const tx2 = require('tx2')

tx2.action('hello', (reply) => {
  reply({ answer : 'world' })
})

setInterval(function() {
  // Keep application online
}, 100)

并使用 PM2 启动它

$ pm2 start rpc.js

现在要触发进程操作,请使用以下命令

$ pm2 trigger <application-name> <action-name>
# pm2 trigger rpc hello

列出可用的 RPC 方法

要列出所有可用的 RPC 方法

pm2 show <application-name>
# pm2 show rpc

传递参数

要将参数传递给远程函数,只需将 param 属性添加到回调函数中

var tx2 = require('tx2')

tx2.action('world', function(param, reply) {
  console.log(param)
  reply({success : param})
})

重启您的应用程序并使用 PM2 调用此进程函数

pm2 trigger <application-name> <action-name> [parameter]
# pm2 trigger rpc world somedata

从 Web 仪表板触发

所有从您的应用程序暴露的 RPC 方法,一旦连接到 pm2.io,都将显示在 Web 界面上,并且可以操作。

TX2 API 文档

https://github.com/pm2/tx2/blob/main/API.md

为此页面做出贡献