implement Gearman web interface with available functions
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 14 Mar 2011 19:51:54 +0000 (19:51 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 14 Mar 2011 19:51:54 +0000 (19:51 +0000)
public/gearman.html [new file with mode: 0644]
web_ui.pl

diff --git a/public/gearman.html b/public/gearman.html
new file mode 100644 (file)
index 0000000..7e80567
--- /dev/null
@@ -0,0 +1,58 @@
+<!doctype html>
+<html xmlns:ng="http://angularjs.org">
+ <script src="http://code.angularjs.org/angular-0.9.12.min.js" ng:autobind></script>
+ <body>
+  <script>
+    function GearmanCntl($xhr) {
+      var self = this;
+  
+      this.status_update = function() {
+        $xhr('JSON', '/_g/status?callback=JSON_CALLBACK', function(code,response) {
+          self.g = response;
+        });
+      }
+      this.status_update();
+      this.fetch = function() {
+        self.clear();
+       self.url = '/g/' + self.function + '/' + self.args + '?callback=JSON_CALLBACK';
+        $xhr('JSON', self.url, function(code, response) {
+          self.code = code;
+          self.response = response;
+        });
+      };
+   
+      this.clear = function() {
+        self.code = null;
+        self.response = null;
+      };
+    }
+    GearmanCntl.$inject = ['$xhr'];
+  </script>
+  <div ng:controller="GearmanCntl">
+    <select name="function">
+    <option ng:repeat="f in g.status" ng:show="f.available" value="{{f.function}}">{{f.function}}</option>
+    <input type="text" name="args"     value="127.0.0.1" size="40"/>
+    <button ng:click="fetch()">fetch</button>
+    <button ng:click="clear()">clear</button>
+
+    <pre>
+url={{url}}
+code={{code}}
+response={{response}}
+    </pre>
+
+<table>
+<tr><th>function</th><th>total</th><th>running</th><th>available</th></tr>
+<tr ng:repeat="f in g.status">
+<td>{{f.function}}</td>
+<td>{{f.total}}</td>
+<td>{{f.running}}</td>
+<td>{{f.available}}</td>
+</tr>
+</table>
+<button ng:click="status_update()">update</button>
+
+  </div>
+ </body>
+</html>
index 78823d0..455777f 100755 (executable)
--- a/web_ui.pl
+++ b/web_ui.pl
@@ -51,6 +51,26 @@ get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
        die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
        _render_jsonp( $self, $ret );
 };
+
+get '/_g/status' => sub {
+       my $self = shift;
+
+       my $ret = $gearman->req( 'TEXT' => 'status' );
+       warn "# status:\n$ret";
+
+       my @c = qw(function total running available);
+
+       my $status;
+       foreach my $l ( split(/\n/,$ret) ) {
+               my @v = split(/\t/, $l);
+               my $h;
+               $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
+               push @$status, $h;
+       }
+       warn "## ", dump $status;
+       _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
+};
+
 app->start;
 __DATA__
 
@@ -59,6 +79,14 @@ __DATA__
 % title 'Gearman demo';
 Welcome to Mojolicious Gearman integration demo!
 
+<p>
+
+<%= link_to 'Gearman web interface' => 'gearman.html' %>
+
+<p>
+
+Low-level API tests:
+
 <%= link_to 'HTTP ping' => 'ping_http' %>
 <%= link_to 'Gearman ping' => 'ping_g' %>