Overhall of Spawn code, fixes various hanging issues, blocking issues, and more.
authorAlan Knowles <alan@akbkhome.com>
Sun, 18 Sep 2011 15:59:11 +0000 (23:59 +0800)
committerAlan Knowles <alan@akbkhome.com>
Sun, 18 Sep 2011 15:59:11 +0000 (23:59 +0800)
Spawn.js

index 5a281b9..473d66e 100644 (file)
--- a/Spawn.js
+++ b/Spawn.js
@@ -25,6 +25,9 @@ var GLib      = imports.gi.GLib;
 *   }
 *  });
 * 
+*
+*
+
 *
 *
 *  CRITICAL - needs this change to gir in GLib-2.0.gir g_spawn_async_with_pipes
@@ -98,6 +101,7 @@ function Spawn(cfg) {
 
 Spawn.prototype = {
     
+    ctx : false, // the mainloop ctx.
     listeners : false,
     async : false,
     env : null,
@@ -121,9 +125,6 @@ Spawn.prototype = {
      * @property pid {Number} pid of child process (of false if it's not running)
      */
     pid : false,
-    
-   
-    
     /**
      * @property in_ch {GLib.IOChannel} input io channel
      */
@@ -136,15 +137,13 @@ Spawn.prototype = {
      * @property err_ch {GLib.IOChannel} stderr io channel
      */
     err_ch : false,
-    
     /**
      * 
      * @method run
      * Run the configured command.
-     * 
+     * result is applied to object properties (eg. 'output' or 'stderr')
+     * @returns {Object} self.
      */
-    
-    
     run : function()
     {
         
@@ -152,7 +151,6 @@ Spawn.prototype = {
         
         var err_src = false;
         var out_src = false;
-        var ctx = false; 
         var ret = {};
         
         if (this.debug) {
@@ -163,13 +161,35 @@ Spawn.prototype = {
             GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH , 
             null, null, ret);
             
-       print(JSON.stringify(ret));    
+       //print(JSON.stringify(ret));    
         this.pid = ret.child_pid;
         
         if (this.debug) {
             print("PID: " + this.pid);
         }
         
+        
+       
+        GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
+            _this.result = result;
+            if (_this.debug) {
+                print("child_watch_add : result: " + result);
+            }
+            _this.read(_this.out_ch);
+            _this.read(_this.err_ch);
+            
+            GLib.spawn_close_pid(_this.pid);
+            _this.pid = false;
+            if (_this.ctx) {
+                _this.ctx.quit();
+            }
+            tidyup();
+           //print("DONE TIDYUP");
+            if (_this.listeners.finish) {
+                _this.listeners.finish.call(this, _this.result);
+            }
+        });
+        
         function tidyup()
         {
             if (_this.pid) {
@@ -197,46 +217,31 @@ Spawn.prototype = {
         this.err_ch = GLib.io_channel_unix_new(ret.standard_error);
         
         // make everything non-blocking!
-        this.in_ch.set_flags (GLib.IOFlags.NONBLOCK);
-        this.out_ch.set_flags (GLib.IOFlags.NONBLOCK);
-        this.err_ch.set_flags (GLib.IOFlags.NONBLOCK);
-        
-        
-        GLib.child_watch_add(GLib.PRIORITY_DEFAULT, this.pid, function(pid, result) {
-            _this.result = result;
-            if (_this.debug) {
-                print("child_watch_add : result: " + result);
-            }
-            _this.read(_this.out_ch);
-            _this.read(_this.err_ch);
             
-            GLib.spawn_close_pid(_this.pid);
-            _this.pid = false;
-            if (ctx) {
-                ctx.quit();
-            }
-            tidyup();
-            if (_this.listeners.finish) {
-                _this.listeners.finish.call(this, _this.result);
-            }
-        });
         
         
+         // using NONBLOCKING only works if io_add_watch
+       //returns true/false in right conditions
+       this.in_ch.set_flags (GLib.IOFlags.NONBLOCK);
+       this.out_ch.set_flags (GLib.IOFlags.NONBLOCK);
+       this.err_ch.set_flags (GLib.IOFlags.NONBLOCK);
        
 
       
         // add handlers for output and stderr.
         out_src= GLib.io_add_watch(this.out_ch, GLib.PRIORITY_DEFAULT, 
-            GLib.IOCondition.OUT + GLib.IOCondition.IN  + GLib.IOCondition.PRI, function()
-        {
-            _this.read(_this.out_ch);
+            GLib.IOCondition.OUT + GLib.IOCondition.IN  + GLib.IOCondition.PRI +  GLib.IOCondition.HUP +  GLib.IOCondition.ERR,
+            function() {
             
-        });
+               return  _this.read(_this.out_ch);
+            
+            }
+        );
         err_src= GLib.io_add_watch(this.err_ch, GLib.PRIORITY_DEFAULT, 
-            GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT, 
+            GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT +  GLib.IOCondition.HUP
             function()
         {
-            _this.read(_this.err_ch);
+            return _this.read(_this.err_ch);
              
         });
         
@@ -246,8 +251,18 @@ Spawn.prototype = {
         if (this.pid !== false) {
             // child can exit before we get this far..
             if (this.listeners.input) {
+               print("Trying to call listeners");
                 try {
                     this.write(this.listeners.input.call(this));
+                    // this probably needs to be a bit smarter...
+                   //but... let's close input now..
+                   this.in_ch.close();
+                   _this.in_ch = false;
+                  
+                   
+                   
+                   
+                   
                 } catch (e) {
                     tidyup();
                     throw e;
@@ -269,9 +284,10 @@ Spawn.prototype = {
                 print("starting main loop");
             }
            
-            ctx = new GLib.MainLoop.c_new (null, false);
-            ctx.run(false); // wait fore exit?
+            this.ctx = new GLib.MainLoop.c_new (null, false);
+            this.ctx.run(false); // wait fore exit?
             
+            //print("main_loop done!");
         } else {
             tidyup(); // tidyup get's called in main loop. 
         }
@@ -296,12 +312,19 @@ Spawn.prototype = {
         if (!this.in_ch) {
             return 0; // input is closed
         }
-        var ret = {};
+       //print("write: " + str);
+       // NEEDS GIR FIX! for return value.. let's ignore for the time being..
+       //var ret = {};
+        //var res = this.in_ch.write_chars(str, str.length, ret);
         var res = this.in_ch.write_chars(str, str.length);
+       
+       //print("write_char retunred:" + JSON.stringify(res) +  ' ' +JSON.stringify(ret)  );
+       
         if (res != GLib.IOStatus.NORMAL) {
             throw "Write failed";
         }
-        return ret.bytes_written;
+        //return ret.value;
+        return str.length;
         
     },
     
@@ -313,16 +336,17 @@ Spawn.prototype = {
     read: function(ch) 
     {
         var prop = ch == this.out_ch ? 'output' : 'stderr';
-       print("prop: " + prop);
+       // print("prop: " + prop);
         var _this = this;
         
+       
         //print(JSON.stringify(ch, null,4));
         while (true) {
             var x = {};
             var status = ch.read_line(x);
-           //print("READ LINE STRING STATUS: " + status);
-             //print("got: " + JSON.stringify(x, null,4));
-
+            // print('status: '  +JSON.stringify(status));
+            // print(JSON.stringify(x));
             switch(status) {
                 case GLib.IOStatus.NORMAL:
                
@@ -332,18 +356,35 @@ Spawn.prototype = {
                     }
                     _this[prop] += x.str_return;
                     if (_this.debug) {
-                        print(prop + ':' + x.str_return);
+                        print(prop + ':' + x.str_return.replace(/\n/, ''));
                     }
+                    if (this.async) {
+                        try {
+                            if (imports.gi.Gtk.events_pending()) {
+                                imports.gi.Gtk.main_iteration();
+                            }
+                        } catch(e) {
+                            
+                        }
+                    }
+                    
+                    //this.ctx.iteration(true);
                    continue;
                 case GLib.IOStatus.AGAIN:   
+                   //print("Should be called again.. waiting for more data..");
+                   return true;
                     break;
                 case GLib.IOStatus.ERROR:    
                 case GLib.IOStatus.EOF:   
+                   return false;
                    break;
                 
             }
             break;
         }
+       
+        //print("RETURNING");
+         return false; // allow it to be called again..
     }
     
 };
@@ -379,4 +420,6 @@ Seed.print(run({
 }));
 } catch (e) { print( 'Error: ' + JSON.stringify(e)); }
 
  */