Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[minor] Always try to parse the response before calling next()

  • Loading branch information...
commit d6c7590a0337c96446726deb0709253d4be69cfb 1 parent dcbfc70
Charlie Robbins authored December 02, 2011

Showing 1 changed file with 29 additions and 22 deletions. Show diff stats Hide diff stats

  1. 51  lib/forever.js
51  lib/forever.js
@@ -96,15 +96,41 @@ function getAllProcesses(callback) {
96 96
   function getProcess(name, next) {
97 97
     var fullPath = path.join(sockPath, name),
98 98
         socket = new net.Socket({ type: 'unix' }),
  99
+        parsed = false,
99 100
         data = '';
100 101
 
  102
+    function tryParse() {
  103
+      if (!parsed) {
  104
+        parsed = true;
  105
+      
  106
+        var monitors;
  107
+        try {
  108
+          monitors = JSON.parse(data);
  109
+        }
  110
+        catch (ex) {
  111
+          //
  112
+          // Ignore errors
  113
+          //
  114
+        }
  115
+      
  116
+        //
  117
+        // Be a little lazier about loading results
  118
+        //
  119
+        if (monitors && monitors.monitors) {
  120
+          results = results.concat(monitors.monitors);
  121
+        }
  122
+      
  123
+        next();
  124
+      }
  125
+    }
  126
+
101 127
     socket.on('error', function (err) {
102 128
       if (err.code === 'ECONNREFUSED') {
103 129
         try {
104 130
           fs.unlinkSync(fullPath);
105 131
         }
106 132
         catch (ex) { }
107  
-        return next();
  133
+        return tryParse();
108 134
       }
109 135
       else if (err.code === 'EACCES') {
110 136
         forever.log.warn('Error contacting: ' + fullPath.magenta);
@@ -113,33 +139,14 @@ function getAllProcesses(callback) {
113 139
         forever.log.error('Unknown error (' + err.code + ') when contacting: ' + fullPath.magenta);
114 140
       }
115 141
 
116  
-      next();
  142
+      tryParse();
117 143
     });
118 144
 
119 145
     socket.on('data', function (msg) {
120 146
       data += msg;
121 147
     });
122 148
 
123  
-    socket.on('close', function () {
124  
-      var monitors;
125  
-      try {
126  
-        monitors = JSON.parse(data);
127  
-      }
128  
-      catch (ex) {
129  
-        //
130  
-        // Ignore errors
131  
-        //
132  
-      }
133  
-      
134  
-      //
135  
-      // Be a little lazier about loading results
136  
-      //
137  
-      if (monitors && monitors.monitors) {
138  
-        results = results.concat(monitors.monitors);
139  
-      }
140  
-      
141  
-      next();
142  
-    });
  149
+    socket.on('close', tryParse);
143 150
 
144 151
     socket.connect(fullPath);
145 152
   }

0 notes on commit d6c7590

Please sign in to comment.
Something went wrong with that request. Please try again.