{"action":"created","issue":{"url":"https://api.github.com/repos/Unitech/pm2/issues/2417","repository_url":"https://api.github.com/repos/Unitech/pm2","labels_url":"https://api.github.com/repos/Unitech/pm2/issues/2417/labels{/name}","comments_url":"https://api.github.com/repos/Unitech/pm2/issues/2417/comments","events_url":"https://api.github.com/repos/Unitech/pm2/issues/2417/events","html_url":"https://github.com/Unitech/pm2/issues/2417","id":180126791,"number":2417,"title":"call pm2.start in a for loop","user":{"login":"yuqinlear","id":2876055,"avatar_url":"https://avatars.githubusercontent.com/u/2876055?v=3","gravatar_id":"","url":"https://api.github.com/users/yuqinlear","html_url":"https://github.com/yuqinlear","followers_url":"https://api.github.com/users/yuqinlear/followers","following_url":"https://api.github.com/users/yuqinlear/following{/other_user}","gists_url":"https://api.github.com/users/yuqinlear/gists{/gist_id}","starred_url":"https://api.github.com/users/yuqinlear/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuqinlear/subscriptions","organizations_url":"https://api.github.com/users/yuqinlear/orgs","repos_url":"https://api.github.com/users/yuqinlear/repos","events_url":"https://api.github.com/users/yuqinlear/events{/privacy}","received_events_url":"https://api.github.com/users/yuqinlear/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-29T18:31:17Z","updated_at":"2016-09-30T15:45:39Z","closed_at":null,"body":"#### Expected behaviour\r\nBecause I need pass different arguments to different processes, I made a for loop to call pm2.start.\r\nThe problem is that I can not always create the expected number of processes.\r\n\r\n```\r\n'use strict';\r\nvar pm2 = require('pm2');\r\nvar NUMBER_OF_WORKERS = 10;\r\nvar PORT_START = 13001;\r\n\r\npm2.connect(function(err) {\r\n  if (err) {\r\n    console.error(err);\r\n    process.exit(2);\r\n  }\r\n\r\n  for(let i = 0; i < NUMBER_OF_WORKERS; i++) {\r\n    let proxyPort = PORT_START + i;\r\n    pm2.start(\r\n      {\r\n        name: 'worker-' + proxyPort,\r\n        script : 'src/foo.js',\r\n        exec_mode : 'fork',\r\n        force: true,\r\n        instances : 1,\r\n        args: [ proxyPort ]\r\n      },\r\n      function(err, apps) {\r\n        console.error('the error handler is called ----------------');\r\n        pm2.disconnect();\r\n        if (err) {\r\n          throw err;\r\n        }\r\n      });\r\n  }\r\n});\r\n```\r\n\r\n...\r\n\r\n#### Actual behaviour\r\nthe number of started processes is less than defined NUMBER_OF_WORKERS.\r\n\"the error handler is called ----------------\" will be printed out several times inconsistently.\r\n\r\n#### Steps to reproduce\r\ntest code at foo.js: \r\nsetInterval(function() {\r\n  console.log(process.argv[2]);\r\n}, 1000);\r\n\r\n...\r\n\r\n#### Software versions used\r\n\r\n```\r\nOS       windows server 2012 & windows 7  :\r\nnode.js  4.5.0  :\r\nPM2    2.0.15 :\r\n```\r\n\r\n#### PM2 Log output\r\n\r\n```\r\n\r\nsometimes I can get the error log below, and no process will be created:\r\n\r\n===============================================================================\r\n--- PM2 global error caught ---------------------------------------------------\r\nTime                 : Fri Sep 30 2016 02:12:35 GMT+0800 (China Standard Time)\r\nThis socket is closed.\r\nError: This socket is closed.\r\n    at Socket._writeGeneric (net.js:648:19)\r\n    at Socket._write (net.js:700:8)\r\n    at doWrite (_stream_writable.js:300:12)\r\n    at clearBuffer (_stream_writable.js:407:7)\r\n    at onwrite (_stream_writable.js:339:7)\r\n    at Socket.WritableState.onwrite (_stream_writable.js:89:5)\r\n    at WriteWrap.afterWrite (net.js:781:12)\r\n===============================================================================\r\n\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/Unitech/pm2/issues/comments/250779415","html_url":"https://github.com/Unitech/pm2/issues/2417#issuecomment-250779415","issue_url":"https://api.github.com/repos/Unitech/pm2/issues/2417","id":250779415,"user":{"login":"yuqinlear","id":2876055,"avatar_url":"https://avatars.githubusercontent.com/u/2876055?v=3","gravatar_id":"","url":"https://api.github.com/users/yuqinlear","html_url":"https://github.com/yuqinlear","followers_url":"https://api.github.com/users/yuqinlear/followers","following_url":"https://api.github.com/users/yuqinlear/following{/other_user}","gists_url":"https://api.github.com/users/yuqinlear/gists{/gist_id}","starred_url":"https://api.github.com/users/yuqinlear/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yuqinlear/subscriptions","organizations_url":"https://api.github.com/users/yuqinlear/orgs","repos_url":"https://api.github.com/users/yuqinlear/repos","events_url":"https://api.github.com/users/yuqinlear/events{/privacy}","received_events_url":"https://api.github.com/users/yuqinlear/received_events","type":"User","site_admin":false},"created_at":"2016-09-30T15:45:39Z","updated_at":"2016-09-30T15:45:39Z","body":"@Unitech what is the callback function once pm2.start succeeds? just want to make sure because errback(err,proc) sounds like an error call back."}}
{"actor":{"display_login":"yuqinlear"}}