From d749187e6d399f00e97ae0ea2978a7761ddc72fa Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Tue, 13 Dec 2016 12:19:22 +0100
Subject: [PATCH] Make sure that no more than 3 PhantomJS processes are running
 at once

---
 plugins/reporting/index.js | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/plugins/reporting/index.js b/plugins/reporting/index.js
index d5a5c97..b4039d8 100644
--- a/plugins/reporting/index.js
+++ b/plugins/reporting/index.js
@@ -19,11 +19,13 @@ var Ping = require('../../models/ping');
 
 exports.initWebApp = function(options) {
 
-  var api = options.api;
   var config = options.config;
-  var dashboard = options.dashboard;
+  var count = 0;
 
   Ping.on('afterInsert', function(ping) {
+    if (count > 3) {
+      return;
+    }
     Check.findById(ping.check, function(err, check) {
       if (err) {
         return;
@@ -36,6 +38,13 @@ exports.initWebApp = function(options) {
       var phantomjs = require('phantomjs-prebuilt'),
           path = require('path'),
           program = phantomjs.exec(path.join(__dirname, 'phantomjs-script.js'), config['url'], config['basicAuth']['username'], config['basicAuth']['password'], check._id, check.name, 'month', Date.now());
+
+      count++;
+      console.log('PhantomJS started, currently ' + count + ' processes.');
+      program.on('exit', function() {
+        count--;
+        console.log('PhantomJS finished');
+      });
     });
   });
 
-- 
GitLab