Search code examples
node.jselectronipc

Electron - How to get process argv from all electron pids


I have an electron app with multiple browserWindows.
For my own help, I spawn them with additional arguments (for example: '--renderer-mode="second-window"').

Now I want to collect Metric Data of my current electron processes.
I already have a IPC interface in my main process I call from one of my renderer.

ipcMain.handle('app-metrics', (event, message) => {
    return new Promise((resolve) => {
        const appMetrics = app.getAppMetrics()
        resolve(appMetrics)
    })
})

Here I want to add all the argv from my apps processes.
I don't know how I could get the info in this function. I only know the way with process.argv, but how could I collect these info from all the sub processes and bundle it with my appMetrics array?


Solution

  • I solved my question in another way. My goal was to display the "process" type (not the chromium types that already exists in the metric data).

    I'm collecting the PIDs I already know and hardcode them a specific type. The next thing was to add this info into the metric object. Here is my result:

    ipcMain.handle('app-metrics', (event, message) => {
        return new Promise((resolve) => {
            const pids = [
                {
                    name: 'main-process',
                    pid: process.pid
                },
                {
                    name: 'app-gui',
                    pid: this.win.webContents.getOSProcessId()
                },
                {
                    name: 'popup-gui',
                    pid: this.winPopup.webContents.getOSProcessId()
                }
            ]
    
            const appMetrics = app.getAppMetrics().map((metric) => {
                const pidType = pids.filter((e) => e.pid === metric.pid)
                if (pidType.length > 0) {
                    return {
                        ...metric,
                        appType: pidType[0].name
                    }
                }
                return {
                    ...metric,
                    appType: ''
                }
            })
            resolve(appMetrics)
        })
    })
    

    If there is a simpler and smarter way, I'm happy to hear it. :)