nklayman / vue-cli-plugin-electron-builder

Easily Build Your Vue.js App For Desktop With Electron
https://nklayman.github.io/vue-cli-plugin-electron-builder/
MIT License
4.12k stars 280 forks source link

Error of creating Multiple Instances of Windows in MacOS after building #919

Open edcamcam opened 4 years ago

edcamcam commented 4 years ago

Describe the bug I am building an vue-cli-electron app that uses pm2 and nedb. it is mainly to use pm2 to start external .js script provided the path, and also nedb to store the saved configurations. The code works fine when I use electron:serve, but when i use electron:build, the app will work fine for a while, but after restarting the pc, the app will no longer work (including the external modules), and will repeated open multiple instances of the app.

To Reproduce Steps to reproduce the behavior: In vue.config.js,

  pluginOptions: {
      electronBuilder: {
        preload: 'src/preload.js',
        externals: ['pm2','nedb'],
        builderOptions: {
          extraResources: ['src/serverconfig.db']
        },
      }
  }

In background.js ,

const Datastore = require('nedb')
const pm2 = require('pm2')
...
const isBuild = process.env.NODE_ENV === 'production'
const pathToDbFile = path.join(
  isBuild ? __dirname : __static,
  '../src/serverconfig.db',
);

let db = new Datastore({ filename: pathToDbFile });
db.persistence.setAutocompactionInterval(5000)

...
ipcMain.on('start', (event, arg) => {
  console.log(arg)

  pm2.start({
      name: arg[0],
      cwd: path.dirname(arg[2]),
      script: arg[1],
      logDateFormat: 'YYYY-MM-DD HH:mm Z',
      interpreter:(process.platform !== 'darwin')?'node':'/usr/local/bin/node'
    }, (e, r) => {
        if (e) {
          const message = arg[0] + ' server failed to start!'
          console.log (message)
          win.webContents.send('ipcMessage',message)
        } else {
          const message = arg[0] + ' server started'
          console.log (message)
          win.webContents.send('ipcMessage',message)
        }
      })
})

ipcMain.on('saveDb', async (event, arg) => {

  console.log(arg)
  db.loadDatabase()
  var doc = { name:arg[0],
              cwd: path.dirname(arg[2]),
              script: arg[1],
              logDateFormat: 'YYYY-MM-DD HH:mm Z',
              interpreter:(process.platform !== 'darwin')?'node':'/usr/local/bin/node'
            }
  await db.insert(doc, function (err, newDoc) {
    const message = newDoc.name + ' config successfully saved'
    win.webContents.send('ipcMessage',message)
  });
  db.persistence.compactDatafile()

})

In setup.js, //where the script is saved and use pm2 to start

...
      const ipcArray=[appName,script,path]
      window.ipcRenderer.send('start',ipcArray)
      window.ipcRenderer.send('saveDb',ipcArray)
...

Environment (please complete the following information):

I have tried requestSingleInstanceLock of electron to solve the issue. although it can restrict the spawning of the multiple instances, but my app is non functional, both nedb and pm2 cant be used.

i checked on the pm2 log, and this is the problem (node:2085) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed. (node:2090) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed. (node:2096) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed. (node:2102) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed. (node:2109) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed. (node:2114) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed. (node:2119) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed.

I have run and build this app on windows and there is not a single problem. The problem only appears when i build it and run it on Mac

edcamcam commented 4 years ago

Additional information: After uninstalling, rebuild and reinstall the APP with the same name will surely reproduce the error. I have tried rebuilding from another dev folder and reinstalling under a different name, it might work, but after restarting the pc, the whole error will repeat once again